Swappy

Anagrams are useless but funny. Let's make some!

Swappy produces anagrams of a given input phrase using a given word list file.

It prioritizes words that occur earlier in the word list, and lets you limit how many results are produced. This lets you tailor what kinds of results you want (eg, by putting words you think are funny at the top, or sorting the list longest to shortest for impressive finds). By default, it looks for a word list file at ~/.swappy_wordlist, but you can set the WORDLIST env var to another file. Swappy's repo contains a wordlist file sorted longest to shortest, and with many short, uncommon words (like "oe" and "mu") removed for increased efficiency.

Installation

cargo install swappy

Usage:

Basic Strategy

Swappy performs a depth-first search of the tree of possible anagrams which use our word list and phrase. A node where there are no remaining letters is an anagram.

Our tree could look like this, where a node is represented as [found words] / [remaining letters].

Before deciding if a phrase contains a word, it converts them both to "alphagrams", which are order-agnostic; "bat" and "tab" have the same alphagram. We represent alphagrams internally as a hashmap listing the count of each character. This makes comparison and subtraction efficient.

Swappy is single-threaded, but performs well. I considered a multi-threaded design, but could not think of a good way to use multiple threads and still guarantee that results are produced in the order given in the word list. I experimented with using a priority queue for this, but the queue was a significant performance bottleneck.