-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
55 lines (50 loc) · 1.9 KB
/
util.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
function randomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
function randomWeighted(dict) {
var total = Object.values(dict).reduce((total, x) => total + x);
var random = Math.floor(Math.random() * total);
var value = 0;
for (let property in dict) {
value += dict[property];
if (value >= random) return property;
}
}
function buildChain(text, splitSentences, stripUrls, stripChars, length) {
var sentences = text;
if (stripUrls) sentences = sentences.replace(/https?:\/\/.*\s/g, " ");
if (splitSentences)
sentences = sentences.replace("\n", " ").split(/[.?!]+/);
else
sentences = sentences.split(/\n+/);
if (stripChars) sentences = sentences.map(x => x.replace(/[^a-zA-Z'_ ]/g, " "));
sentences = sentences.map(x => x.trim()).filter(x => x.length > 0);
var chain = { "\n": {} };
for (let sentence of sentences) {
let wordsSingle = sentence.split(" ").filter(x => x.length > 0);
let words = [];
while (wordsSingle.length > 0)
words.push(wordsSingle.splice(0, length).join(" "));
words.push("\n");
for (let i = 0; i < words.length - 1; i++) {
if (i == 0) {
if (chain["\n"][words[i]] == null) chain["\n"][words[i]] = 0;
chain["\n"][words[i]]++;
}
if (chain[words[i]] == null) chain[words[i]] = {};
if (chain[words[i]][words[i + 1]] == null) chain[words[i]][words[i + 1]] = 0;
chain[words[i]][words[i + 1]]++;
}
}
return chain;
}
function reverseChain(chain) {
var reverse = {};
for (let key in chain) {
for (let follow in chain[key]) {
if (reverse[follow] == null) reverse[follow] = {};
reverse[follow][key] = chain[key][follow];
}
}
return reverse;
}