-
Notifications
You must be signed in to change notification settings - Fork 860
/
bayes_classifier_spec.js
173 lines (140 loc) · 7.85 KB
/
bayes_classifier_spec.js
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
/*
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.
*/
var natural = require('../lib/natural');
describe('bayes classifier', function() {
describe('classifier', function() {
it('should classify with arrays', function() {
var 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');
classifier.train();
expect(classifier.classify(['bug', 'code'])).toBe('computing');
expect(classifier.classify(['read', 'thing'])).toBe('literature');
});
it('should provide all classification scores', function() {
var 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');
classifier.train();
expect(classifier.getClassifications('i write code')[0].label).toBe('computing');
expect(classifier.getClassifications('i write code')[1].label).toBe('literature');
});
it('should classify with strings', function() {
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();
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() {
var classifier = new natural.BayesClassifier()
, arr
, item
, 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
arr = classifier.getClassifications('qux');
for (var 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() {
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');
var obj = JSON.stringify(classifier);
var 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() {
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');
asyncSpecDone();
});
});
});
it('should accept an optional smoothing parameter for the Bayesian estimates', function() {
var defaultClassifier = new natural.BayesClassifier();
var PorterStemmer = require('../lib/natural/stemmers/porter_stemmer');
var newClassifier1 = new natural.BayesClassifier(PorterStemmer);
var 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);
});
});
});