-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathnearest.js
62 lines (53 loc) · 1.82 KB
/
nearest.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
/**
* FastText.js
* @author Loreto Parisi (loretoparisi at gmail dot com)
* @copyright Copyright (c) 2017-2019 Loreto Parisi
*/
"use strict";
(function () {
var MODELS_ROOT = __dirname + '/models';
var Util = require('../lib/util');
var FastText = require('../lib/index');
var ft = new FastText({
predict: {
mostlikely: 10,
normalize: true
},
loadModel: MODELS_ROOT + '/sms_model_w2v.bin' // must specifiy filename and ext
});
function getRandomWord(arr) {
var word = '';
while (word.length <= 3) {
word = Util.randomElement(Util.randomElement(arr).split(/[\s.]/));
}
return word;
}
var samples = [
"You have WON a guaranteed �1000 cash or a �2000 prize. To claim yr prize call our customer service representative on 08714712379 between 10am-7pm Cost 10p",
"Sounds better than my evening im just doing my costume. Im not sure what time i finish tomorrow but i will txt you at the end"
];
// load unsupervised model
ft.loadnn()
.then(labels => {
// find Nearest Neighbor words
var word = getRandomWord(samples);
console.log("find Nearest Neighbor of \"%s\"", word);
return ft.nn(word)
})
.then(labels => {
console.log(JSON.stringify(labels, null, 2));
var word = getRandomWord(samples);
console.log("find Nearest Neighbor of \"%s\"", word);
return ft.nn(word);
})
.then(labels => {
console.log(JSON.stringify(labels, null, 2));
ft.unload();
})
.then(done => {
console.log("model unloaded.");
})
.catch(error => {
console.error("predict error", error);
});
}).call(this);