This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.71 KB
/
index.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
const program = require('commander')
const natural = require('natural')
const classifier = new natural.BayesClassifier()
const fs = require('fs')
program
.command('train')
.option(
'-t, --training-set <file>',
'training set file name',
'training_set.json'
)
.option('-m, --model <file>', 'model file name', 'model.json')
.description('Trains the classifier and saves the model')
.action((cmd) => {
const rawFile = fs.readFileSync(cmd.trainingSet)
const training_set = JSON.parse(rawFile)
for (const key in training_set) {
if (Object.prototype.hasOwnProperty.call(training_set, key)) {
const samples = training_set[key]
samples.forEach((sample) => {
classifier.addDocument(sample, key)
})
}
}
classifier.train()
classifier.save(cmd.model)
})
program
.command('classify [sample]')
.option('-s, --samples <file>', 'test samples file', 'samples.json')
.option('-m, --model <file>', 'model file', 'model.json')
.option('-b, --best-match', 'show only best match', false)
.description('Classify a samples or samples from a file applying a model')
.action((sample, cmd) => {
natural.BayesClassifier.load(cmd.model, null, (err, clazzifier) => {
if (err) throw err
const classify = (sample, bestMatch) => {
if (!bestMatch) return clazzifier.getClassifications(sample)
return clazzifier.classify(sample)
}
if (sample) console.log(classify(cmd.bestMatch))
else {
const rawdata = fs.readFileSync(cmd.samples)
const samples = JSON.parse(rawdata)
samples.forEach((sample) => {
console.log(classify(sample, cmd.bestMatch))
})
}
})
})
program.parse()