forked from kvfrans/twitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopwords.js
55 lines (49 loc) · 1.32 KB
/
topwords.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
var topwords = {}
var jsonfile = require('jsonfile')
fs = require('fs')
fs.readFile('database.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
data = data.toLowerCase();
var sentences = data.split("<eos>");
for(var s = 0; s < sentences.length; s++)
{
sentences[s].replace(/[^0-9a-zA-Z ]/g, '')
var words = sentences[s].split(" ");
for(var w = 0; w < words.length; w++)
{
// console.log(words[w]);
if(topwords[words[w]] == null)
{
topwords[words[w]] = 1;
}
else
{
topwords[words[w]] = topwords[words[w]] + 1;
// console.log(topwords[words[w]]);
}
}
}
var len = Object.keys(topwords).length;
var thewords = Object.keys(topwords);
for(var w = 0; w < len; w++)
{
if(topwords[thewords[w]] < 5)
{
// console.log(thewords[w]);
delete topwords[thewords[w]];
// w--;
}
else
{
topwords[thewords[w]] = w+1;
}
}
jsonfile.writeFile("words.json", topwords, function (err) {
// console.error(err)
})
jsonfile.writeFile("words.json", topwords, function (err) {
// console.error(err)
})
});