Skip to content

Instantly share code, notes, and snippets.

@frahugo
Last active May 28, 2024 13:00
Show Gist options
  • Save frahugo/13ff145f963744dfd20eb9df26fffd49 to your computer and use it in GitHub Desktop.
Save frahugo/13ff145f963744dfd20eb9df26fffd49 to your computer and use it in GitHub Desktop.
Count words in a text file

Write a module that takes a file path as argument and returns a map with word counts.

  • The module name is up to you
  • Additional modules can be implemented as needed
  • The file will always be a text file
  • The counting logic should be case insensitive
  • Other than the apostrophe in a contraction all forms of punctuation are ignored
  • The words can be separated by any form of whitespace (ie "\t", "\n", " ")
  • A word is:
    • A number composed of one or more ASCII digits (ie "0" or "1234") OR
    • A simple word composed of one or more ASCII letters (ie "a" or "they") OR
    • A contraction of two simple words joined by a single apostrophe (ie "it's" or "they're")

For example, this file:

That's the password: 'PASSWORD 123'!", cried the Special Agent.

So I fled.

would return:

%{
  "that's" => 1,
  "the" => 2,
  "password" => 2,
  "123" => 1,
  "cried" => 1,
  "special" => 1,
  "agent" => 1,
  "so" => 1,
  "i" => 1,
  "fled" => 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment