-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclassify.js
103 lines (80 loc) · 2.64 KB
/
classify.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
var fs = require('fs');
var nj = require('numjs');
var syllable = require("./syllable-breaker");
var ERROR_THRESHOLD = 0.2;
/* === Loading Synapses === */
var synapse_file = "synapses.json";
var synapse = JSON.parse(fs.readFileSync(synapse_file, "utf8"));
var synapse_0 = nj.array(synapse.synapse0);
var synapse_1 = nj.array(synapse.synapse1);
var words = synapse.words;
var classes = synapse.classes;
/* === Helper Functions === */
// Break sentence to syllable and remove whitespaces
function tokenize(sentence) {
var result = syllable(sentence).filter(function(item) {
return !!item.trim();
});
return result;
}
// Building syllable matrix
function bow(sentence, words) {
var sentence_words = tokenize(sentence);
var bag = [];
for(var i in words) {
bag.push(0);
}
for(var i in sentence_words) {
var s = sentence_words[i];
for(var ii in words) {
var w = words[ii];
if(w == s) {
bag[ii] = 1;
}
}
}
return nj.array(bag);
}
/* === classification === */
function think(sentence) {
var x = bow(sentence, words);
var l0 = x;
var l1 = nj.sigmoid(nj.dot(l0, synapse_0));
var l2 = nj.sigmoid(nj.dot(l1, synapse_1));
return l2;
}
function classify(sentence) {
var results = think(sentence);
var results_array = results.tolist();
// Only keeping the results that are above ERROR_THRESHOLD
var trimmed_results = [];
for(i in results_array) {
if(results_array[i] > ERROR_THRESHOLD) {
trimmed_results.push([i, results_array[i]]);
}
}
// Sorting the results in descending order
trimmed_results = trimmed_results.sort(function(a, b) {
if (a[0] === b[0]) {
return 0;
} else {
return (a[0] > b[0]) ? -1 : 1;
}
});
var return_results = [];
for(i in trimmed_results) {
var r = trimmed_results[i];
return_results.push( [ classes[ r[0] ], r[1] ] );
}
if(!return_results.length) {
return_results.push( [ 'မသိပါ', '-1' ] );
}
console.log(sentence + " : " + return_results);
return return_results;
}
/* === Classifying === */
classify("နေကောင်းသွားပြီလား");
classify("ဒီကနေ့ဘာထူးလဲ");
classify("မနေ့ကကိစ္စ စိတ်မဆိုးနဲ့နော်");
classify("နားလည်ပေးလို့ အရမ်းကျေးဇူးတင်တယ်သိလား");
classify("သွားလိုက်ဦးမယ်၊ နောက်မှတွေ့ကြမယ်");