/
gnames.go
215 lines (193 loc) · 4.97 KB
/
gnames.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
package gnames
import (
"context"
"sort"
"unicode"
"github.com/gnames/gnames/config"
"github.com/gnames/gnames/ent/facet"
"github.com/gnames/gnames/ent/score"
"github.com/gnames/gnames/ent/verifier"
"github.com/gnames/gnames/io/matcher"
gnctx "github.com/gnames/gnlib/ent/context"
"github.com/gnames/gnlib/ent/gnvers"
mlib "github.com/gnames/gnlib/ent/matcher"
vlib "github.com/gnames/gnlib/ent/verifier"
"github.com/gnames/gnmatcher"
"github.com/gnames/gnparser/ent/str"
"github.com/gnames/gnquery/ent/search"
"github.com/gnames/gnuuid"
log "github.com/sirupsen/logrus"
)
type gnames struct {
cfg config.Config
vf verifier.Verifier
facet facet.Facet
matcher gnmatcher.GNmatcher
}
// NewGNames is a constructor that returns implmentation of GNames interface.
func NewGNames(
cnf config.Config,
vf verifier.Verifier,
fc facet.Facet,
) GNames {
return gnames{
cfg: cnf,
vf: vf,
facet: fc,
matcher: matcher.NewGNmatcher(cnf.MatcherURL),
}
}
func (g gnames) GetVersion() gnvers.Version {
return gnvers.Version{
Version: Version,
Build: Build,
}
}
func (g gnames) Verify(
ctx context.Context,
input vlib.Input,
) (vlib.Output, error) {
log.Printf("Verifying %d name-strings.", len(input.NameStrings))
namesRes := make([]vlib.Name, len(input.NameStrings))
var matches []mlib.Match
if input.WithCapitalization {
names := make([]string, len(input.NameStrings))
for i := range input.NameStrings {
names[i] = str.CapitalizeName(input.NameStrings[i])
}
matches = g.matcher.MatchNames(names)
} else {
matches = g.matcher.MatchNames(input.NameStrings)
}
var errString string
matchRecords, err := g.vf.MatchRecords(ctx, matches)
if err != nil {
errString = err.Error()
}
for i, v := range matches {
if mr, ok := matchRecords[v.ID]; ok {
s := score.New()
s.SortResults(mr)
item := vlib.Name{
ID: mr.ID,
Name: mr.Name,
MatchType: mr.MatchType,
Curation: mr.Curation,
DataSourcesNum: mr.DataSourcesNum,
BestResult: s.BestResult(mr),
Results: s.Results(input.DataSources, mr, input.WithAllMatches),
Error: errString,
}
if input.WithCapitalization {
item.Name = input.NameStrings[i]
item.ID = gnuuid.New(item.Name).String()
}
namesRes[i] = item
} else {
log.Warnf("Cannot find record for '%s'.", v.Name)
}
}
res := vlib.Output{Meta: meta(input, namesRes), Names: namesRes}
return res, nil
}
func (g gnames) Search(
ctx context.Context,
inp search.Input,
) search.Output {
inp.Query = inp.ToQuery()
log.Printf("Searching '%s'.", inp.Query)
res := search.Output{Meta: search.Meta{Input: inp}}
matchRecords, err := g.facet.Search(ctx, inp)
if err != nil {
res.Error = err.Error()
}
res.NamesNumber = len(matchRecords)
sortedNames := sortNames(matchRecords)
resNames := make([]vlib.Name, len(matchRecords))
var dss []int
var all bool
if inp.WithAllResults {
dss = []int{0}
all = true
}
for i, v := range sortedNames {
mr := matchRecords[v]
s := score.New()
s.SortResults(mr)
item := vlib.Name{
ID: mr.ID,
Name: mr.Name,
MatchType: mr.MatchType,
BestResult: s.BestResult(mr),
Results: s.Results(dss, mr, all),
}
item.Curation = item.BestResult.Curation
resNames[i] = item
}
res.Names = resNames
return res
}
func meta(input vlib.Input, names []vlib.Name) vlib.Meta {
allSources := len(input.DataSources) == 1 && input.DataSources[0] == 0
hs := make([]gnctx.Hierarch, len(names))
for i := range names {
hs[i] = names[i]
}
var c gnctx.Context
var ks []vlib.Kingdom
if input.WithContext {
c = gnctx.New(hs, input.ContextThreshold)
ks = make([]vlib.Kingdom, len(c.Kingdoms))
for i, v := range c.Kingdoms {
ks[i] = vlib.Kingdom{
KingdomName: v.Name,
NamesNumber: v.NamesNum,
Percentage: v.Percentage,
}
}
}
res := vlib.Meta{
NamesNumber: len(input.NameStrings),
WithAllSources: allSources,
WithAllMatches: input.WithAllMatches,
WithContext: input.WithContext,
WithCapitalization: input.WithCapitalization,
ContextThreshold: input.ContextThreshold,
DataSources: input.DataSources,
Context: c.Context.Name,
ContextPercentage: c.ContextPercentage,
Kingdom: c.Kingdom.Name,
KingdomPercentage: c.KingdomPercentage,
Kingdoms: ks,
}
return res
}
func (g gnames) DataSources(ids ...int) ([]*vlib.DataSource, error) {
return g.vf.DataSources(ids...)
}
func FirstUpperCase(name string) string {
runes := []rune(name)
if len(runes) < 2 {
return name
}
one := runes[0]
two := runes[1]
if unicode.IsUpper(one) || !unicode.IsLetter(one) {
return name
}
if one == 'x' && (two == ' ' || unicode.IsUpper(two)) {
return name
}
runes[0] = unicode.ToUpper(one)
return string(runes)
}
func sortNames(mrs map[string]*verifier.MatchRecord) []string {
res := make([]string, len(mrs))
var count int
for k := range mrs {
res[count] = k
count++
}
sort.Strings(res)
return res
}