anagram v1.0.0 Anagram.Finder

Summary

Functions

Add a word to our anagrams dictionary

Find all anagrams of the given word (from our dictionary). Returns a list

Canonicalise the word by sorting it’s characters

Test if our candidate word can be made using only the given letters (case insensitive)

Load the given dictionary for later use by our anagram finder

Find all words in our given dictionary, which can be made from a subset of the given letters (case insensitive)

Functions

add_word(word, dict)

Add a word to our anagrams dictionary

iex> Anagram.Finder.add_word("sober", Map.new)
%{"beors" => ["sober"]}

iex> ["sober", "robes"] |> Enum.reduce(Map.new, &Anagram.Finder.add_word/2)
%{"beors" => ["robes", "sober"]}
anagrams(word, remove_self \\ false, dict \\ load_dictionary())

Find all anagrams of the given word (from our dictionary). Returns a list

For repeated searches its highly recommended to preload the dictionary using load_dictionary/1 and pass it to this function

The default dictionary will be web2, ie assumes a unixy OS. Load your own dictionary if this isn’t present/wanted

canonicalise(word)

Canonicalise the word by sorting it’s characters

iex> Anagram.Finder.canonicalise("hello\r\n")
"ehllo"
is_made_from(word, letters)

Test if our candidate word can be made using only the given letters (case insensitive)

load_dictionary(path \\ "/usr/share/dict/web2")

Load the given dictionary for later use by our anagram finder

Format is a map, where

  • the keys are a normalised form of the word (lower case + sorted)
  • the values are a list of anagrams of these letters

Finding anagrams is simply a case of normalising a candidate word and then looking up all the anagrams from our dictionary

words_from(letters, dictionary \\ "/usr/share/dict/web2")

Find all words in our given dictionary, which can be made from a subset of the given letters (case insensitive)

letters can be passed either as a bitstring or as a list of individual (bitstring) letters Dictionary defaults to: @dictionary