-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryutils.go
85 lines (71 loc) · 2.33 KB
/
queryutils.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
// This file is part of GoIRS.
//
// GoIRS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GoIRS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GoIRS. If not, see <http://www.gnu.org/licenses/>.
package goirs
import "sort"
//Query representa una consulta en forma de "vector"
type Query map[int]float64
//QueryResult es el resultado de realizar una consulta
type QueryResult map[int]float64
//DocumentWeight representa el valor de similitud de un documento con una consulta
type DocumentWeight struct {
DocID int
Weight float64
}
//DocumentWeights representa una lista de similitudes
type DocumentWeights []DocumentWeight
//GetQuerySimilarities calcula el valor de similitud para todos los documentos
//indexados en ind con respecto a la consulta q
func GetQuerySimilarities(q Query, ind *FrequencyIndex) QueryResult {
res := make(QueryResult)
for token, wq := range q {
for doc, wd := range ind.Weight[token] {
//fmt.Println("Documento", ind.DocNames[doc], "término", ind.TokenNames[token], "peso", wd)
res[doc] += wq * wd
}
}
return res
}
func (d DocumentWeights) Len() int {
return len(d)
}
func (d DocumentWeights) Less(i, j int) bool {
return d[i].Weight < d[j].Weight
}
func (d DocumentWeights) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
//GetNGreatest elige los N mejores resultados de una consulta.
//Utiliza el sort de todos los valores (ineficiente)
func (qr QueryResult) GetNGreatest() DocumentWeights {
res := make(DocumentWeights, len(qr))
i := 0
for doc, weight := range qr {
res[i] = DocumentWeight{doc, weight}
i++
}
sort.Sort(sort.Reverse(res))
return res
}
//ToQuery transforma un iterador de cadenas en el formato requerido para una consulta
func (tokens StringIterator) ToQuery(ind *FrequencyIndex) Query {
q := make(Query)
for token := range tokens {
id := ind.TokenIds[token]
if id != 0 {
q[id] = 1
}
}
return q
}