-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprobability.go
executable file
·404 lines (351 loc) · 9.77 KB
/
probability.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package linguo
import (
"container/list"
set "gopkg.in/fatih/set.v0"
"strconv"
"strings"
)
const (
PROBABILITY_SINGLE_TAG = 1 + iota
PROBABILITY_CLASS_TAG
PROBABILITY_FORM_TAG
PROBABILITY_UNKNOWN
PROBABILITY_THEETA
PROBABILITY_SUFFIXES
PROBABILITY_SUFF_BIASS
PROBABILITY_LAMBDA_LEX
PROBABILITY_LAMBDA_CLASS
PROBABILITY_TAGSET
)
type Probability struct {
ProbabilityThreshold float64
Tags *TagSet
BiassSuffixes float64
LidstoneLambdaLexical float64
LidstoneLambdaClass float64
activateGuesser bool
singleTags map[string]float64
classTags map[string]map[string]float64
lexicalTags map[string]map[string]float64
unkTags map[string]float64
unkSuffS map[string]map[string]float64
theeta float64
longSuff int
}
func NewProbability(probFile string, Threashold float64) *Probability {
this := Probability{
singleTags: make(map[string]float64),
classTags: make(map[string]map[string]float64),
lexicalTags: make(map[string]map[string]float64),
unkTags: make(map[string]float64),
unkSuffS: make(map[string]map[string]float64),
}
line := ""
var key, frq, tag, ftags string
var probab, sumUnk, sumSing, count float64
var tmpMap map[string]float64
this.activateGuesser = true
this.ProbabilityThreshold = Threashold
this.BiassSuffixes = 0.3
this.LidstoneLambdaLexical = 0.1
this.LidstoneLambdaClass = 1.0
cfg := NewConfigFile(false, "##")
cfg.AddSection("SingleTagFreq", PROBABILITY_SINGLE_TAG)
cfg.AddSection("ClassTagFreq", PROBABILITY_CLASS_TAG)
cfg.AddSection("FormTagFreq", PROBABILITY_FORM_TAG)
cfg.AddSection("UnknownTags", PROBABILITY_UNKNOWN)
cfg.AddSection("Theeta", PROBABILITY_THEETA)
cfg.AddSection("Suffixes", PROBABILITY_SUFFIXES)
cfg.AddSection("BiassSuffixes", PROBABILITY_SUFF_BIASS)
cfg.AddSection("LidstoneLambdaLexical", PROBABILITY_LAMBDA_LEX)
cfg.AddSection("LidstoneLambdaClass", PROBABILITY_LAMBDA_CLASS)
cfg.AddSection("TagsetFile", PROBABILITY_TAGSET)
if !cfg.Open(probFile) {
CRASH("Error opening file "+probFile, MOD_PROBABILITY)
}
sumUnk = 0
sumSing = 0
this.longSuff = 0
for cfg.GetContentLine(&line) {
items := Split(line, " ")
switch cfg.GetSection() {
case PROBABILITY_SINGLE_TAG:
{
key = items[0]
frq = items[1]
probab, _ = strconv.ParseFloat(frq, 64)
this.singleTags[key] = probab
sumSing += probab
break
}
case PROBABILITY_CLASS_TAG:
{
tmpMap = make(map[string]float64)
key = items[0]
for i := 1; i < len(items)-1; i = i + 2 {
tag = items[i]
frq = items[i+1]
probab, _ = strconv.ParseFloat(frq, 64)
tmpMap[tag] = probab
}
this.classTags[key] = tmpMap
break
}
case PROBABILITY_FORM_TAG:
{
tmpMap = make(map[string]float64)
key = items[0]
//clas = items[1]
for i := 2; i < len(items)-1; i = i + 2 {
tag = items[i]
frq = items[i+1]
probab, _ = strconv.ParseFloat(frq, 64)
tmpMap[tag] = probab
}
this.lexicalTags[key] = tmpMap
break
}
case PROBABILITY_UNKNOWN:
{
key = items[0]
frq = items[1]
probab, _ = strconv.ParseFloat(frq, 64)
this.unkTags[key] = probab
sumUnk += probab
break
}
case PROBABILITY_THEETA:
{
frq = items[0]
this.theeta, _ = strconv.ParseFloat(frq, 64)
break
}
case PROBABILITY_SUFFIXES:
{
tmpMap = make(map[string]float64)
key = items[0]
frq1 := items[1]
this.longSuff = If(len(key) > this.longSuff, len(key), this.longSuff).(int)
count, _ = strconv.ParseFloat(frq1, 64)
for i := 2; i < len(items)-1; i = i + 2 {
tag = items[i]
frq = items[i+1]
probab, _ = strconv.ParseFloat(frq, 64)
probab /= count
tmpMap[tag] = probab
}
this.unkSuffS[key] = tmpMap
break
}
case PROBABILITY_SUFF_BIASS:
{
frq = items[0]
this.BiassSuffixes, _ = strconv.ParseFloat(frq, 64)
break
}
case PROBABILITY_LAMBDA_LEX:
{
frq = items[0]
this.LidstoneLambdaLexical, _ = strconv.ParseFloat(frq, 64)
break
}
case PROBABILITY_LAMBDA_CLASS:
{
frq = items[0]
this.LidstoneLambdaClass, _ = strconv.ParseFloat(frq, 64)
break
}
case PROBABILITY_TAGSET:
{
ftags = items[0]
break
}
default:
break
}
}
for k, _ := range this.unkTags {
this.unkTags[k] /= sumUnk
}
for k, _ := range this.singleTags {
this.singleTags[k] /= sumSing
}
path := probFile[0:strings.LastIndex(probFile, "/")]
this.Tags = NewTagset(path + "/" + strings.Replace(ftags, "./", "", -1))
TRACE(3, "analyzer succesfully created", MOD_PROBABILITY)
return &this
}
func (this *Probability) Analyze(se *Sentence) {
for pos := se.Front(); pos != nil; pos = pos.Next() {
this.AnnotateWord(pos.Value.(*Word))
}
}
func (this *Probability) AnnotateWord(w *Word) {
var sum float64
na := w.getNAnalysis()
TRACE(2, "--Assigning probabilities to: "+w.getForm(), MOD_PROBABILITY)
if na > 0 && (w.foundInDict() || strings.HasPrefix(w.getTag(0), "F") || strings.HasPrefix(w.getTag(0), "Z") || w.hasRetokenizable()) {
//TRACE(2, "Form with analysis. Found in dict (" + )
this.smoothing(w)
sum = 1
} else if this.activateGuesser {
TRACE(2, "Form with NO analysis. Guessing "+w.getForm(), MOD_PROBABILITY)
var mass float64 = 1.0
for li := w.Front(); li != nil; li = li.Next() {
li.Value.(*Analysis).setProb(mass / float64(w.getNAnalysis()))
}
sum = this.guesser(w, mass)
for li := w.Front(); li != nil; li = li.Next() {
li.Value.(*Analysis).setProb(li.Value.(*Analysis).getProb() / sum)
}
na = w.getNAnalysis()
}
w.sort()
w.selectAllAnalysis(0)
for li := w.Front(); li != nil; li = li.Next() {
rtk := li.Value.(*Analysis).getRetokenizable()
for k := rtk.Front(); k != nil; k = k.Next() {
this.AnnotateWord(k.Value.(*Word))
}
}
}
func (this *Probability) smoothing(w *Word) {
na := w.getNAnalysis()
if na == 1 {
TRACE(2, "Inambiguous form, set prob to 1", MOD_PROBABILITY)
w.Front().Value.(*Analysis).setProb(1)
return
}
TRACE(2, "Form with analysis. Smoothing probabilities", MOD_PROBABILITY)
var tagShorts = make(map[string]float64)
for li := w.Front(); li != nil; li = li.Next() {
tagShorts[this.Tags.GetShortTag(li.Value.(*Analysis).getTag())]++
}
var tmpMap map[string]float64
sum := 0.0
form := w.getLCForm()
it := this.lexicalTags[form]
var usingBackoff bool
if it != nil {
TRACE(2, "Form "+form+" lexical probabilities found", MOD_PROBABILITY)
tmpMap = it
usingBackoff = false
} else {
TRACE(2, "Form "+form+" lexical probabilities not found", MOD_PROBABILITY)
usingBackoff = true
c := ""
cNP := ""
for k, _ := range tagShorts {
cNP += "-" + k
if k != "NP" {
c += "-" + k
}
}
cNP = cNP[1:]
if c != "" {
c = c[1:]
}
TRACE(3, "Ambiguity class: ["+cNP+"]. Secondary class: ["+c+"]", MOD_PROBABILITY)
it = this.classTags[cNP]
if it != nil {
TRACE(2, "Ambiguity class "+cNP+" probabilities found.", MOD_PROBABILITY)
tmpMap = it
} else if c != "" && c != cNP {
it = this.classTags[c]
if it != nil {
TRACE(2, "Secondary ambiguity class '"+c+"' probabilities found", MOD_PROBABILITY)
tmpMap = it
}
}
if tmpMap == nil {
tmpMap = this.singleTags
}
}
for k, v := range tagShorts {
p := tmpMap[k]
sum += p * v
}
lambda := If(usingBackoff, this.LidstoneLambdaClass, this.LidstoneLambdaLexical).(float64)
var norm float64 = sum + lambda*float64(na)
for li := w.Front(); li != nil; li = li.Next() {
p := tmpMap[this.Tags.GetShortTag(li.Value.(*Analysis).getTag())]
li.Value.(*Analysis).setProb((p + lambda) / norm)
}
if usingBackoff {
TRACE(2, "Using suffixes to smooth probs", MOD_PROBABILITY)
norm := 0.0
p := make([]float64, w.Len())
i := 0
for li := w.Front(); li != nil; li = li.Next() {
p[i] = this.computeProbability(li.Value.(*Analysis).getTag(), li.Value.(*Analysis).getProb(), w.getForm())
norm += p[i]
i++
}
i = 0
for li := w.Front(); li != nil; li = li.Next() {
li.Value.(*Analysis).setProb((1-this.BiassSuffixes)*li.Value.(*Analysis).getProb() + this.BiassSuffixes*p[i]/norm)
i++
}
p = nil
}
}
func (this *Probability) computeProbability(tag string, prob float64, s string) float64 {
x := prob
spos := len(s)
found := true
var pt float64
TRACE(4, " suffixes. Tag "+tag+" initial prob="+strconv.FormatFloat(prob, 'f', -1, 64), MOD_PROBABILITY)
for spos > 0 && found {
spos--
is := this.unkSuffS[s[spos:]]
found = is != nil
if found {
pt = is[tag]
if pt != 0 {
TRACE(4, " found prob for suffix -"+s[spos:], MOD_PROBABILITY)
} else {
pt = 0
TRACE(4, " NO prob found for suffix -"+s[spos:], MOD_PROBABILITY)
}
x = (pt + this.theeta*x) / (1 + this.theeta)
}
}
TRACE(4, " final prob="+strconv.FormatFloat(x, 'f', -1, 64), MOD_PROBABILITY)
return x
}
func (this *Probability) guesser(w *Word, mass float64) float64 {
form := w.getLCForm()
sum := If(w.getNAnalysis() > 0, mass, 0.0).(float64)
sum2 := 0.0
TRACE(2, "Initial sum="+strconv.FormatFloat(sum, 'f', 3, 64), MOD_PROBABILITY)
stags := set.New()
for li := w.Front(); li != nil; li = li.Next() {
stags.Add(this.Tags.GetShortTag(li.Value.(*Analysis).getTag()))
}
la := list.New()
for k, v := range this.unkTags {
TRACE(2, " guesser checking tag "+k, MOD_PROBABILITY)
hasit := stags.Has(this.Tags.GetShortTag(k))
if !hasit {
p := this.computeProbability(k, v, form)
a := NewAnalysis(form, k)
a.setProb(p)
if p >= this.ProbabilityThreshold {
sum += p
w.addAnalysis(a)
TRACE(2, " added. sum is:"+strconv.FormatFloat(sum, 'f', 3, 64), MOD_PROBABILITY)
} else {
sum2 += p
la.PushBack(a)
}
}
}
if w.getNAnalysis() == 0 {
w.setAnalysis(List2Array(la)...)
sum = sum2
}
return sum
}
func (this *Probability) setActivateGuesser(b bool) {
this.activateGuesser = b
}