/
score.go
138 lines (124 loc) · 3.28 KB
/
score.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
package score
import (
"fmt"
"sort"
"github.com/gnames/gnames/ent/verifier"
vlib "github.com/gnames/gnlib/ent/verifier"
)
// NewScore returns an implementation of the Score interface.
func NewScore() Score {
return score{}
}
// String returns a string representation of a score as a set of bits with
// every byte (8 bits) separated by an underscore.
func (s score) String() string {
str := fmt.Sprintf("%032b", s.value)
res := make([]byte, 35)
offset := 0
for i, v := range []byte(str) {
res[i+offset] = v
if (i+1)%8 == 0 && (i+1)%32 != 0 {
offset++
res[i+offset] = '_'
}
}
return string(res)
}
// SortResults goes through vlib.ResultData aggregated by a match and
// assigns each of them a score accoring to the scoring algorithms.
func (s score) SortResults(mr *verifier.MatchRecord) {
for _, rd := range mr.MatchResults {
s = s.rank(mr.CanonicalFull, rd.MatchedCanonicalFull,
mr.Cardinality, rd.MatchedCardinality).
fuzzy(rd.EditDistance).
curation(rd.DataSourceID, rd.Curation).
auth(mr.Authors, rd.MatchedAuthors, mr.Year, rd.MatchedYear).
accepted(rd.RecordID, rd.CurrentRecordID).
parsingQuality(rd.ParsingQuality)
rd.Score = s.value
s.value = 0
}
// Sort (in reverse) according to the score. First element has
// the highest score, the last has the lowest.
mrs := mr.MatchResults
sort.SliceStable(mrs, func(i, j int) bool {
return mrs[i].Score > mrs[j].Score
})
mr.Sorted = true
}
// BestResult returns the highest runked vlib.ResultData according to
// scoring algorithm.
func (s score) BestResult(mr *verifier.MatchRecord) *vlib.ResultData {
if mr.MatchResults == nil {
return nil
}
if !mr.Sorted {
s.SortResults(mr)
}
return mr.MatchResults[0]
}
// Results returns the best scoring vlib.ResultData for each of
// the preffered data-source. From 0 to 1 results per data-source are allowed.
func (s score) Results(
sources []int,
mr *verifier.MatchRecord,
allMatches bool,
) []*vlib.ResultData {
if mr.MatchResults == nil || len(mr.MatchResults) == 0 {
return nil
}
allSources := len(sources) == 1 && sources[0] == 0
if !mr.Sorted {
s.SortResults(mr)
}
if allSources {
return getAllSources(mr, allMatches)
}
return getPrefSources(sources, mr, allMatches)
}
func getPrefSources(
sources []int,
mr *verifier.MatchRecord,
allMatches bool,
) []*vlib.ResultData {
// maps a data-source ID to corresponding result data.
sourceMap := make(map[int][]*vlib.ResultData)
for _, v := range sources {
sourceMap[v] = nil
}
var resLen int
for _, v := range mr.MatchResults {
dsID := v.DataSourceID
if data, ok := sourceMap[dsID]; ok && (allMatches || data == nil) {
resLen++
sourceMap[dsID] = append(sourceMap[dsID], v)
}
}
res := make([]*vlib.ResultData, 0, resLen)
for _, v := range sources {
if data := sourceMap[v]; data != nil {
res = append(res, data...)
}
}
return res
}
func getAllSources(
mr *verifier.MatchRecord,
allMatches bool,
) []*vlib.ResultData {
if allMatches {
return mr.MatchResults
}
res := make([]*vlib.ResultData, 0, len(mr.MatchResults))
sourceMap := make(map[int]struct{})
for i := range mr.MatchResults {
dsID := mr.MatchResults[i].DataSourceID
if _, ok := sourceMap[dsID]; ok {
continue
} else {
sourceMap[dsID] = struct{}{}
res = append(res, mr.MatchResults[i])
}
}
return res
}