Last active
December 27, 2019 02:15
-
-
Save kaleguy/1b89c870bfd08ff5bd19dd281dcdb280 to your computer and use it in GitHub Desktop.
Counting word frequencies with lodash/fp
This file contains 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
const fs = require('fs'); | |
const lineReader = require('line-reader'); | |
const _ = require('lodash'); | |
const {flow, values, uniq, orderBy, reverse, slice, each} = require('lodash/fp'); | |
const counts = {}; | |
const freq = {} | |
function report() { | |
// for a given word frequency, print out the terms with that frequency | |
function print(frequency) { | |
_.each(counts, (v, k) => { | |
(v === frequency) && k && console.log(`${k}: ${v}`) | |
}) | |
} | |
// get the top 10 word frequencies (and print) | |
const topTenCounts = flow(values, uniq, orderBy(_.identity, 'desc'), slice(0, 9), each(print))(counts) | |
} | |
function getCounts(line, last) { | |
line.toLowerCase() | |
.split(/\s+/) | |
.forEach(word => { | |
counts[word] | |
? counts[word] = counts[word] + 1 | |
: counts[word] = 1 | |
}) | |
last && report() | |
} | |
lineReader.eachLine('text.txt', getCounts); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment