O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

  1. Create a new Mix project:
    > mix new word_frequency
    
  2. Add the escript option to the mix.exs file, indicating where the main function is located; in this case, it will be in the WordFrequency module:
    def project do [app: :word_frequency, version: "0.0.1", elixir: "~> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required