forked from habeanf/yap
-
Notifications
You must be signed in to change notification settings - Fork 20
/
heblex.go
319 lines (300 loc) · 7.72 KB
/
heblex.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package ma
import (
"yap/alg/graph"
"yap/nlp/format/lex"
. "yap/nlp/types"
"yap/util"
"fmt"
"log"
"regexp"
"strings"
)
const ESTIMATED_MORPHS_PER_TOKEN = 5
type BGULex struct {
MaxPrefixLen int
Prefixes map[string][]BasicMorphemes
Lex map[string][]BasicMorphemes
Files []string
Stats *AnalyzeStats
AlwaysNNP bool
LogOOV bool
MAType string
}
var (
PUNCT = map[string]string{
":": "yyCLN",
",": "yyCM",
"-": "yyDASH",
".": "yyDOT",
"...": "yyELPS",
"!": "yyEXCL",
"(": "yyLRB",
"?": "yyQM",
")": "yyRRB",
";": "yySCLN",
"\"": "yyQUOT",
}
OOVMSRS = []string{
"NNP-",
"NNP-gen=F|gen=M|num=S",
"NNP-gen=M|num=S",
"NNP-gen=F|num=S",
"NN-gen=M|num=P|num=S",
"NN-gen=M|num=S",
"NN-gen=F|num=S",
"NN-gen=M|num=P",
"NN-gen=F|num=P",
}
REGEX = []struct {
RE *regexp.Regexp
POS string
}{
{regexp.MustCompile("^\\d+(\\.\\d+)?$|^\\d{1,3}(,\\d{3})*(\\.\\d+)?$"), "CD"},
{regexp.MustCompile("\\d"), "NCD"},
}
_ MorphologicalAnalyzer = &BGULex{}
)
func (l *BGULex) loadTokens(file, format string) {
tokens, err := lex.ReadFile(file, format, l.MAType)
if err != nil {
panic(fmt.Sprintf("Failed to load %v: %v", file, err))
}
var m map[string][]BasicMorphemes
if format == "prefix" {
l.Prefixes = make(map[string][]BasicMorphemes, len(tokens))
m = l.Prefixes
} else if format == "lexicon" {
l.Lex = make(map[string][]BasicMorphemes, len(tokens))
m = l.Lex
}
log.Println("Found", len(tokens), "tokens in lexicon file:", file)
for _, token := range tokens {
if cur, exists := m[token.Token]; exists {
m[token.Token] = append(cur, token.Morphemes...)
} else {
m[token.Token] = token.Morphemes
}
}
}
func (l *BGULex) LoadPrefixes(file string) {
l.loadTokens(file, "prefix")
l.MaxPrefixLen = 0
for _, morphs := range l.Prefixes {
if l.MaxPrefixLen < len(morphs) {
l.MaxPrefixLen = len(morphs)
}
}
log.Println("Loaded", len(l.Prefixes), "prefixes from lexicon")
}
func (l *BGULex) LoadLex(file string, nnpnofeats bool) {
lex.ADD_NNP_NO_FEATS = nnpnofeats
l.loadTokens(file, "lexicon")
log.Println("Loaded", len(l.Lex), "tokens from lexicon")
}
func makeMorphWithPOS(input, lemma, POS string) []BasicMorphemes {
return []BasicMorphemes{BasicMorphemes([]*Morpheme{
&Morpheme{
BasicDirectedEdge: graph.BasicDirectedEdge{0, 0, 1},
Form: input,
Lemma: lemma,
CPOS: POS,
POS: POS,
FeatureStr: "",
},
})}
}
func (l *BGULex) AddOOVAnalysis(lat *Lattice, prefix BasicMorphemes, hostStr string, numToken int) {
var OOVPOS, featuresStr string
for _, msr := range OOVMSRS {
// if logAnalyze {
// log.Println("Adding msr", msr)
// }
msrsplit := strings.Split(msr, "-")
OOVPOS, featuresStr = msrsplit[0], msrsplit[1]
if l.MAType == "ud" {
OOVPOS = util.HEB2UDPOS[OOVPOS]
if len(featuresStr) > 0 {
featuresStr = util.Heb2UDFeaturesString(featuresStr)
}
}
newMorph := []BasicMorphemes{BasicMorphemes([]*Morpheme{
&Morpheme{
BasicDirectedEdge: graph.BasicDirectedEdge{0, 0, 1},
Form: hostStr,
Lemma: hostStr,
CPOS: OOVPOS,
POS: OOVPOS,
FeatureStr: featuresStr,
},
})}
lat.AddAnalysis(prefix, newMorph, numToken)
}
}
func checkRegexes(input string) ([]BasicMorphemes, bool) {
for _, curRegex := range REGEX {
if curRegex.RE.MatchString(input) {
return makeMorphWithPOS(input, "", curRegex.POS), true
}
}
return nil, false
}
var logAnalyze bool = false
func (l *BGULex) OOVForLen(lat *Lattice, input string, startingNode, numToken, prefixLen int) bool {
var (
found bool
hostStr string
)
if len(input) < prefixLen*2 {
return found
}
prefixLat, prefixExists := l.Prefixes[input[0:prefixLen*2]]
// log.Println("\tPrefixes", input[0:prefixLen*2], prefixExists)
if prefixExists {
hostStr = input[2*prefixLen:]
if len(hostStr) > 2 {
// Always add NNP hosts for len(hosts)>1 (unicode = 2 runes)
for _, prefix := range prefixLat {
l.AddOOVAnalysis(lat, prefix, hostStr, numToken)
// lat.AddAnalysis(prefix, l.OOVAnalysis(hostStr), numToken)
}
}
}
return found
}
func (l *BGULex) analyzeTokenForLen(lat *Lattice, input string, startingNode, numToken, prefixLen int) bool {
var (
found, hostExists bool
hostLat []BasicMorphemes
hostStr string
)
if len(input) < prefixLen*2 {
return found
}
prefixLat, prefixExists := l.Prefixes[input[0:prefixLen*2]]
// log.Println("\tPrefixes", input[0:prefixLen*2], prefixExists)
if prefixExists {
hostStr = input[2*prefixLen:]
if l.AlwaysNNP {
if len(hostStr) > 2 {
// Always add NNP hosts for len(hosts)>1 (unicode = 2 runes)
for _, prefix := range prefixLat {
l.AddOOVAnalysis(lat, prefix, hostStr, numToken)
// lat.AddAnalysis(prefix, l.OOVAnalysis(hostStr), numToken)
}
}
}
hostLat, hostExists = l.Lex[hostStr]
if !hostExists {
hostLat, hostExists = checkRegexes(hostStr)
}
// log.Println("\tHosts", input[2*prefixLen:], hostExists)
if hostExists {
for _, prefix := range prefixLat {
// log.Println("\t\tAdding", prefix, hostLat)
lat.AddAnalysis(prefix, hostLat, numToken)
}
found = true
}
}
return found
}
func (l *BGULex) AnalyzeToken(input string, startingNode, indexToken int) (*Lattice, interface{}) {
numToken := indexToken + 1
if logAnalyze {
log.Println("Analyzing token", numToken, "starting at", startingNode)
}
lat := &Lattice{
Token: Token(input),
Morphemes: make(Morphemes, 0, ESTIMATED_MORPHS_PER_TOKEN),
Next: make(map[int][]int, ESTIMATED_MORPHS_PER_TOKEN),
BottomId: startingNode,
TopId: startingNode,
}
lat.Next[0] = make([]int, 0, 1)
var (
hostLat []BasicMorphemes
hostExists, anyExists bool
punctPOS string
)
if punctVal, exists := PUNCT[input]; exists {
punctPOS = punctVal
if l.MAType == "ud" {
punctPOS = "PUNCT"
}
m := &Morpheme{
BasicDirectedEdge: graph.BasicDirectedEdge{0, 0, 0},
Form: input,
CPOS: punctPOS,
POS: punctPOS,
}
basics := []BasicMorphemes{BasicMorphemes{m}}
lat.AddAnalysis(nil, basics, numToken)
return lat, nil
}
if l.AlwaysNNP {
l.AddOOVAnalysis(lat, nil, input, numToken)
// oovLat := l.OOVAnalysis(input)
// lat.AddAnalysis(nil, oovLat, numToken)
}
hostLat, hostExists = l.Lex[input]
if !hostExists {
hostLat, hostExists = checkRegexes(input)
}
if hostExists {
if logAnalyze {
log.Println("\tPrefix 0")
}
lat.AddAnalysis(nil, hostLat, numToken)
anyExists = true
} else {
if !l.AlwaysNNP {
l.AddOOVAnalysis(lat, nil, input, numToken)
// oovLat := l.OOVAnalysis(input)
// lat.AddAnalysis(nil, oovLat, numToken)
}
}
for i := 1; i < util.Min(l.MaxPrefixLen, len(input)); i++ {
if logAnalyze {
log.Println("\ti is", i)
}
found := l.analyzeTokenForLen(lat, input, startingNode, numToken, i)
anyExists = anyExists || found
}
if !anyExists {
// if logAnalyze {
if l.LogOOV {
log.Println("Token", numToken, "is OOV:", input)
}
for i := 1; i < util.Min(l.MaxPrefixLen, len(input)); i++ {
if logAnalyze {
log.Println("\ti is", i)
}
_ = l.OOVForLen(lat, input, startingNode, numToken, i)
}
// }
if l.Stats != nil {
l.Stats.OOVTokens++
l.Stats.AddOOVToken(input)
}
}
lat.Optimize()
return lat, nil
}
func (l *BGULex) Analyze(input []string) (LatticeSentence, interface{}) {
retval := make(LatticeSentence, len(input))
var (
lat *Lattice
curNode int
)
for i, token := range input {
if l.Stats != nil {
l.Stats.TotalTokens++
l.Stats.AddToken(token)
}
lat, _ = l.AnalyzeToken(token, curNode, i)
curNode = lat.Top()
// log.Println("New top is", curNode)
retval[i] = *lat
}
return retval, nil
}