-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtfidf_example_test.go
266 lines (231 loc) · 6.54 KB
/
tfidf_example_test.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
262
263
264
265
266
package tfidf_test
import (
"fmt"
"math"
"sort"
"strings"
. "github.com/go-nlp/tfidf"
"github.com/xtgo/set"
"gorgonia.org/tensor"
)
var mobydick = []string{
"Call me Ishmael .",
"Some years ago -- never mind how long precisely -- having little or no money in my purse , and nothing particular to interest me on shore , I thought I would sail about a little and see the watery part of the world .",
"It is a way I have of driving off the spleen and regulating the circulation .",
"Whenever I find myself growing grim about the mouth ; ",
"whenever it is a damp , drizzly November in my soul ; ",
"whenever I find myself involuntarily pausing before coffin warehouses , and bringing up the rear of every funeral I meet ; ",
"and especially whenever my hypos get such an upper hand of me , that it requires a strong moral principle to prevent me from deliberately stepping into the street , and methodically knocking people's hats off -- then , I account it high time to get to sea as soon as I can .",
"This is my substitute for pistol and ball . ",
"With a philosophical flourish Cato throws himself upon his sword ; ",
"I quietly take to the ship . There is nothing surprising in this .",
"If they but knew it , almost all men in their degree , some time or other , cherish very nearly the same feelings towards the ocean with me .",
}
type doc []int
func (d doc) IDs() []int { return []int(d) }
func makeCorpus(a []string) (map[string]int, []string) {
retVal := make(map[string]int)
invRetVal := make([]string, 0)
var id int
for _, s := range a {
for _, f := range strings.Fields(s) {
f = strings.ToLower(f)
if _, ok := retVal[f]; !ok {
retVal[f] = id
invRetVal = append(invRetVal, f)
id++
}
}
}
return retVal, invRetVal
}
func makeDocuments(a []string, c map[string]int) []Document {
retVal := make([]Document, 0, len(a))
for _, s := range a {
var ts []int
for _, f := range strings.Fields(s) {
f = strings.ToLower(f)
id := c[f]
ts = append(ts, id)
}
retVal = append(retVal, doc(ts))
}
return retVal
}
type docScore struct {
id int
score float64
}
type docScores []docScore
func (ds docScores) Len() int { return len(ds) }
func (ds docScores) Less(i, j int) bool { return ds[i].score < ds[j].score }
func (ds docScores) Swap(i, j int) {
ds[i].score, ds[j].score = ds[j].score, ds[i].score
ds[i].id, ds[j].id = ds[j].id, ds[i].id
}
func cosineSimilarity(queryScore []float64, docIDs []int, relVec []float64) docScores {
// special case
if len(docIDs) == 1 {
// even more special case!
if len(queryScore) == 1 {
return docScores{
{docIDs[0], queryScore[0] * relVec[0]},
}
}
q := tensor.New(tensor.WithBacking(queryScore))
m := tensor.New(tensor.WithBacking(relVec))
score, err := q.Inner(m)
if err != nil {
panic(err)
}
return docScores{
{docIDs[0], score.(float64)},
}
}
m := tensor.New(tensor.WithShape(len(docIDs), len(queryScore)), tensor.WithBacking(relVec))
q := tensor.New(tensor.WithShape(len(queryScore)), tensor.WithBacking(queryScore))
dp, err := m.MatVecMul(q)
if err != nil {
panic(err)
}
m2, err := tensor.Square(m)
if err != nil {
panic(err)
}
normDocs, err := tensor.Sum(m2, 1)
if err != nil {
panic(err)
}
normDocs, err = tensor.Sqrt(normDocs)
if err != nil {
panic(err)
}
q2, err := tensor.Square(q)
if err != nil {
panic(err)
}
normQt, err := tensor.Sum(q2)
if err != nil {
panic(err)
}
normQ := normQt.Data().(float64)
normQ = math.Sqrt(normQ)
norms, err := tensor.Mul(normDocs, normQ)
if err != nil {
panic(err)
}
cosineSim, err := tensor.Div(dp, norms)
if err != nil {
panic(err)
}
csData := cosineSim.Data().([]float64)
var ds docScores
for i, id := range docIDs {
score := csData[i]
ds = append(ds, docScore{id: id, score: score})
}
return ds
}
func contains(query Document, in []Document, tf *TFIDF) (docIDs []int, relVec []float64) {
q := query.IDs()
q = set.Ints(q) // unique words only
for i := range in {
doc := in[i].IDs()
var count int
var relevant []float64
for _, wq := range q {
inner:
for _, wd := range doc {
if wq == wd {
count++
break inner
}
}
}
if count == len(q) {
// calculate the score of the doc
score := tf.Score(in[i])
// get the scores of the relevant words
for _, wq := range q {
inner2:
for j, wd := range doc {
if wd == wq {
relevant = append(relevant, score[j])
break inner2
}
}
}
docIDs = append(docIDs, i)
relVec = append(relVec, relevant...)
}
}
return
}
func Example() {
corpus, invCorpus := makeCorpus(mobydick)
docs := makeDocuments(mobydick, corpus)
tf := New()
for _, doc := range docs {
tf.Add(doc)
}
tf.CalculateIDF()
fmt.Println("IDF:")
for i, w := range invCorpus {
fmt.Printf("\t%q: %1.1f\n", w, tf.IDF[i])
if i >= 10 {
break
}
}
// now we search
// "ishmael" is a query
ishmael := doc{corpus["ishmael"]}
// "whenever i find" is another query
whenever := doc{corpus["whenever"], corpus["i"], corpus["find"]}
// step1: score the queries
ishmaelScore := tf.Score(ishmael)
wheneverScore := tf.Score(whenever)
// step2: find the docs that contains the queries.
// if there are no docs, oops.
ishmaelDocs, ishmaelRelVec := contains(ishmael, docs, tf)
wheneverDocs, wheneverRelVec := contains(whenever, docs, tf)
// step3: calculate the cosine similarity
ishmaelRes := cosineSimilarity(ishmaelScore, ishmaelDocs, ishmaelRelVec)
wheneverRes := cosineSimilarity(wheneverScore, wheneverDocs, wheneverRelVec)
// step4: sort the results
sort.Sort(sort.Reverse(ishmaelRes))
sort.Sort(sort.Reverse(wheneverRes))
fmt.Printf("Relevant Docs to \"Ishmael\":\n")
for _, d := range ishmaelRes {
fmt.Printf("\tID : %d\n\tScore: %1.3f\n\tDoc : %q\n", d.id, d.score, mobydick[d.id])
}
fmt.Println("")
fmt.Printf("Relevant Docs to \"whenever i find\":\n")
for _, d := range wheneverRes {
fmt.Printf("\tID : %d\n\tScore: %1.3f\n\tDoc : %q\n", d.id, d.score, mobydick[d.id])
}
// Output:
// IDF:
// "call": 2.4
// "me": 1.0
// "ishmael": 2.4
// ".": 0.5
// "some": 1.7
// "years": 2.4
// "ago": 2.4
// "--": 1.7
// "never": 2.4
// "mind": 2.4
// "how": 2.4
// Relevant Docs to "Ishmael":
// ID : 0
// Score: 1.437
// Doc : "Call me Ishmael ."
//
// Relevant Docs to "whenever i find":
// ID : 5
// Score: 0.985
// Doc : "whenever I find myself involuntarily pausing before coffin warehouses , and bringing up the rear of every funeral I meet ; "
// ID : 3
// Score: 0.962
// Doc : "Whenever I find myself growing grim about the mouth ; "
}