anagram v1.0.0 Anagram

Useful functions to find anagrams of words and words that can be made with a set of letters (sort of a sub anagram)

Our algorithm for anagrams is simply to canonicalise each word in the dictionary by forcing case, removing whitespace and sorting the characters. This will mean that all words with the same constituent letters in some permuted order, will have the same canonicalised form. Now we simply stuff our words into a Map, where the key is our canonicalised form

Finding anagrams is simply a case of canonicalising the word we are after and searching for an entry in the Map with that key

For speed, load the dictionary once and hang onto it and pass to the anagram function

Summary

Functions

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

Load the given dictionary for later use by our anagram finder

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

Functions

anagrams(word, remove_self \\ false)

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

anagrams(word, remove_self, dict)
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(word, dictionary \\ "/usr/share/dict/web2")

Find all words in our given dictionary file, 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

Default dictionary is web2, ie assumes a unixy OS. Specify your own dictionary if this isn’t present/wanted