GitXplorerGitXplorer
n

swappy_rs

public
0 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
76fb45a9df4a457001b851b77f064332995d8849

Lower bench expectation again - regression was due to the badly-sorted file

nnathanl committed 5 years ago
Unverified
a2662b2d255e79e21ae040f9e02e09b6ce9d0b91

Fix wordlist - sort longest to shortest

nnathanl committed 5 years ago
Unverified
9c2d075a40c531b29ea96f32159d79c3d31a879e

Bump version to 0.3.0

nnathanl committed 5 years ago
Unverified
601777c12cccc346348fbae076553ac1f42e33bf

Improve README

nnathanl committed 5 years ago
Unverified
6f674890ed48065d3970689951d59999f68b460e

Better input sanitization

nnathanl committed 5 years ago
Unverified
b5432e0a04efe52a77fb9a60f77a919fc93a65f6

Missed lock file

nnathanl committed 5 years ago

README

The README file for this repository.

Swappy

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

  • "rust programming" => "grim gun port arms" or "asymmetry foe" or "tarring prom mugs"
  • "rust anagrams library" => "triangular brass army" or "snag arbitrary murals"
  • "software engineer" => "we are fingers o net" or "sea of wintergreen"
  • "memory safety" => "me, my artsy foe" or "ye soft yammer" or "format my eyes"

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

Installation

cargo install swappy

Usage:

  • swappy 'my phrase' (uses ~/.swappy_wordlist - copy this or your own)
  • LIMIT=20 WORDLIST=/some/file.txt swappy 'my phrase'

Swappy prioritizes anagrams containing words that occur earlier in the word list, and lets you limit how many anagrams 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.

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].

  • racecar /
    • race / car
      • race a / cr [failed leaf]
      • race car / [successful leaf]
    • racer / ca
      • racer a / c [failed leaf]

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.