Created
August 22, 2016 14:11
-
-
Save zefirka/ada87e95a617ff83a2f2ef4eaf26dee1 to your computer and use it in GitHub Desktop.
Word counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const {readFile} = require('fs'); | |
const uniq = (res, x) => { | |
if (!~res.indexOf(x)){ | |
res.push(x); | |
}; | |
return res; | |
}; | |
function excludes(word){ | |
/* Должен вернуть false для всех слов исключений */ | |
return true | |
} | |
readFile('data.txt', 'utf-8', (err, data) => { | |
const words = data | |
.replace(/\n/g, ' ') | |
.split(' ') | |
.map(e => e.trim().toLowerCase()) | |
.filter(e => /^[a-zа-я]+$/.test(e)) | |
.filter(e => excludes(e)) | |
.filter(e => e.length > 3) | |
const map = words.reduce((map, word) => { | |
if (map[word]){ | |
map[word] += 1; | |
} else { | |
map[word] = 1 | |
} | |
return map; | |
}, {}); | |
const popular = Object.keys(map).map(word => ({[word]: map[word]})).sort((w1, w2) => { | |
return w2[Object.keys(w2).pop()] - w1[Object.keys(w1).pop()] | |
}).slice(0, 100); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment