-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
151 lines (138 loc) · 4.04 KB
/
index.test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
import { nGram } from "./index.ts";
/**
* @param {Array} a the input
* @param {Array} b the example to test against
* @return {boolean}
*/
const deepCompare = (a, b) => {
if (typeof a !== typeof b) {
throw new Error(
`Cannot compare elements of different types. Expected ${typeof b}, got ${typeof a}.`,
);
} else if (Array.isArray(a) && Array.isArray(b) && (a.length !== b.length)) {
throw new Error(
`Arrays were different lengths! Expected ${b.length}, got ${a.length}.`,
);
}
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) && Array.isArray(b[i])) {
return deepCompare(a[i], b[i]);
}
if (a[i] !== b[i]) {
throw new Error(`Found ${a[i]}, expected ${b[i]}`);
}
}
return true;
};
/* TEST INPUTS */
const simpleStr = "In the beginning God created the heavens and the earth.";
/* EXPECTED TEST OUTPUTS */
const simpleBigramResults = [
["In", "the"],
["the", "beginning"],
["beginning", "God"],
["God", "created"],
["created", "the"],
["the", "heavens"],
["heavens", "and"],
["and", "the"],
["the", "earth."],
];
const simpleBigramResultsPadded = [
[null, "In"],
["In", "the"],
["the", "beginning"],
["beginning", "God"],
["God", "created"],
["created", "the"],
["the", "heavens"],
["heavens", "and"],
["and", "the"],
["the", "earth."],
["earth.", null],
];
const simpleBigramResultsCustom = [
["In", "the"],
["the", "beginning"],
["beginning", "God"],
["God", "created"],
["created", "the"],
["the", "heavens"],
["heavens", "and"],
["and", "the"],
["the", "earth."],
["earth.", "END"],
];
const simpleTrigramResults = [
["In", "the", "beginning"],
["the", "beginning", "God"],
["beginning", "God", "created"],
["God", "created", "the"],
["created", "the", "heavens"],
["the", "heavens", "and"],
["heavens", "and", "the"],
["and", "the", "earth."],
];
const simpleTrigramResultsPadded = [
[null, null, "In"],
[null, "In", "the"],
["In", "the", "beginning"],
["the", "beginning", "God"],
["beginning", "God", "created"],
["God", "created", "the"],
["created", "the", "heavens"],
["the", "heavens", "and"],
["heavens", "and", "the"],
["and", "the", "earth"],
["the", "earth.", null],
["earth.", null, null],
];
const simpleTrigramResultsCustom = [
["START", "START", "In"],
["START", "In", "the"],
["In", "the", "beginning"],
["the", "beginning", "God"],
["beginning", "God", "created"],
["God", "created", "the"],
["created", "the", "heavens"],
["the", "heavens", "and"],
["heavens", "and", "the"],
["and", "the", "earth."],
["the", "earth.", "END"],
["earth.", "END", "END"],
];
/* eslint-disable indent */
/** SIMPLE */
/** BIGRAM */
/** DEFAULT */
console.log("START - simple - bigram - default");
const syncBigramDef = nGram(simpleStr, 2);
deepCompare(syncBigramDef, simpleBigramResults);
console.log("END - simple - bigram - default");
/** PADDED */
console.log("START - simple - bigram - padded");
const syncBigramPadded = nGram(simpleStr, 2, true);
deepCompare(syncBigramPadded, simpleBigramResultsPadded);
console.log("END - simple - bigram - padded");
/** CUSTOM PADDED */
console.log("START - simple - bigram - custom");
const syncBigramCustom = nGram(simpleStr, 2, [undefined, "END"]);
deepCompare(syncBigramCustom, simpleBigramResultsCustom);
console.log("END - simple - bigram - custom");
/** TRIGRAM */
/** DEFAULT */
console.log("START - simple - trigram - default");
const syncTrigramDef = nGram(simpleStr, 3);
deepCompare(syncTrigramDef, simpleTrigramResults);
console.log("END - simple - trigram - default");
/** PADDED */
console.log("START - simple - trigram - padded");
const syncTrigramPadded = nGram(simpleStr, 3, true);
deepCompare(syncTrigramPadded, simpleTrigramResultsPadded);
console.log("END - simple - trigram - padded");
/** CUSTOM PADDED */
console.log("START - simple - trigram - custom");
const syncTrigramCustom = nGram(simpleStr, 3, ["START", "END"]);
deepCompare(syncTrigramCustom, simpleTrigramResultsCustom);
console.log("END - simple - trigram - custom");