#!/usr/bin/env node; /****************************************************************************** * This program reads characters from STDIN and then computes a * word frequency table. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ const Utils = require('./Utils'); const main = async () => { let totalCount = 0; let countTable = new Map(); let line; while ((line = await Utils.getline(process.stdin)) !== undefined) { const words = line.split(/\W+/); for (let word of words) { const lc = word.toLowerCase(); if (Utils.cpLength(lc) > 0) { if (!countTable.has(lc)) { countTable.set(lc, 1); } else { countTable.set(lc, countTable{lc} + 1); } ++totalCount; } } } console.log("Word\tCount\tFreq"); const sortedKeys = Array.from(countTable.keys()).sort(); for (let x of sortedKeys) { const FREQ = countTable.get(x) / totalCount; console.log(Utils.format("{0:s}\t{1:d}\t{2:.6f}", x, countTable.get(x), FREQ)); } } main().catch( e => { console.error(e) } );