Download the Dictionary
First you have to download the dictionary words.txt
https://github.com/indrajithi/anagram/blob/master/words.txt
Open the dictionary file in python
Create a file called anagram.py and the add the following.
file = open('words.txt')
wd_in = raw_input("enter a jumbled word: ")
found = 0
Function anagram
Now lets define a function is_anagram()
def is_anagram(word1, word2):
"""Returns True if anagram is found. Else return False."""
count = 0
word1, word2 = list(word1), list(word2)
if len(word1) == len(word2):
for i in word1:
if i in word2:
word2.remove(i)
count += 1
if count == len(word1):
return True
return False
Call the anagram function
for line in file:
if is_anagram(wd_in,line.strip()):
found += 1
if found == 1:
print "The possible anagrams are:"
print line.strip()
if found == 0:
print "No possible match"
else:
print "Found %d match." % found
Complete code
https://github.com/indrajithi/anagram
def is_anagram(word1, word2):
"""Returns True if anagram is found. Else return False."""
count = 0
word1, word2 = list(word1), list(word2)
if len(word1) == len(word2):
for i in word1:
if i in word2:
word2.remove(i)
count += 1
if count == len(word1):
return True
return False
file = open('words.txt')
wd_in = raw_input("enter a jumbled word: ")
found = 0
for line in file:
if is_anagram(wd_in,line.strip()):
found += 1
if found == 1:
print "The possible anagrams are:"
print line.strip()
if found == 0:
print "No possible mathch"
else:
print "Found %d match." % found