-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalice_count.js
38 lines (31 loc) · 1.01 KB
/
alice_count.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
const fs = require("fs");
const aliceInWonderland = fs.readFileSync("./alice_in_wonderland.txt", "utf8");
const regex = /[.,\/#!$%\^&\*;:{}=\-_`~()?“”‘’—\[\]]+/g;
const countCat = () => {
const searchTerm = "cat";
const split = aliceInWonderland.replace(regex, "").split(/\s+/);
const catCount = split.reduce((acc, word) => {
if (word.toLowerCase().includes(searchTerm)) {
acc++;
}
return acc;
}, 0);
return catCount;
};
// Expected output is 35
console.log("Cat Count:", countCat());
const wordFrequency = () => {
const words = aliceInWonderland.replace(regex, "").split(/\s+/);
const wordCount = words.reduce((acc, word) => {
word = word.toLowerCase();
if (!acc[word]) {
acc[word] = 1;
} else {
acc[word]++;
}
return acc;
}, {});
return wordCount;
};
// Expected output is the top-ten most-frequent words, including the number of times they appear in the text, ordered from most-to-least frequent
console.log("Word Frequency:", wordFrequency());