Letterpress Word Finder
In an attempt to start to blog more, here’s a quick follow-up post on the previous Letterpress article.
As a reminder, here’s how I outlined steps in creating a Letterpress solver:
- Take screenshot of game and import it into solver
- Parse the board into a string of letters
- Reduce a dictionary of valid words against those characters to find playable words
- Optionally make recommendations of which word to play based on current board state and strategy. (i.e. don’t be naive)
We built step one (sort-of) and step two in the previous article, so let’s move on to step three.
We want our script to fulfill the following requirements:
- Accept the board letters via STDIN or commandline arguments.
- Reduce the dictionary words against those letters.
- Dump out matching words (without regard to board state/strategy).
We’ll take either an argument or read STDIN and downcase it.
I don’t have the official Letterpress dictionary (a quick googling will get you on the right track if you insist), but every good unix-y system has a dictionary file.
$ cat /usr/share/dict/words | wc -l 235886
OK, that’s a lot of words. Let’s pull them in and downcase them too.
Now, the only really interesting part: a method to determine if a word can be constructed from letters. I’ve shamelessly borrowed a perfectly fast solution from Stackoverflow.
1 2 3
And now we reduce our words by those that match our letters
1 2 3
And there’s nothing left to do but dump them out.
Here’s the entire word generating script.
And an example of using it with the board parser from the previous post:
$ ruby -r ./board_parser -e "puts BoardParser.new('light.png').tiles.join" | ruby letter.rb | tail -n 10 hermodactyl typhlectomy cryohydrate polydactyle pterodactyl crymotherapy hydrolyzable acetylthymol overthwartly protractedly
Excellent. Of course, not all words in your system’s dictionary file may be playable, YMMV, etc.