/
bayes.go
172 lines (153 loc) · 4.31 KB
/
bayes.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
package nlp
import (
"fmt"
"io/ioutil"
"log"
"github.com/gnames/bayes"
"github.com/gnames/gnfinder/dict"
"github.com/gnames/gnfinder/fs"
"github.com/gnames/gnfinder/lang"
"github.com/gnames/gnfinder/token"
)
func TagTokens(ts []token.Token, d *dict.Dictionary, nb *bayes.NaiveBayes,
thr float64) {
for i := range ts {
t := &ts[i]
if !t.Features.Capitalized || t.UninomialDict == dict.BlackUninomial {
continue
}
t.SetUninomialDict(d)
ts2 := ts[i:token.UpperIndex(i, len(ts))]
fs := NewFeatureSet(ts2)
priorOdds := nameFrequency()
odds := predictOdds(nb, t, &fs, priorOdds)
processBayesResults(odds, ts, i, thr, d)
}
}
func processBayesResults(odds []bayes.Posterior, ts []token.Token, i int,
oddsThreshold float64, d *dict.Dictionary) {
uni := &ts[i]
decideUninomial(odds, uni, oddsThreshold)
if uni.Indices.Species == 0 || (odds[1].MaxLabel != Name &&
uni.Decision.In(token.NotName, token.Uninomial)) {
return
}
sp := &ts[i+uni.Indices.Species]
decideSpeces(odds, uni, sp, oddsThreshold, d)
if uni.Indices.Infraspecies == 0 || (odds[2].MaxLabel != Name &&
!uni.Decision.In(token.Trinomial, token.BayesTrinomial)) {
return
}
isp := &ts[i+uni.Indices.Infraspecies]
decideInfraspeces(odds, uni, isp, oddsThreshold, d)
}
func decideInfraspeces(odds []bayes.Posterior, uni *token.Token,
isp *token.Token, oddsThreshold float64, d *dict.Dictionary) {
isp.SetSpeciesDict(d)
if isp.SpeciesDict == dict.BlackSpecies {
return
}
isp.Odds = odds[2].MaxOdds
isp.OddsDetails = token.NewOddsDetails(odds[2].Likelihoods)
if isp.Odds >= oddsThreshold && uni.Decision.In(token.NotName,
token.PossibleBinomial, token.Binomial, token.BayesBinomial) {
uni.Decision = token.BayesTrinomial
}
}
func decideSpeces(odds []bayes.Posterior, uni *token.Token, sp *token.Token,
oddsThreshold float64, d *dict.Dictionary) {
sp.SetSpeciesDict(d)
if sp.SpeciesDict == dict.BlackSpecies {
return
}
sp.Odds = odds[1].MaxOdds
sp.OddsDetails = token.NewOddsDetails(odds[1].Likelihoods)
if sp.Odds >= oddsThreshold && uni.Odds > 1 &&
uni.Decision.In(token.NotName, token.Uninomial, token.PossibleBinomial) {
uni.Decision = token.BayesBinomial
}
}
func decideUninomial(odds []bayes.Posterior, uni *token.Token,
oddsThreshold float64) {
if odds[0].MaxLabel == Name {
uni.Odds = odds[0].MaxOdds
} else {
uni.Odds = 1 / odds[0].MaxOdds
}
uni.OddsDetails = token.NewOddsDetails(odds[0].Likelihoods)
uni.LabelFreq = odds[0].LabelFreq
if odds[0].MaxLabel == Name &&
odds[0].MaxOdds >= oddsThreshold &&
uni.Decision == token.NotName &&
uni.UninomialDict != dict.BlackUninomial &&
!uni.Abbr {
uni.Decision = token.BayesUninomial
}
}
func predictOdds(nb *bayes.NaiveBayes, t *token.Token, fs *FeatureSet,
odds bayes.LabelFreq) []bayes.Posterior {
evenOdds := map[bayes.Labeler]float64{Name: 1.0, NotName: 1.0}
oddsUni, err := nb.Predict(features(fs.Uninomial), bayes.WithPriorOdds(odds))
if err != nil {
log.Fatal(err)
}
if t.Indices.Species == 0 {
return []bayes.Posterior{oddsUni}
}
oddsSp, err := nb.Predict(features(fs.Species), bayes.WithPriorOdds(evenOdds))
if err != nil {
log.Fatal(err)
}
delete(oddsSp.Likelihoods[Name], "PriorOdds")
if t.Indices.Infraspecies == 0 {
return []bayes.Posterior{oddsUni, oddsSp}
}
f := features(fs.InfraSp)
oddsInfraSp, err := nb.Predict(f, bayes.WithPriorOdds(evenOdds))
if err != nil {
log.Fatal(err)
}
delete(oddsInfraSp.Likelihoods[Name], "PriorOdds")
return []bayes.Posterior{oddsUni, oddsSp, oddsInfraSp}
}
func nameFrequency() bayes.LabelFreq {
return map[bayes.Labeler]float64{
Name: 1.0,
NotName: 10.0,
}
}
func BayesWeights() map[lang.Language]*bayes.NaiveBayes {
bw := make(map[lang.Language]*bayes.NaiveBayes)
for k := range lang.LanguagesSet() {
bw[k] = naiveBayesFromDump(k)
}
return bw
}
func naiveBayesFromDump(l lang.Language) *bayes.NaiveBayes {
nb := bayes.NewNaiveBayes()
bayes.RegisterLabel(labelMap)
dir := fmt.Sprintf("/nlp/%s/bayes.json", l.String())
f, err := fs.Files.Open(dir)
if err != nil {
log.Fatal(err)
}
defer func() {
err := f.Close()
if err != nil {
log.Fatal(err)
}
}()
json, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
nb.Restore(json)
return nb
}
func features(bf []BayesF) []bayes.Featurer {
f := make([]bayes.Featurer, len(bf))
for i, v := range bf {
f[i] = v
}
return f
}