Solving anagrams in Python

Here's an example of how to make a simple anagram solver. The user will type in a word with the letters jumbled up and thanks to some python magic the word that these letters spell out will be revealed.

There are two parts to this problem. First we take a text file, dict.txt that has one English word on each line. The file looks something like this:

AARDVARK 

BAKERY 

CAKE 

From this we will create a new file, new_dict.csv that contains strings of all the words in the dictionary and along with each word another string with their letters rearranged in alphabetical order. So new_dict,csv will look like this:

AARDVARK AAADKRRV

BAKERY ABEKRY

CAKE ACEK

To create new_dict.csv we open up dict.txt and loop through each line creating an alphabetically sorted version of the word and remove all whitespace. The word and it's sorted version are assigned to a python dictionary 'd' as keys and values. Once dict.txt is closed we use list comprehension to assign the key value pairs to a new csv file.

p =  open('d:/programming/python/words/dict.txt', 'r+')
d = {}
for line in p:
    new_string = ''
    y = line.strip()
    d[line.strip()] = new_string.join(sorted(y))

p.close()

lines = ['%s,%s\n' % x for x in d.iteritems()]
with open('new_dict.csv', 'w') as q:
    q.writelines(lines)

To create the file new_dict.csv we can run all of the above code, before moving onto part two.

 

In part two we take the users jumbled up letters and using our newly created new_dict.csv find the word that they spell out. To do this we take the users jumble and sort it alphabetically. We then search the values (alphabetically sorted words) in new_dict.csv to see if there is a match. If there is no match the user sees the message 'Nothing found in the dictionary.' If a matching value is found we take it's key and print this as part of the following message for the user. 'Your word is (key)'

import csv

f = open('d:/programming/python/words/new_dict.csv')

question = raw_input('Anagram to solve: ')

csv_f = csv.reader(f)

for row in csv_f:
    if row[1] == ''.join(sorted(question)):
        print 'Your word is ' + row[0]
        break
else:
    print 'Nothing found in the dictionary'

f.close()

So here's how a successful word search would look in the python shell. I'd like to know what the letters TLAECINSDEN spell. So I run part two of our code in the python shell. I'm asked to type in my letters, a match is found and I am given the relevant word.

>>> 
Anagram to solve: TLAECINSDEN
Your word is CLANDESTINE
>>> 

There we have it our simple word search has worked. We might consider improving this further. Our code looks through 250,000 words but we might want to optimise it for larger dictionaries. We might want to consider multiple matches in our results too. But for now I hope you enjoyed this example of the power of python!