This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
/
ranker.go
executable file
·261 lines (217 loc) · 6.18 KB
/
ranker.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
// Copyright 2013 Hui Chen
// Copyright 2016 ego authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package core
import (
"log"
"sort"
"sync"
"github.com/go-ego/riot/types"
"github.com/go-ego/riot/utils"
)
// Ranker ranker
type Ranker struct {
lock struct {
sync.RWMutex
fields map[string]interface{}
docs map[string]bool
// new
content map[string]string
attri map[string]interface{}
}
idOnly bool
initialized bool
}
// Init init ranker
func (ranker *Ranker) Init(onlyID ...bool) {
if ranker.initialized == true {
log.Fatal("The Ranker can not be initialized twice.")
}
ranker.initialized = true
if len(onlyID) > 0 {
ranker.idOnly = onlyID[0]
}
ranker.lock.fields = make(map[string]interface{})
ranker.lock.docs = make(map[string]bool)
if !ranker.idOnly {
// new
ranker.lock.content = make(map[string]string)
ranker.lock.attri = make(map[string]interface{})
}
}
// AddDoc add doc
// 给某个文档添加评分字段
func (ranker *Ranker) AddDoc(
// docId uint64, fields interface{}, content string, attri interface{}) {
docId string, fields interface{}, content ...interface{}) {
if ranker.initialized == false {
log.Fatal("The Ranker has not been initialized.")
}
ranker.lock.Lock()
ranker.lock.fields[docId] = fields
ranker.lock.docs[docId] = true
if !ranker.idOnly {
// new
if len(content) > 0 {
ranker.lock.content[docId] = content[0].(string)
}
if len(content) > 1 {
ranker.lock.attri[docId] = content[1]
// ranker.lock.attri[docId] = attri
}
}
ranker.lock.Unlock()
}
// RemoveDoc 删除某个文档的评分字段
func (ranker *Ranker) RemoveDoc(docId string) {
if ranker.initialized == false {
log.Fatal("The Ranker has not been initialized.")
}
ranker.lock.Lock()
delete(ranker.lock.fields, docId)
delete(ranker.lock.docs, docId)
if !ranker.idOnly {
// new
delete(ranker.lock.content, docId)
delete(ranker.lock.attri, docId)
}
ranker.lock.Unlock()
}
func maxOutput(options types.RankOpts, docsLen int) (int, int) {
var start, end int
if options.MaxOutputs != 0 {
start = utils.MinInt(options.OutputOffset, docsLen)
end = utils.MinInt(options.OutputOffset+options.MaxOutputs, docsLen)
return start, end
}
start = utils.MinInt(options.OutputOffset, docsLen)
end = docsLen
return start, end
}
func (ranker *Ranker) rankOutIDs(docs []types.IndexedDoc, options types.RankOpts,
countDocsOnly bool) (outputDocs types.ScoredIDs, numDocs int) {
for _, d := range docs {
ranker.lock.RLock()
// 判断 doc 是否存在
if _, ok := ranker.lock.docs[d.DocId]; ok {
fs := ranker.lock.fields[d.DocId]
ranker.lock.RUnlock()
// 计算评分并剔除没有分值的文档
scores := options.ScoringCriteria.Score(d, fs)
if len(scores) > 0 {
if !countDocsOnly {
outputDocs = append(outputDocs,
types.ScoredID{
DocId: d.DocId,
Scores: scores,
TokenSnippetLocs: d.TokenSnippetLocs,
TokenLocs: d.TokenLocs,
})
}
numDocs++
}
} else {
ranker.lock.RUnlock()
}
}
return
}
// RankDocID rank docs by types.ScoredIDs
func (ranker *Ranker) RankDocID(docs []types.IndexedDoc,
options types.RankOpts, countDocsOnly bool) (types.ScoredIDs, int) {
outputDocs, numDocs := ranker.rankOutIDs(docs, options, countDocsOnly)
// 排序
if !countDocsOnly {
if options.ReverseOrder {
sort.Sort(sort.Reverse(outputDocs))
} else {
sort.Sort(outputDocs)
}
// 当用户要求只返回部分结果时返回部分结果
docsLen := len(outputDocs)
start, end := maxOutput(options, docsLen)
return outputDocs[start:end], numDocs
}
return outputDocs, numDocs
}
func (ranker *Ranker) rankOutDocs(docs []types.IndexedDoc, options types.RankOpts,
countDocsOnly bool) (outputDocs types.ScoredDocs, numDocs int) {
for _, d := range docs {
ranker.lock.RLock()
// 判断 doc 是否存在
if _, ok := ranker.lock.docs[d.DocId]; ok {
fs := ranker.lock.fields[d.DocId]
content := ranker.lock.content[d.DocId]
attri := ranker.lock.attri[d.DocId]
ranker.lock.RUnlock()
// 计算评分并剔除没有分值的文档
scores := options.ScoringCriteria.Score(d, fs)
if len(scores) > 0 {
if !countDocsOnly {
scoredID := types.ScoredID{
DocId: d.DocId,
Scores: scores,
TokenSnippetLocs: d.TokenSnippetLocs,
TokenLocs: d.TokenLocs,
}
outputDocs = append(outputDocs,
types.ScoredDoc{
ScoredID: scoredID,
// new
Fields: fs,
Content: content,
Attri: attri,
})
}
numDocs++
}
} else {
ranker.lock.RUnlock()
}
}
return
}
// RankDocs rank docs by types.ScoredDocs
func (ranker *Ranker) RankDocs(docs []types.IndexedDoc,
options types.RankOpts, countDocsOnly bool) (types.ScoredDocs, int) {
outputDocs, numDocs := ranker.rankOutDocs(docs, options, countDocsOnly)
// 排序
if !countDocsOnly {
if options.ReverseOrder {
sort.Sort(sort.Reverse(outputDocs))
} else {
sort.Sort(outputDocs)
}
// 当用户要求只返回部分结果时返回部分结果
docsLen := len(outputDocs)
start, end := maxOutput(options, docsLen)
return outputDocs[start:end], numDocs
}
return outputDocs, numDocs
}
// Rank rank docs
// 给文档评分并排序
func (ranker *Ranker) Rank(docs []types.IndexedDoc,
options types.RankOpts, countDocsOnly bool) (interface{}, int) {
if ranker.initialized == false {
log.Fatal("The Ranker has not been initialized.")
}
// 对每个文档评分
if ranker.idOnly {
outputDocs, numDocs := ranker.RankDocID(docs, options, countDocsOnly)
return outputDocs, numDocs
}
outputDocs, numDocs := ranker.RankDocs(docs, options, countDocsOnly)
return outputDocs, numDocs
}