-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (37 loc) · 1.46 KB
/
index.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
const count_array = ({ arr, debug, n }) => {
const counts = {};
if (debug) console.log("arr:", arr);
for (let i = n; i <= arr.length; i++) {
const ngram = arr.slice(i - n, i);
if (debug) console.log("ngram:", ngram);
if (counts.hasOwnProperty(ngram)) counts[ngram].count++;
else counts[ngram] = { original: ngram, count: 1 };
}
if (debug) console.log("counts:", counts);
const sorted = Object.entries(counts).sort((a, b) => Math.sign(b[1].count - a[1].count));
if (debug) console.log("sorted:", sorted);
const results = sorted.map(it => [it[1].original, it[1].count]);
if (debug) console.log("results:", results);
return results;
}
const count_text = ({ text, debug, n }) => {
const counts = {};
if (debug) console.log("text: " + text.substring(0, 100).trim() + "...");
for (let i = n; i <= text.length; i++) {
const ngram = text.substring(i - n, i);
if (debug) console.log("ngram:", ngram);
if (counts.hasOwnProperty(ngram)) counts[ngram]++;
else counts[ngram] = 1;
}
if (debug) console.log("counts:", counts);
const sorted = Object.entries(counts).sort((a, b) => Math.sign(b[1] - a[1]));
if (debug) console.log("sorted:", sorted);
return sorted;
}
module.exports = ({ data, debug, n }) => {
if (Array.isArray(data)) {
return count_array({ arr: data, debug, n });
} else {
return count_text({ text: data, debug, n });
}
};