forked from NaturalNode/natural
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxEntClassifier_spec.js
More file actions
129 lines (103 loc) · 4.02 KB
/
Copy pathMaxEntClassifier_spec.js
File metadata and controls
129 lines (103 loc) · 4.02 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
/*
Unit test of Classifier
Copyright (C) 2018 Hugo W.L. ter Doest
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict'
var natural = require('../lib/natural');
var SE_Element = natural.SE_Element;
var Context = natural.Context;
var Feature = natural.Feature;
var FeatureSet = natural.FeatureSet;
var Sample = natural.Sample;
var Scaler = natural.GISScaler;
var Classifier = natural.MaxEntClassifier;
var classifierFilename = "classifier.json";
var minImprovement = 0.01;
var nrIterations = 20;
var sample = null;
var featureSet = null;
var classifier = null;
const DEBUG = false;
describe("The MaxEnt module", function() {
it("The Sample class creates a sample", function() {
sample = new Sample();
sample.addElement(new SE_Element("x", new Context("0")));
sample.addElement(new SE_Element("x", new Context("0")));
sample.addElement(new SE_Element("x", new Context("0")));
sample.addElement(new SE_Element("y", new Context("0")));
sample.addElement(new SE_Element("y", new Context("0")));
sample.addElement(new SE_Element("y", new Context("0")));
sample.addElement(new SE_Element("x", new Context("1")));
sample.addElement(new SE_Element("y", new Context("1")));
sample.addElement(new SE_Element("y", new Context("1")));
sample.addElement(new SE_Element("y", new Context("1")));
expect(sample.size()).toBe(10);
});
it("The FeatureSet class creates a feature set", function() {
featureSet = new FeatureSet();
sample.generateFeatures(featureSet);
expect(featureSet.size()).toBe(2);
});
it("The Classifier class creates a classifier", function() {
// Create a classifier
classifier = new Classifier(featureSet, sample);
expect(classifier).not.toBe(undefined);
});
it("Classifier does not need a correction feature", function() {
});
it("The classifier stops training after a specified number or iterations " +
"or when the minimum improvement in likelihood is reached", function() {
classifier.train(nrIterations, minImprovement);
expect(classifier.scaler.iteration).toBeLessThan(nrIterations + 1);
if (classifier.scaler.iteration < nrIterations) {
expect(classifier.scaler.improvement).toBeLessThan(minImprovement);
}
});
it("Save classifer to a file", function(done) {
classifier.save(classifierFilename, function(err, c) {
if (err) {
console.log(err);
}
else {
DEBUG && console.log("Classifier saved to " + classifierFilename);
}
done();
});
});
var newClassifier = null;
it("Load classifer", function(done) {
classifier.load(classifierFilename, SE_Element, function(err, c) {
if (err) {
console.log(err);
}
else {
DEBUG && console.log("Classifier loaded from " + classifierFilename);
newClassifier = c;
}
done();
});
if (newClassifier) {
classifier = newClassifier;
}
});
it("The classifier classifies events", function() {
var context = new Context("0");
DEBUG && console.log("Classes plus scores " + JSON.stringify(classifier.getClassifications(context)));
var classification = classifier.classify(context);
expect(classification).toBe("x");
var context = new Context("1");
DEBUG && console.log("Classes plus scores " + JSON.stringify(classifier.getClassifications(context)));
var classification = classifier.classify(context);
expect(classification).toBe("y");
});
});