forked from andrewsli/node-markov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.test.js
104 lines (84 loc) · 3.47 KB
/
markov.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
const { MarkovMachine } = require("./markov.js");
describe("Markov Machine", function () {
let mm = new MarkovMachine("Billy and the Billy trains that ever billied. How many times did Billy chase down the Billy train?");
describe('makeChains', function () {
test('test that every word is included in wordChain keys', function () {
for (let word of mm.words) {
expect(Object.keys(mm.wordChain)).toContain(word);
}
});
test('test that every key in wordChain is in words', function () {
for (let key of Object.keys(mm.wordChain)) {
expect(mm.words).toContain(key);
}
});
test('test that for every key word in wordChain, the word that occurs after it is in the value array', function () {
for (let key of Object.keys(mm.wordChain)) {
for (let i = 0; i < mm.words.length - 1; i++) {
if (mm.words[i] === key) {
expect(mm.wordChain[key]).toContain(mm.words[i + 1]);
}
}
}
});
});
describe('makeBigramChain', function () {
test('test that each bigram key has two words', function () {
for (let key of Object.keys(mm.bigramChain)) {
let wordList = key.split(" ");
expect(wordList.length).toEqual(2);
}
})
test('test that each bigram key word pair appears in that order in words at least once', function () {
let wordString = mm.words.join(" ");
for (let key of Object.keys(mm.bigramChain)) {
expect(wordString).toContain(key);
}
});
test('test that for every bigram key in bigramChain, the word that occurs after it is in the value array', function () {
for (let key of Object.keys(mm.bigramChain)) {
let wordList = key.split(" ");
for (let i = 0; i < mm.words.length - 2; i++) {
if (wordList[0] === mm.words[i] && wordList[1] === mm.words[i + 1]) {
expect(mm.bigramChain[key]).toContain(mm.words[i + 2]);
}
}
}
});
});
describe('makeBigramText', function () {
let text = mm.makeBigramText(numWords = 100)
let words = text.split(" ")
test('test that text length is between 0 and specified numWords', function () {
expect(words.length).toBeGreaterThan(0)
expect(words.length).toBeLessThan(100)
});
test('test that text output adheres to bigramChain', function () {
for (let key of Object.keys(mm.bigramChain)) {
let keyWords = key.split(" ");
for (let i = 0; i < words.length - 2; i++) {
if (keyWords[0] === words[i] && keyWords[1] === words[i + 1]) {
// console.log("THE CHAIN IS: ",mm.bigramChain, "THE VALUES ARE: ", mm.bigramChain[key])
// console.log("SHOULD CONTAIN: ", words[i+2])
// console.log("keyword 0: ", keyWords[0]," || keyword 1:", keyWords[1], "|| words i: ", words[i],"|| word i+1: ", words[i+1] )
// console.log(text)
expect(mm.bigramChain[key]).toContain(words[i + 2])
}
}
}
});
});
describe('makeText', function () {
let text = mm.makeText(numWords = 100)
let words = text.split(" ")
test('test that text length is between 0 and specified numWords', function () {
expect(words.length).toBeGreaterThan(0)
expect(words.length).toBeLessThan(100)
});
test('test that text output adheres to wordChain', function () {
for (let i = 1; i < words.length - 1; i++) {
expect(mm.wordChain[words[i]]).toContain(words[i + 1])
}
});
});
});