Anagram Solver Program From Scratch with Python - ThePygrammer

Simple Anagram Solver with Python Itertools and Enchant Module

Anagram Solver in Python | Anagram solving is a puzzle some like, and some don't. If you have ever tried to solve an Anagram and been stuck for hours, don't worry. Sometimes even the best word unscramblers may get stumped by a specific puzzle or two. However, when you have a computer in hand, no puzzle can stop you. "How can I tell the computer to solve puzzles ?" you might ask. This is where Python can help you out. A quick and straightforward Anagram Solver in Python, built with some basic modules, can assist you in unscrambling words quickly and finding anagrams of any word. 

(Now, for anyone who doesn't know) What is an Anagram: An anagram is a word formed by rearranging the letters of a different word using all the original letters exactly once. Some anagram examples are binary: brainy or internet: renitent.

Now, let's get into the Anagram Solver itself: The code is divided into two parts: The Word Creator and The Word Checker. What are these? Let me break it down - 

The Word Creator takes the unscrambled word input, breaks it down into letters, and finds all the possible permutations and hence, possible words that can be formed with those letters, with or without meaning. We can use the itertools module, which comes with the permutations function. 

The Word Checker is the awesome part. Using a fun module called enchant, we can check if any word is in the English dictionary. This will help us eliminate all the words that do not make sense and only take the words that have meaning. This will help us find all the valid anagrams of a word.

We also need to ensure that the same word isn't printed twice. For this, we can use a list and add all unique words to it so any word that is already in the list doesn't get printed again.  

Combining these, we can develop a word unscrambler code that looks something like this: 

import enchant
d = enchant.Dict("en_US")


word = input('Enter word: ')
letters = [chr for chr in word]
repeat_check = []

from itertools import permutations

for current in permutations(letters):
    current_word = ''.join(current)
    if d.check(current_word)and current_word not in repeat_check:
        print(current_word)
        repeat_check.append(current_word)
        

That's it. This is all the code you need for finding all possible valid anagrams of any word. 

What if we want to find all possible words that can be formed with any number of letters from the entered word? That is easy too, thanks again to the itertools module. itertools offers a function called combinations which we can use to take small groups of letters from the main word and use the above code on them. 

The combinations function takes two attributes, the list and the number of elements to consider at a time from the list. Since we need to find all the words, we can use a for-loop of range 3 to (length of the term + 1). Now, we can apply the combinations function and the anagram solver code for each iteration to find all possible words with equal to or more than 3 letters (I have taken 3 because, in most cases, words below 3 letters are useless). 

The code would then look like this: 


import enchant
d = enchant.Dict("en_US")


word = input('Enter word: ')
letters = [chr for chr in word]
repeat_check = []

from itertools import permutations,combinations

for number in range(3,len(letters)+1): #For Loop
    for current_set in combinations(letters,number): #Combinations Function

        #Code for the Basic Anagram Finder
        
        for current in permutations(current_set):
            current_word = ''.join(current)
            if d.check(current_word)and current_word not in repeat_check:
                print(current_word)
                repeat_check.append(current_word)

Now, Python can give us all possible, valid words of any length (above 2 letters) than can be formed with the letters of the entered word. 

Let's have a look at the output: 

Enter word: code
cod
doc
dec
ode
doe
code
coed
deco
>>> 

Clearly, the code outputs all possible words that can be formed with the letters of the word code. Cool, right?

<Note> One thing we need to know about the enchant dictionary is that it takes some abbreviations to be valid words. So, you might have to do some manual removing if you do not need abbreviations. </Note> 

The fun thing about this code is that we can use it with some edits for a couple of different things. Here are some suggestions: 
  • Remove the word checker to build an anagram-maker and make an 'unscramble the word' puzzle page to give your friends a good challenge and watch them break their heads (unless they have the Anagram Solver with them).
  • Add values to each letter using a dictionary and try to build a Scrabble Word Finder. Find the total points for each word possible with your letters and print the word that will give you the highest score. 
These are just a few of the many projects you can try with minor edits to the code. Now, you can solve any anagram with your fantastic anagram solver and become the ultimate word unscrambling/Anagram solving champion, or should I say granmaa mioncpah

Make sure to follow ThePygrammer to be the first to know when a new post comes out. Just click the blue 'follow' button in the menu to follow. 

Feel free to use the comment section below to share your queries (if you have any), and I will try and get back to you as soon as possible with an answer.

If you liked this post and found it helpful, you might also like these other posts:

If you want to learn how to plot professional-looking graphs with Python, click here:

If you found the enchant module interesting and want more projects related to the enchant module, try out this simple Spelling-Checker program: 

Comments