I haven’t had a chance to do much coding this semester, and I’ve been itching to get back into it. My problem with coding has always been finding interesting projects that aren’t too difficult – everything I think of is either boring or beyond my capabilities.
While I was home on Thanksgiving break, during a family Scrabble game, it occurred to me that there were probably anagram generators available online specifically for playing Scrabble, and after looking at a few I decided I wanted to try to make my own. I figured it would be a pretty easy project, but would allow me to refresh my rusty Python.
I began by looking for a dictionary to use for checking whether I’ve generated valid words. I thought I would use the built-in Linux word list in /usr/share/dict, but reading around I learned about Python’s Enchant library. This library allows a user to define dictionary objects using a language tag (such as “en_US” for American English), and to check strings against them. The library also contains more advanced spell-checking methods such as “suggest”, which generates a list of suggestions for a misspelled word. I only touched the surface in my little script, but for more information a complete tutorial can be found here.
After setting up an Enchant dictionary, I started writing some loops to go through a set of letters and generate anagrams. I started getting bogged down quickly, and during a Google search I stumbled upon the wonderful itertools module. It’s a Python library for working with iterable datasets, and it happens to contain a function called
permutations() that does exactly what it sounds like – generate permutations of an iterable object. Using this function I was able to accomplish most of the “complicated” logic of my program in one line.
Having these two libraries in my toolkit, it remained only for me to put together the main logic. Two for loops and an enchant dict.check() later I had a working program. In the original version, the user was prompted to enter their set of letters while the program was running. I later changed this to make the input a command line argument. This allowed for combination with other command line tools (specifically grep).
The resulting script was only a few lines long. I left the commented line because I wasn’t sure I wanted to keep the command line style input.
import enchant import itertools import sys d = enchant.Dict("en-US") #combo = raw_input("Enter your letters: ") list =  combo = sys.argv for num in range(len(combo), 2, -1): for x in itertools.permutations(combo, num): test = ''.join(x) if (d.check(test) == True) and not(test in list): list.append(test) print test
Because the output of this program was intended for a human user, it seemed ok to just print out the resulting word list, instead of storing it somewhere. Originally, the shortest words were printed first, but I reversed the order because the most important words to a Scrabble player are obviously the longer ones.
The last thing I did with this program was to pipe the output to grep, as I mentioned above, and search it with regular expressions. I figured this would help a Scrabble player find only words that would fit in a certain location on the board, say a place with an O and a T separated by one blank space (this command would then be “
$python scrabble.py 'asdfghj' | grep o.t“).
As soon as I got a working prototype going I put this script to use in my current Scrabble game, but it was too late to save me. It turned out my gameplay had suffered while I was distracted by coding… Despite this, it was a really fun, albeit very short, project, and it introduced me to some new Python tools that are definitely worth knowing about. This was probably my best ‘one-evening project’ yet, and I certainly hope not my last.