forked from NaturalNode/natural
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayes_classifier_spec.js
More file actions
230 lines (195 loc) · 9.03 KB
/
Copy pathbayes_classifier_spec.js
File metadata and controls
230 lines (195 loc) · 9.03 KB
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
/*
Copyright (c) 2011, Chris Umbel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
'use strict'
const natural = require('../lib/natural')
// var sinon = require('sinon');
// var baseClassifier = require('../lib/natural/classifiers/classifier.js');
function setupClassifier () {
const classifier = new natural.BayesClassifier()
classifier.addDocument(['fix', 'box'], 'computing')
classifier.addDocument(['write', 'code'], 'computing')
classifier.addDocument(['script', 'code'], 'computing')
classifier.addDocument(['write', 'book'], 'literature')
classifier.addDocument(['read', 'book'], 'literature')
classifier.addDocument(['study', 'book'], 'literature')
return classifier
}
describe('bayes classifier', function () {
describe('classifier', function () {
it('should classify with arrays', function () {
const classifier = setupClassifier()
classifier.train()
expect(classifier.classify(['bug', 'code'])).toBe('computing')
expect(classifier.classify(['read', 'thing'])).toBe('literature')
})
it('should classify with parallel training', function () {
const classifier = setupClassifier()
// Check for parallel method
if (classifier.trainParallel) {
classifier.trainParallel(2, function (err) {
if (err) {
console.log(err)
return
}
expect(classifier.classify(['bug', 'code'])).toBe('computing')
expect(classifier.classify(['read', 'thing'])).toBe('literature')
// asyncSpecDone();
})
}
})
it('should classify with parallel batched training', function () {
const classifier = setupClassifier()
if (classifier.trainParallelBatches) {
// Check for parallel method
classifier.events.on('doneTraining', function () {
expect(classifier.classify(['bug', 'code'])).toBe('computing')
expect(classifier.classify(['read', 'thing'])).toBe('literature')
// asyncSpecDone();
})
classifier.trainParallelBatches({ numThreads: 2, batchSize: 2 })
}
})
it('should provide all classification scores', function () {
const classifier = setupClassifier()
classifier.train()
expect(classifier.getClassifications('i write code')[0].label).toBe('computing')
expect(classifier.getClassifications('i write code')[1].label).toBe('literature')
})
function setupClassifierWithSentences () {
const classifier = new natural.BayesClassifier()
classifier.addDocument('i fixed the box', 'computing')
classifier.addDocument('i write code', 'computing')
classifier.addDocument('nasty script code', 'computing')
classifier.addDocument('write a book', 'literature')
classifier.addDocument('read a book', 'literature')
classifier.addDocument('study the books', 'literature')
return classifier
}
it('should classify with strings', function () {
const classifier = setupClassifierWithSentences()
classifier.train()
expect(classifier.classify('a bug in the code')).toBe('computing')
expect(classifier.classify('read all the books')).toBe('literature')
})
it('should classify and re-classify after document-removal', function () {
const classifier = new natural.BayesClassifier()
let item
const classifications = {}
// Add some good/bad docs and train
classifier.addDocument('foo bar baz', 'good')
classifier.addDocument('qux zooby', 'bad')
classifier.addDocument('asdf qwer', 'bad')
classifier.train()
expect(classifier.classify('foo')).toBe('good')
expect(classifier.classify('qux')).toBe('bad')
// Remove one of the bad docs, retrain
classifier.removeDocument('qux zooby', 'bad')
classifier.retrain()
// Simple `classify` will still return a single result, even if
// ratio for each side is equal -- have to compare actual values in
// the classifications, should be equal since qux is unclassified
const arr = classifier.getClassifications('qux')
for (let i = 0, ii = arr.length; i < ii; i++) {
item = arr[i]
classifications[item.label] = item.value
}
expect(classifications.good).toEqual(classifications.bad)
// Re-classify as good, retrain
classifier.addDocument('qux zooby', 'good')
classifier.retrain()
// Should now be good, original docs should be unaffected
expect(classifier.classify('foo')).toBe('good')
expect(classifier.classify('qux')).toBe('good')
})
it('should serialize and deserialize a working classifier', function () {
const classifier = setupClassifierWithSentences()
const obj = JSON.stringify(classifier)
const newClassifier = natural.BayesClassifier.restore(JSON.parse(obj))
newClassifier.addDocument('kick a ball', 'sports')
newClassifier.addDocument('hit some balls', 'sports')
newClassifier.addDocument('kick and punch', 'sports')
newClassifier.train()
expect(newClassifier.classify('a bug in the code')).toBe('computing')
expect(newClassifier.classify('read all the books')).toBe('literature')
expect(newClassifier.classify('kick butt')).toBe('sports')
})
/*
it('should save and load a working classifier', function(done) {
var classifier = new natural.BayesClassifier();
classifier.addDocument('i fixed the box', 'computing');
classifier.addDocument('i write code', 'computing');
classifier.addDocument('nasty script code', 'computing');
classifier.addDocument('write a book', 'literature');
classifier.addDocument('read a book', 'literature');
classifier.addDocument('study the books', 'literature');
classifier.train();
classifier.save('bayes_classifier.json', function(err) {
natural.BayesClassifier.load('bayes_classifier.json', null, function(err, newClassifier){
newClassifier.addDocument('kick a ball', 'sports');
newClassifier.addDocument('hit some balls', 'sports');
newClassifier.addDocument('kick and punch', 'sports');
newClassifier.train();
expect(newClassifier.classify('a bug in the code')).toBe('computing');
expect(newClassifier.classify('read all the books')).toBe('literature');
expect(newClassifier.classify('kick butt')).toBe('sports');
done();
});
});
});
*/
/*
it('should only execute the callback once when failing to load a classifier', function(done) {
natural.BayesClassifier.load('nonexistant_bayes_classifier.json', null, function(err, newClassifier){
expect(err.code).toBe('ENOENT');
expect(newClassifier).toBe(undefined);
done();
});
});
*/
it('should accept an optional smoothing parameter for the Bayesian estimates', function () {
const defaultClassifier = new natural.BayesClassifier()
const PorterStemmer = require('../lib/natural/stemmers/porter_stemmer')
const newClassifier1 = new natural.BayesClassifier(PorterStemmer)
const newClassifier2 = new natural.BayesClassifier(PorterStemmer, 0.1)
expect(defaultClassifier.classifier.smoothing).toBe(1.0)
expect(newClassifier1.classifier.smoothing).toBe(1.0)
expect(newClassifier2.classifier.smoothing).toBe(0.1)
})
})
/*
describe('load', function () {
var sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it('should pass an error to the callback function', function () {
sandbox.stub(baseClassifier, 'load', function (filename, cb) {
cb(new Error('An error occurred'));
});
natural.BayesClassifier.load('/spec/test_data/tfidf_document1.txt', null, function (err, newClassifier) {
expect(err).toBe.ok;
expect(newClassifier).not.toBe.ok;
});
});
});
*/
})