-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtraining.js
49 lines (40 loc) · 1.35 KB
/
training.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
const {BayesClassifier} = require('natural')
const {readFileSync} = require('fs')
const {join} = require('lodash')
const classifier = new BayesClassifier()
function train () {
var data = readFileSync('training-data.json', 'utf-8')
data = JSON.parse(data)
for (let i = 0; i < data.length; i++) {
// The keywords is an array but natural classifier works better with a string
// so keywords are turned to string of text.
let text = join(data[i].keywords, ',')
classifier.addDocument(text, data[i].field)
}
}
train()
classifier.events.on('trainedWithDocument', function (obj) {
// console.log(obj);
})
classifier.train()
classifier.save('classifier.json', (err, classifier) => {
if (err) throw err
})
// function classifyTweet(text){
// return new Promise((resolve, reject)=>{
// BayesClassifier.load('classifier.json', null, (err, classifier)=>{
// if(err)reject(err);
// //text = removeHash(text);
// let classification = classifier.classify(text);
// if(classification.length > 0){
// resolve(classification);
// }
// resolve(null);
// });
// })
// }
// classifyTweet("RT @rbanks: I\'m looking for two design interns for the summer to work at @MSFTResearchCam. One will focus on UX, working on ").then((data)=>{
// console.log(data);
// }).catch((err)=>{
// console.log(err);
// })