Skip to content

Instantly share code, notes, and snippets.

@kaleguy
Last active December 27, 2019 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaleguy/1b89c870bfd08ff5bd19dd281dcdb280 to your computer and use it in GitHub Desktop.
Save kaleguy/1b89c870bfd08ff5bd19dd281dcdb280 to your computer and use it in GitHub Desktop.
Counting word frequencies with lodash/fp
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