A word frequency map shows how many times each distinct word appears in a body of text. It is the foundation of keyword-density checks, basic text analytics, and quick readability or repetition audits.
How it works
The text is tokenised by splitting on whitespace, then each token is normalised and tallied in a map:
tokens = text.split(/\s+/) // split on whitespace
for each token:
if stripPunctuation: trim leading/trailing symbols
if caseInsensitive: token = token.toLowerCase()
counts[token] += 1
sort rows by count desc, then word asc
Using a map keyed by the normalised word gives an exact count in a single pass. The final sort puts the most common words on top, with alphabetical ordering breaking ties so the table is deterministic.
Example and tips
For the input the cat sat on the mat the cat ran, the word the appears three
times and cat twice, so they head the table. To estimate keyword density,
divide a word’s count by the total-words figure shown above the table. Strip
punctuation when analysing prose so that dog. and dog are not counted as two
different words.