Determining the word frequency in a text
In this recipe, we will load a text file, extract the words from it, and then determine the number of times each of these words appears in the text.
The output will be written into a new file named word_frequency.txt
, where the words found in the text will be sorted and followed by an integer indicating their frequency in the text.
Getting ready
We will create a new Mix project and escriptize it, allowing us to run it as a command-line application:
Create a new Mix project:
> mix new word_frequency
Add the
escript
option to themix.exs
file, indicating where the main function is located; in this case, it will be in theWordFrequency
module:def project do [app: :word_frequency, version: "0.0.1", elixir: "~> 1.0.0", escript: [main_module: WordFrequency], deps: deps] end
How to do it…
We will be adding all the required code to the lib/word_frequency.ex
file. Open it in your editor and let's get started:
We will be using the
Logger
module to...