/
features.go
135 lines (121 loc) · 3.14 KB
/
features.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
package nlp
import (
"strconv"
"github.com/gnames/bayes/ent/feature"
"github.com/gnames/gnfinder/ent/token"
"github.com/gnames/gnfinder/io/dict"
)
// BayesF implements bayes.Featurer
type BayesF struct {
Name string
Value string
}
// FeatureSet splits features into Uninomial, Species, Ifraspecies groups
type FeatureSet struct {
Uninomial []BayesF
Species []BayesF
InfraSp []BayesF
}
func (fs *FeatureSet) Flatten() []feature.Feature {
l := len(fs.Uninomial) + len(fs.Species) + len(fs.InfraSp)
res := make([]feature.Feature, 0, l)
res = append(res, features(fs.Uninomial)...)
res = append(res, features(fs.Species)...)
res = append(res, features(fs.InfraSp)...)
return res
}
// BayesFeatures creates slices of features for a token that might represent
// genus or other uninomial
func NewFeatureSet(ts []token.TokenSN) FeatureSet {
var fs FeatureSet
var u, sp, isp, rank token.TokenSN
u = ts[0]
if !u.Features().IsCapitalized {
return fs
}
if i := u.Indices().Species; i > 0 {
sp = ts[i]
}
if i := u.Indices().Infraspecies; i > 0 {
isp = ts[i]
}
if i := u.Indices().Rank; i > 0 {
rank = ts[i]
}
fs.convertFeatures(u, sp, isp, rank)
return fs
}
func (fs *FeatureSet) convertFeatures(
uni token.TokenSN,
sp token.TokenSN,
isp token.TokenSN,
rank token.TokenSN,
) {
var uniDict, spDict, ispDict string
if !uni.Features().Abbr {
uniDict = uni.Features().UninomialDict.String()
fs.Uninomial = append(fs.Uninomial,
BayesF{"uniLen", strconv.Itoa(len(uni.Cleaned()))},
BayesF{"abbr", "false"},
)
} else {
fs.Uninomial = append(fs.Uninomial, BayesF{"abbr", "true"})
}
if w3 := wordEnd(uni); !uni.Features().Abbr && w3 != "" {
fs.Uninomial = append(fs.Uninomial, BayesF{"uniEnd3", w3})
}
if uni.Indices().Species > 0 {
spDict = sp.Features().SpeciesDict.String()
fs.Species = append(fs.Species,
BayesF{"spLen", strconv.Itoa(len(sp.Cleaned()))},
)
if uni.Features().GenSpInAmbigDict > 0 {
uniDict = dict.InAmbigGenusSp.String()
spDict = dict.InAmbigGenusSp.String()
}
if sp.Features().HasDash {
fs.Species = append(fs.Species, BayesF{"hasDash", "true"})
}
if w3 := wordEnd(sp); w3 != "" {
fs.Species = append(fs.Species, BayesF{"spEnd3", w3})
}
}
if uni.Indices().Rank > 0 {
fs.InfraSp = []BayesF{
{"ispRank", "true"},
}
}
if uni.Indices().Infraspecies > 0 {
ispDict = isp.Features().SpeciesDict.String()
fs.InfraSp = append(fs.InfraSp,
BayesF{"ispLen", strconv.Itoa(len(isp.Cleaned()))},
)
if uni.Features().GenSpInAmbigDict > 1 {
ispDict = dict.InAmbigGenusSp.String()
}
if isp.Features().HasDash {
fs.InfraSp = append(fs.InfraSp, BayesF{"hasDash", "true"})
}
if w3 := wordEnd(isp); w3 != "" {
fs.InfraSp = append(fs.InfraSp, BayesF{"ispEnd3", w3})
}
}
if uniDict != "" {
fs.Uninomial = append(fs.Uninomial, BayesF{"uniDict", uniDict})
}
if spDict != "" {
fs.Species = append(fs.Species, BayesF{"spDict", spDict})
}
if ispDict != "" {
fs.InfraSp = append(fs.InfraSp, BayesF{"ispDict", ispDict})
}
}
func wordEnd(t token.TokenSN) string {
name := []rune(t.Cleaned())
l := len(name)
if l < 4 {
return ""
}
w3 := string(name[l-3 : l])
return w3
}