-
Notifications
You must be signed in to change notification settings - Fork 0
/
createVectors.js
64 lines (52 loc) · 1.45 KB
/
createVectors.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 fs = require("fs");
require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const use = require('@tensorflow-models/universal-sentence-encoder');
input_file_name = "data/motions/data.json";
let datas;
try {
datas = JSON.parse(fs.readFileSync(input_file_name, 'utf-8'));
} catch (err) {
console.log(err);
}
let sentences;
let vectors = {}
vectors["national"] = []
vectors["international"] = []
let allVectors = []
use.load()
.then(model => {
sentences = datas["national"]
console.log(sentences);
model.embed(sentences)
.then(async embeddings => {
let vec = await embeddings.array();
console.log(vec);
datas["national_vectors"] = [];
for (let i=0; i<Object.keys(sentences).length; i++){
datas["national_vectors"].push(vec[i]);
vectors["national"].push(vec[i]);
allVectors.push(vec[i]);
}
console.log(datas[["national_vectors"]]);
use.load()
.then(model => {
sentences = datas["international"]
console.log(sentences);
model.embed(sentences)
.then(async embeddings => {
let vec = await embeddings.array();
console.log(vec);
datas["international_vectors"] = [];
for (let i=0; i<Object.keys(sentences).length; i++){
datas["international_vectors"].push(vec[i]);
vectors["international"].push(vec[i]);
allVectors.push(vec[i]);
}
output_file_name = "data/vectors/data.json"
let output = JSON.stringify(allVectors);
fs.writeFileSync(output_file_name, output);
});
});
});
});