-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_reduce.js
37 lines (29 loc) · 844 Bytes
/
map_reduce.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
const _ = require('lodash');
function letterFrequency(text, min = 3) {
const results = countItems(text, 'letters');
return _.pickBy(results, function (value, key) { return value > min } );
}
function wordFrequency(text, min = 3) {
let results = countItems(text, 'words');
return _.pickBy(results, function (value, key) { return value > min } );
}
function countItems(text, item) {
separator = (item == 'letters') ? '' : ' '
// map phase
words = text.split(separator).map(function(word){
return word.toLowerCase().replace(/[\.,]/g,'')
});
// reduce phase
hash = words.reduce(function(x, y) {
x[y] = (x[y] || 0) + 1;
return x;
}, {});
return hash;
}
var mapReduce = (function (){
return {
letterFrequency: letterFrequency,
wordFrequency: wordFrequency,
};
})();
module.exports = mapReduce;