-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfidf.go
191 lines (153 loc) · 3.84 KB
/
tfidf.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
package tfidf
import (
"math"
"sync"
"github.com/KEVISONG/go/pkg/algorithms/tfidf/tokenizer"
"github.com/KEVISONG/go/pkg/algorithms/tfidf/utils"
)
// TFIDF defines TFIDF
type TFIDF struct {
Corpus *Corpus
t tokenizer.Tokenizer
mu *sync.RWMutex
}
// Corpus defines Corpus
type Corpus struct {
// TermCount stores all terms appeared in the corpus as key
// and the # of docs containing the term as value. It is
// used for calculating idf.
TermCount map[string]float64
// Corpus stores all documents with it's content hash as key
// and the document as value. It is used for calculating idf.
Documents map[string]Document
}
// Document defines Document
type Document struct {
ID string
Content string
Terms []string
tfidfs map[string]float64
}
// NewTFIDF factory
func NewTFIDF() *TFIDF {
return &TFIDF{
Corpus: &Corpus{
TermCount: map[string]float64{},
Documents: map[string]Document{},
},
t: &tokenizer.EnTokenizer{},
mu: &sync.RWMutex{},
}
}
// NewTFIDFWithTokenizer factory
func NewTFIDFWithTokenizer(t tokenizer.Tokenizer) *TFIDF {
return &TFIDF{
Corpus: &Corpus{
TermCount: map[string]float64{},
Documents: map[string]Document{},
},
t: t,
mu: &sync.RWMutex{},
}
}
func (t *TFIDF) calTF(terms []string) map[string]float64 {
termCount := utils.WordCount(terms)
tfs := map[string]float64{}
for _, term := range terms {
tfs[term] = termCount[term] / float64(len(terms))
}
return tfs
}
func (t *TFIDF) calIDF(terms []string) map[string]float64 {
idfs := map[string]float64{}
for _, term := range terms {
var denominator float64
denominator, ok := t.Corpus.TermCount[term]
if !ok {
denominator = 0
}
idfs[term] = float64(
math.Log(
float64(
//(float64(len(t.Corpus)) + 1) / float64(int(denominator)+1),
(float64(len(t.Corpus.Documents))) / float64(int(denominator)+1),
),
),
)
}
return idfs
}
// AddDoc adds doc to the corpus by:
// 1. Update Corupus for later calculation of other doc's idf (as numerator)
// 2. Update TermDocMap for later calculation of other docs's idf (as denominator)
func (t *TFIDF) AddDoc(doc string) {
// Update Corpus
id := utils.SHA256(doc)
terms := t.t.Exec(doc)
if len(terms) == 0 {
return
}
t.Corpus.Documents[id] = Document{
ID: id,
Content: doc,
Terms: terms,
}
// Update TermDocMap
for _, term := range terms {
t.Corpus.TermCount[term]++
}
// Recalculate tfidf
t.CalAll()
}
// Add Docs adds doc in batch
func (t *TFIDF) AddDocs(docs ...string) {
for _, doc := range docs {
t.AddDoc(doc)
}
// Recalculate tfidf
t.CalAll()
}
// CalAll calculates tf-idf for all documents in the corpus
func (t *TFIDF) CalAll() {
for id, doc := range t.Corpus.Documents {
for term, tfidf := range t.cal(doc.Terms) {
if doc.tfidfs == nil {
doc.tfidfs = map[string]float64{}
}
doc.tfidfs[term] = tfidf
}
t.Corpus.Documents[id] = doc
}
}
// Cal calculates tfidf for the doc
func (t *TFIDF) Cal(doc string) map[string]float64 {
terms := t.t.Exec(doc)
return t.cal(terms)
}
// cal calculates tfidf for given terms by:
// 1. Calculate tf of the doc
// 2. Calculate idf of the doc
// 2. Calculate tf-idf of the doc by tf * idf
func (t *TFIDF) cal(terms []string) map[string]float64 {
tfidfs := map[string]float64{}
// Calculate TF for each word in the doc
tfs := t.calTF(terms)
// Calculate IDF for each word in the corpus
idfs := t.calIDF(terms)
// Calculate TF-IDF for each word
for term, tf := range tfs {
tfidfs[term] = tf * idfs[term]
}
return tfidfs
}
// Query returns the calculated similarities of all
// the document in the corpus with the given doc.
func (t *TFIDF) Query(doc string) map[string]float64 {
sims := map[string]float64{}
tfidfs := t.cal(t.t.Exec(doc))
for id, document := range t.Corpus.Documents {
sim := utils.Cosine(tfidfs, document.tfidfs)
sims[id] = sim
}
return sims
}