Mojo (Mojo v0.1.1)

View Source

Implements interface functions using the Trie module under-the-hood.

Summary

Functions

Creates an empty dictionary.

Checks if a word exists in a dictionary.

Maps any error in a passage with their closest matches in the dictionary.

Returns all words that match a given prefix.

Updates a dictionary given a list of words, or a single word.

Functions

create_dict()

Creates an empty dictionary.

Returns a %Trie{}.

Examples

iex> Mojo.create_dict() %Trie{root: %TrieNode{children: %{}, is_terminal: false}}

in_dict?(root, word)

Checks if a word exists in a dictionary.

Returns an Atom representing true, or false.

Examples

iex> dict = Mojo.create_dict() iex> dict = Mojo.update_dict(dict, "word3") iex> Mojo.in_dict?(dict, "word3") true iex> Mojo.in_dict?(dict, "word4") false

parse(root, passage)

Maps any error in a passage with their closest matches in the dictionary.

Returns a %{}.

Examples

iex> dict = Mojo.create_dict() iex> dict = Mojo.update_dict(dict, ["word1", "word2", "word3"]) iex> Mojo.parse(dict, ["werd"]) %{"werd" => ["word1", "word2", "word3"]}

prefix_matches(root, prefix)

Returns all words that match a given prefix.

Returns a List.

Examples

iex> dict = Mojo.create_dict() iex> dict = Mojo.update_dict(dict, ["word1", "word2", "word3"]) iex> Mojo.prefix_matches(dict, "word") ["word1", "word2", "word3"]

update_dict(root, input)

Updates a dictionary given a list of words, or a single word.

Returns an updated %Trie{}.

Examples

iex> dict = Mojo.create_dict() iex> updated_dict = Mojo.update_dict(dict, ["word1", "word2"]) iex> updated_dict %Trie{

root: %TrieNode{
  children: %{
    "w" => %TrieNode{
      children: %{
        "o" => %TrieNode{
          children: %{
            "r" => %TrieNode{
              children: %{
                "d" => %TrieNode{
                  children: %{
                    "1" => %TrieNode{children: %{}, is_terminal: true},
                    "2" => %TrieNode{children: %{}, is_terminal: true}
                  },
                  is_terminal: false
                }
              },
              is_terminal: false
            }
          },
          is_terminal: false
        }
      },
      is_terminal: false
    }
  },
  is_terminal: false
}

}

iex> dict = Mojo.create_dict() iex> updated_dict = Mojo.update_dict(dict, "word3") iex> updated_dict %Trie{

root: %TrieNode{
  children: %{
    "w" => %TrieNode{
      children: %{
        "o" => %TrieNode{
          children: %{
            "r" => %TrieNode{
              children: %{
                "d" => %TrieNode{
                  children: %{
                    "3" => %TrieNode{children: %{}, is_terminal: true}
                  },
                  is_terminal: false
                }
              },
              is_terminal: false
            }
          },
          is_terminal: false
        }
      },
      is_terminal: false
    }
  },
  is_terminal: false
}

}