-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.js
72 lines (50 loc) · 1.36 KB
/
words.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
/*global require */
'use strict';
var FILTER_STRING = 'Carlos Andreu',
MAX_WORD_SIZE = 2,
FILTER_URLS = true,
//OUTPUT_PATH is something like 'carlosandreu.json'
OUTPUT_PATH = './'+FILTER_STRING.toLowerCase().replace(/ /g,'')+'.json',
//INPUT_PATH is generated with analyze.js
INPUT_PATH = './data.json',
messages = require(INPUT_PATH),
fs = require('fs'),
multiReplace = function (str, match, repl) {
do {
str = str.replace(match, repl);
} while(str.indexOf(match) !== -1);
return str;
},
trim = function (stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,'');
},
words = [],
msg = '',
author = '',
word = '',
arr = [],
len = 0;
messages.forEach(function(current){
msg = current.data.msg,
author = current.data.author;
if (author === FILTER_STRING) {
arr = msg.split(' ');
len = arr.length;
while (len--) {
word = trim(multiReplace(arr[len], '\n', '').toLowerCase());
if (word.length > MAX_WORD_SIZE ) {
if (FILTER_URLS) {
//Push to the words array if http and www don't match the current word
if ((word.indexOf('http') < 0) && (word.indexOf('www') < 0)) {
words.push(word);
}
} else {
//Not filtering, push the current word
words.push(word);
}
}
}//end while
}//end FILTER_STRING if statement
});//end forEach
console.log(words);
fs.writeFileSync(OUTPUT_PATH, words);