blob: 1c95d7ea487e9f3083e68c4c6d7ef30f5f3967ff (
plain)
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
|
//import { Doc } from "../new_fields/Doc";
//import { StrCast } from "../new_fields/Types";
//import { List } from "../new_fields/List";
//import { CognitiveServices } from "../client/cognitive_services/CognitiveServices";
var w2v = require('word2vec');
var assert = require('assert');
export class Recommender {
private _model: any;
static Instance: Recommender;
private dimension: number = 0;
constructor() {
console.log("creating recommender...");
Recommender.Instance = this;
}
/***
* Loads pre-trained model from word2vec
*/
private loadModel(): Promise<any> {
let self = this;
return new Promise(res => {
w2v.loadModel("./node_modules/word2vec/vectors.txt", function (err: any, model: any) {
self._model = model;
self.dimension = model.size;
res(model);
});
});
}
/***
* Testing
*/
public async testModel() {
if (!this._model) {
await this.loadModel();
}
if (this._model) {
let similarity = this._model.similarity('father', 'mother');
console.log(similarity);
}
else {
console.log("model not found :(");
}
}
/***
* Tests if instance exists
*/
public async testInstance(text: string) {
if (!this._model) {
await this.loadModel();
}
console.log(text);
}
/***
* Uses model to convert words to vectors
*/
public async vectorize(text: string[]) {
if (!this._model) {
await this.loadModel();
}
if (this._model) {
let word_vecs = this._model.getVectors(text);
return word_vecs;
}
}
}
|