How do I search a text file for anagrams of a given word

I have to search through a text file for anagrams of a given word. The text file has one word per line. So far I've managed to write a function, that makes a dictionary from a given word, with the key being a letter in the word, and it's value being the number of times the letter is in the word. The second function loops through each line of the text file, creates a second dictionary of the same keys and values, and compares the two. if the two are equal to each other, the function will add that word to a list. Once the function finishes looping through the text file, it should print the list of anagrams, but it's printing a blank list. Here is my code, I have no clue where it's going wrong.

this is for creating the dictionary of the given word.

word= input("Enter a word: ")
letterdict = {}

def count_letters(word,letterdict):
    for letter in word:
        letterdict[letter] = letterdict.get(letter,0) + 1
    return letterdict

print(count_letters(word,letterdict))

this is for looping through the text file and comparing

def search():
    count_letters(word,letterdict)
    anagrams = []
    letterdict2={}
    f = open('EnglishWords.txt', 'r')
    for letter in f:
        letterdict2[letter] = letterdict2.get(letter,0) + 1
        if letterdict == letterdict2:
            anagrams.append[f]
        letterdict2.clear()
    f.close()
    anagrams.sort() #put list in alphabetical order

    return print(anagrams)

search()
pythonpython-3.xdictionaryfile-ioanagram

Answers

answered 2 years ago Aaron3468 #1

This looks like a problem of not using the global keyword to access (and write to) the letterdict you create. Declare your variables locally and pass them through your program using function parameters. Python doesn't provide strong global variable support (it's there, but takes a lot of attention to detail to use).

Consider rewriting your functions:

def count_letters(word):
    letterdict = dict()
    for letter in word:
        letterdict[letter] = letterdict.get(letter,0) + 1
    return letterdict

def search(word):
    letterdict = count_letters(word)
    anagrams = []
    letterdict2={}
    with open('EnglishWords.txt', 'r') as f:
        for line in f:
            for letter in line:
                letterdict2[letter] = letterdict2.get(letter,0) + 1
                if letterdict == letterdict2:
                    anagrams.append[line]
                letterdict2.clear()
    anagrams.sort() #put list in alphabetical order

return anagrams

A few notes about the original code:

  • return print(anagrams) probably doesn't do anything and seems like a syntax error in your code.
  • for letter in f: loads lines of the file to letter
  • count_letters(word,letterdict) does nothing with the calculated value
  • You may/may not want to include spaces and numbers in your letterdict

answered 2 years ago Lee Daniel Crocker #2

Much faster algorithm (inside the loop, anyway): go through your entire dictionary just once, creating a new file with two words on each line; the first is the word with its letters alphabetized, and then the word itself, for example:

aaadkrrv aardvark
aabcsu abacus
. . .

Then, sort that file. Now, looking for all the anagrams of a word is a simple direct lookup into a sorted list.

comments powered by Disqus