-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyze.js
54 lines (44 loc) · 1.15 KB
/
analyze.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
const quotes = require('./quotes');
const blacklistedTerms = {
'a': true,
'an': true,
'and': true,
'as': true,
'by': true,
'for': true,
'in': true,
'is': true,
'it': true,
'it\'s': true,
'of': true,
'that': true,
'the': true,
'to': true,
'with': true,
};
function analyseTerms({ quotes }) {
const quotesCount = quotes.length;
const termsOccurences = quotes.reduce((acc, quote) => {
quote
.replace(/,|;|\.|\?/g, '')
.toLowerCase()
.split(' ')
.forEach((term) => {
acc[term] = acc[term] || 0;
acc[term] += 1;
});
return acc;
}, {});
let uniqueTerms = Object.keys(termsOccurences).filter((term) => !blacklistedTerms[term]);
const uniqueTermsCount = uniqueTerms.length;
uniqueTerms = uniqueTerms.map((term) => ({
term,
occurences: termsOccurences[term],
frequency: (termsOccurences[term] * 100 / uniqueTermsCount).toFixed(2),
}))
.sort((a, b) => b.occurences - a.occurences);
const analysis = { quotesCount, uniqueTermsCount, uniqueTerms };
return analysis;
}
const analysis = analyseTerms({ quotes });
console.log(JSON.stringify(analysis, null, 2));