This repository was archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.js
More file actions
77 lines (65 loc) · 1.58 KB
/
classifier.js
File metadata and controls
77 lines (65 loc) · 1.58 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
const msgpack = require("msgpack");
const fs = require("fs");
const bayes = require("bayes");
const zlib = require("zlib");
const { Tokenizer } = require("./tokenizer");
// CLASSIFIER
exports.Classifier = class {
// CONSTRUCTOR
constructor(name) {
if (!name) throw "no name provided for classifier";
this.name = name;
this.classifier = bayes({
tokenizer: new Tokenizer().tokenize
});
// tries to load the classifier content from file
this.load();
}
// LEARN
learn(text, category) {
this.classifier.learn(text, category);
}
// CATEGORIZE
categorize(text) {
return this.classifier.categorize(text);
}
// SAVE
save(callback) {
const json = this.classifier.toJson();
const packed = msgpack.pack(json);
zlib.deflate(packed, (err, zipped) => {
fs.writeFile(
this.name + "_classifier.msgpack.zip",
zipped,
"utf8",
err => {
if (callback) {
if (err) callback(err);
else callback(null, true);
}
}
);
});
}
// LOAD
load() {
// if stored version of the classifer exists, load this one
fs.readFile(
this.name + "_classifier.msgpack.zip",
"utf8",
(err, file) => {
// end function here, because file is not available,
// so we don't want to load anything further from that file
if (err || !file) return;
// unzip content
zlib.inflate(file, (err, unzipped) => {
if (err) throw err;
// un-msgpack the content
const unmsgpacked = msgpack.unpack(unzipped);
this.classifier = bayes.fromJson(unmsgpacked);
this.classifier.tokenizer = new Tokenizer().tokenize;
});
}
);
}
};