-
Notifications
You must be signed in to change notification settings - Fork 45
/
vectorisers.go
344 lines (301 loc) · 12 KB
/
vectorisers.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package nlp
import (
"regexp"
"strings"
"github.com/james-bowman/sparse"
"github.com/spaolacci/murmur3"
"gonum.org/v1/gonum/mat"
)
// Vectoriser provides a common interface for vectorisers that take a variable
// set of string arguments and produce a numerical matrix of features.
type Vectoriser interface {
Fit(...string) Vectoriser
Transform(...string) (mat.Matrix, error)
FitTransform(...string) (mat.Matrix, error)
}
// OnlineVectoriser is an extension to the Vectoriser interface that supports
// online (streaming/mini-batch) training as opposed to just batch.
type OnlineVectoriser interface {
Vectoriser
PartialFit(...string) OnlineVectoriser
}
// Transformer provides a common interface for transformer steps.
type Transformer interface {
Fit(mat.Matrix) Transformer
Transform(mat mat.Matrix) (mat.Matrix, error)
FitTransform(mat mat.Matrix) (mat.Matrix, error)
}
// OnlineTransformer is an extension to the Transformer interface that
// supports online (streaming/mini-batch) training as opposed to just batch.
type OnlineTransformer interface {
Transformer
PartialFit(mat.Matrix) OnlineTransformer
}
// Tokeniser interface for tokenisers allowing substitution of different
// tokenisation strategies e.g. Regexp and also supporting different
// different token types n-grams and languages.
type Tokeniser interface {
// ForEachIn iterates over each token within text and invokes function
// f with the token as parameter
ForEachIn(text string, f func(token string))
// Tokenise returns a slice of all the tokens contained in string
// text
Tokenise(text string) []string
}
// RegExpTokeniser implements Tokeniser interface using a basic RegExp
// pattern for unary-gram word tokeniser supporting optional stop word
// removal
type RegExpTokeniser struct {
RegExp *regexp.Regexp
StopWords map[string]bool
}
// NewTokeniser returns a new, default Tokeniser implementation.
// stopWords is a potentially empty string slice
// that contains the words that should be removed from the corpus
// default regExpTokeniser will split words by whitespace/tabs: "\t\n\f\r "
func NewTokeniser(stopWords ...string) Tokeniser {
var stop map[string]bool
stop = make(map[string]bool)
for _, word := range stopWords {
stop[word] = true
}
return &RegExpTokeniser{
RegExp: regexp.MustCompile("[\\p{L}]+"),
StopWords: stop,
}
}
// ForEachIn iterates over each token within text and invokes function
// f with the token as parameter. If StopWords is not nil then any
// tokens from text present in StopWords will be ignored.
func (t *RegExpTokeniser) ForEachIn(text string, f func(token string)) {
tokens := t.tokenise(text)
for _, token := range tokens {
if t.StopWords != nil {
if t.StopWords[token] {
continue
}
}
f(token)
}
}
// Tokenise returns a slice of all the tokens contained in string
// text. If StopWords is not nil then any tokens from text present in
// StopWords will be removed from the slice.
func (t *RegExpTokeniser) Tokenise(text string) []string {
words := t.tokenise(text)
// filter out stop words
if t.StopWords != nil {
b := words[:0]
for _, w := range words {
if !t.StopWords[w] {
b = append(b, w)
}
}
return b
}
return words
}
// tokenise returns a slice of all the tokens contained in string
// text.
func (t *RegExpTokeniser) tokenise(text string) []string {
// convert content to lower case
c := strings.ToLower(text)
// match whole words, removing any punctuation/whitespace
words := t.RegExp.FindAllString(c, -1)
return words
}
// CountVectoriser can be used to encode one or more text documents into a term document
// matrix where each column represents a document within the corpus and each row represents
// a term present in the training data set. Each element represents the frequency the
// corresponding term appears in the corresponding document e.g. tf(t, d) = 5 would mean
// that term t (perhaps the word "dog") appears 5 times in the document d.
type CountVectoriser struct {
// Vocabulary is a map of words to indices that point to the row number representing
// that word in the term document matrix output from the Transform() and FitTransform()
// methods. The Vocabulary map is populated by the Fit() or FitTransform() methods
// based upon the words occurring in the datasets supplied to those methods. Within
// Transform(), any words found in the test data set that were not present in the
// training data set supplied to Fit() will not have an entry in the Vocabulary
// and will be ignored.
Vocabulary map[string]int
// Tokeniser is used to tokenise input text into features.
Tokeniser Tokeniser
}
// NewCountVectoriser creates a new CountVectoriser.
// stopWords is a potentially empty slice of words to be removed from the corpus
func NewCountVectoriser(stopWords ...string) *CountVectoriser {
return &CountVectoriser{
Vocabulary: make(map[string]int),
Tokeniser: NewTokeniser(stopWords...),
}
}
// Fit processes the supplied training data (a variable number of strings representing
// documents). Each word appearing inside the training data will be added to the
// Vocabulary. The Fit() method is intended to be called once to train the model
// in a batch context. Calling the Fit() method a sceond time have the effect of
// re-training the model from scratch (discarding the previously learnt vocabulary).
func (v *CountVectoriser) Fit(train ...string) Vectoriser {
i := 0
if len(v.Vocabulary) != 0 {
v.Vocabulary = make(map[string]int)
}
v.fitVocab(i, train...)
return v
}
// fitVocab learns the vocabulary contained within the supplied training documents
func (v *CountVectoriser) fitVocab(start int, train ...string) {
i := start
for _, doc := range train {
v.Tokeniser.ForEachIn(doc, func(word string) {
_, exists := v.Vocabulary[word]
if !exists {
v.Vocabulary[word] = i
i++
}
})
}
}
// Transform transforms the supplied documents into a term document matrix where each
// column is a feature vector representing one of the supplied documents. Each element
// represents the frequency with which the associated term for that row occurred within
// that document. The returned matrix is a sparse matrix type.
func (v *CountVectoriser) Transform(docs ...string) (mat.Matrix, error) {
mat := sparse.NewDOK(len(v.Vocabulary), len(docs))
for d, doc := range docs {
v.Tokeniser.ForEachIn(doc, func(word string) {
i, exists := v.Vocabulary[word]
if exists {
mat.Set(i, d, mat.At(i, d)+1)
}
})
}
return mat, nil
}
// FitTransform is exactly equivalent to calling Fit() followed by Transform() on the
// same matrix. This is a convenience where separate training data is not being
// used to fit the model i.e. the model is fitted on the fly to the test data.
// The returned matrix is a sparse matrix type.
func (v *CountVectoriser) FitTransform(docs ...string) (mat.Matrix, error) {
return v.Fit(docs...).Transform(docs...)
}
// HashingVectoriser can be used to encode one or more text documents into a term document
// matrix where each column represents a document within the corpus and each row represents
// a term. Each element represents the frequency the corresponding term appears in the
// corresponding document e.g. tf(t, d) = 5 would mean that term t (perhaps the word "dog")
// appears 5 times in the document d.
type HashingVectoriser struct {
NumFeatures int
Tokeniser Tokeniser
}
// NewHashingVectoriser creates a new HashingVectoriser. If stopWords is not an empty slice then
// english stop words will be removed. numFeatures specifies the number of features
// that should be present in produced vectors. Each word in a document is hashed and
// the mod of the hash and numFeatures gives the row in the matrix corresponding to that
// word.
func NewHashingVectoriser(numFeatures int, stopWords ...string) *HashingVectoriser {
return &HashingVectoriser{
NumFeatures: numFeatures,
Tokeniser: NewTokeniser(stopWords...),
}
}
// Fit does nothing for a HashingVectoriser. As the HashingVectoriser vectorises features
// based on their hash, it does require a pre-determined vocabulary to map features to their
// correct row in the vector. It is effectively stateless and does not require fitting to
// training data. The method is included for compatibility with other vectorisers.
func (v *HashingVectoriser) Fit(train ...string) Vectoriser {
// The hashing vectoriser is stateless and does not require pre-training so this
// method does nothing.
return v
}
// PartialFit does nothing for a HashingVectoriser. As the HashingVectoriser vectorises
// features based on their hash, it does not require a pre-learnt vocabulary to map
// features to the correct row in the feature vector. This method is included
// for compatibility with other vectorisers.
func (v *HashingVectoriser) PartialFit(train ...string) Vectoriser {
// The hashing vectoriser is stateless and does not requre training so this method
// does nothing.
return v
}
// Transform transforms the supplied documents into a term document matrix where each
// column is a feature vector representing one of the supplied documents. Each element
// represents the frequency with which the associated term for that row occurred within
// that document. The returned matrix is a sparse matrix type.
func (v *HashingVectoriser) Transform(docs ...string) (mat.Matrix, error) {
mat := sparse.NewDOK(v.NumFeatures, len(docs))
for d, doc := range docs {
v.Tokeniser.ForEachIn(doc, func(word string) {
h := murmur3.Sum32([]byte(word))
i := int(h) % v.NumFeatures
mat.Set(i, d, mat.At(i, d)+1)
})
}
return mat, nil
}
// FitTransform for a HashingVectoriser is exactly equivalent to calling
// Transform() with the same matrix. For most vectorisers, Fit() must be called
// prior to Transform() and so this method is a convenience where separate
// training data is not used to fit the model. For a HashingVectoriser, fitting is
// not required and so this method is exactly equivalent to Transform(). As with
// Fit(), this method is included with the HashingVectoriser for compatibility
// with other vectorisers. The returned matrix is a sparse matrix type.
func (v *HashingVectoriser) FitTransform(docs ...string) (mat.Matrix, error) {
return v.Transform(docs...)
}
// Pipeline is a mechanism for composing processing pipelines out of vectorisers
// transformation steps. For example to compose a classic LSA/LSI pipeline
// (vectorisation -> TFIDF transformation -> Truncated SVD) one could use a
// Pipeline as follows:
// lsaPipeline := NewPipeline(NewCountVectoriser(false), NewTfidfTransformer(), NewTruncatedSVD(100))
//
type Pipeline struct {
Vectoriser Vectoriser
Transformers []Transformer
}
// NewPipeline constructs a new processing pipline with the supplied Vectoriser
// and one or more transformers
func NewPipeline(vectoriser Vectoriser, transformers ...Transformer) *Pipeline {
pipeline := Pipeline{
Vectoriser: vectoriser,
Transformers: transformers,
}
return &pipeline
}
// Fit fits the model(s) to the supplied training data
func (p *Pipeline) Fit(docs ...string) Vectoriser {
if _, err := p.FitTransform(docs...); err != nil {
panic("nlp: Failed to Fit pipeline because " + err.Error())
}
return p
}
// Transform transforms the supplied documents into a matrix representation
// of numerical feature vectors using a model(s) previously fitted to supplied
// training data.
func (p *Pipeline) Transform(docs ...string) (mat.Matrix, error) {
matrix, err := p.Vectoriser.Transform(docs...)
if err != nil {
return matrix, err
}
for _, t := range p.Transformers {
matrix, err = t.Transform(matrix)
if err != nil {
return matrix, err
}
}
return matrix, nil
}
// FitTransform transforms the supplied documents into a matrix representation
// of numerical feature vectors fitting the model to the supplied data in the
// process.
func (p *Pipeline) FitTransform(docs ...string) (mat.Matrix, error) {
matrix, err := p.Vectoriser.FitTransform(docs...)
if err != nil {
return matrix, err
}
for _, t := range p.Transformers {
matrix, err = t.FitTransform(matrix)
if err != nil {
return matrix, err
}
}
return matrix, nil
}