-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmorpheme.go
161 lines (131 loc) · 3.56 KB
/
morpheme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gosudachi
import (
"github.com/msnoigrs/gosudachi/dictionary"
)
type Morpheme struct {
list *MorphemeList
index int
wordInfo *dictionary.WordInfo
}
func newMorpheme(list *MorphemeList, index int) *Morpheme {
return &Morpheme{
list: list,
index: index,
}
}
func (m *Morpheme) Begin() int {
return m.list.GetBegin(m.index)
}
func (m *Morpheme) End() int {
return m.list.GetEnd(m.index)
}
func (m *Morpheme) Surface() string {
return m.list.GetSurface(m.index)
}
func (m *Morpheme) PartOfSpeech() []string {
wi := m.GetWordInfo()
return m.list.grammar.GetPartOfSpeechString(wi.PosId)
}
func (m *Morpheme) DictionaryForm() string {
wi := m.GetWordInfo()
return wi.DictionaryForm
}
func (m *Morpheme) NormalizedForm() string {
wi := m.GetWordInfo()
return wi.NormalizedForm
}
func (m *Morpheme) ReadingForm() string {
wi := m.GetWordInfo()
return wi.ReadingForm
}
func (m *Morpheme) Split(mode string) *MorphemeList {
wi := m.GetWordInfo()
return m.list.Split(mode, m.index, wi)
}
func (m *Morpheme) IsOOV() bool {
return m.list.IsOOV(m.index)
}
func (m *Morpheme) GetWordId() int {
return m.list.GetWordId(m.index)
}
func (m *Morpheme) GetDictionaryId() int {
return m.list.GetDictionaryId(m.index)
}
func (m *Morpheme) GetWordInfo() *dictionary.WordInfo {
if m.wordInfo == nil {
wordInfo := m.list.GetWordInfo(m.index)
m.wordInfo = wordInfo
}
return m.wordInfo
}
type MorphemeList struct {
inputText *InputText
grammar *dictionary.Grammar
lexicon *dictionary.LexiconSet
path []*LatticeNode
}
func NewMorphemeList(inputText *InputText, grammar *dictionary.Grammar, lexicon *dictionary.LexiconSet, path []*LatticeNode) *MorphemeList {
return &MorphemeList{
inputText: inputText,
grammar: grammar,
lexicon: lexicon,
path: path,
}
}
func (l *MorphemeList) Length() int {
return len(l.path)
}
func (l *MorphemeList) Get(index int) *Morpheme {
return newMorpheme(l, index)
}
func (l *MorphemeList) GetBegin(index int) int {
return l.inputText.GetOriginalIndex(l.path[index].Begin)
}
func (l *MorphemeList) GetEnd(index int) int {
return l.inputText.GetOriginalIndex(l.path[index].End)
}
func (l *MorphemeList) GetSurface(index int) string {
begin := l.GetBegin(index)
end := l.GetEnd(index)
return string([]rune(l.inputText.OriginalText)[begin:end])
}
func (l *MorphemeList) GetWordInfo(index int) *dictionary.WordInfo {
return l.path[index].GetWordInfo()
}
func (l *MorphemeList) Split(mode string, index int, wi *dictionary.WordInfo) *MorphemeList {
var wordIds []int32
switch mode {
case "A":
wordIds = wi.AUnitSplit
case "B":
wordIds = wi.BUnitSplit
default:
return NewMorphemeList(l.inputText, l.grammar, l.lexicon, []*LatticeNode{l.path[index]})
}
if len(wordIds) == 0 || len(wordIds) == 1 {
return NewMorphemeList(l.inputText, l.grammar, l.lexicon, []*LatticeNode{l.path[index]})
}
offset := l.path[index].Begin
nodes := make([]*LatticeNode, len(wordIds), len(wordIds))
for i, wid := range wordIds {
n := NewLatticeNode(l.lexicon, 0, 0, 0, wid)
n.Begin = offset
wi := n.GetWordInfo()
offset += int(wi.HeadwordLength)
n.End = offset
nodes[i] = n
}
return NewMorphemeList(l.inputText, l.grammar, l.lexicon, nodes)
}
func (l *MorphemeList) IsOOV(index int) bool {
return l.path[index].IsOOV()
}
func (l *MorphemeList) GetWordId(index int) int {
return l.path[index].GetWordId()
}
func (l *MorphemeList) GetDictionaryId(index int) int {
return l.path[index].GetDictionaryId()
}
func (l *MorphemeList) GetInternalCost() int {
return l.path[len(l.path)-1].GetPathCost() - l.path[0].GetPathCost()
}