Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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: "~> 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:

  1. We will be using the Logger module to...