Jumbled words solver using python

In this article, we shall see how to build a jumbled word solver using python. What this code basically does it takes a jumbled word and returns all the possible original words.

How does it work?

This code consists of the following two important steps.

  1. Find all the permutations of the word.
  2. Check if each permutation matches with our English word dataset.

Let us look at each step one by one.

Find all permutations of the word

Python has a built-in module called itertools. This module has a class called permutations. This class will take a string as an argument and return a itertools.permutations. This object can be iterated over. Each iteration will give us a tuple of letters.

from itertools import permutations
combinations = permutations('abc')

for combination in combinations:
    print(combination)

The output of the above code is,

('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')

As you can see I have given the string abc as my input. We now have all the possible combinations of the string abc but as a tuple.

We can use the join() method to form a string from these tuple of alphabets.

from itertools import permutations
combinations = permutations('abc')

for combination in combinations:
    print(''.join(combination))

After using the join method our output becomes something like this.

abc
acb
bac
bca
cab
cba

2. Check if each permutation matches with our English word dataset

Next important thing is we need a dataset of all the possible english words. This where the nltk python library comes to the rescue.

what is nltk?

The Natural Language Toolkit, or more commonly NLTK, is a suite of libraries and programs for symbolic and statistical natural language processing for English written in the Python programming language.

We have to install this library first. Just like every other library we can install the nltk library from the pypi.org using pip as

pip install nltk

After installing this we have to download the dataset. Open the python interpreter and run the following commands.

import nltk
nltk.download('words')

This will download the words dataset. You can also create a new python program, copy paste the code and run this.

We can access these words in our python program by,

from nltk.corpus import words
setofwords = set(words.words())

The setofwords will have a set of all the words. We are performing the set operation to get all the unique words alone. Total number of words in the setofwords variable is 235892.

We can then check if each of the combination is in this set of words. If it exists we can assume that the combination is a valid English word.

The complete code

The complete code will look like this.

from itertools import permutations
from nltk.corpus import words

setofwords = set(words.words())
options = permutations('dilch')
for i in options:
    word = ''.join(i)
    if word in setofwords:
        print(word)

I have jumbled the word child as dilch and given it as input. The output of the above code is,

child

We can test this with more examples.

from itertools import permutations
from nltk.corpus import words

setofwords = set(words.words())

options = permutations('ptercomu')
for i in options:
    word = ''.join(i)
    if word in setofwords:
        print(word)

The output of this code is,

computer
from itertools import permutations
from nltk.corpus import words

setofwords = set(words.words())

options = permutations('lebat')
for i in options:
    word = ''.join(i)
    if word in setofwords:
        print(word)

If there are multiple possibilities they are also listed one by one like this.

bleat
blate
batel
table

Conclusion

Hope this article is helpful. Although It is not the best way to find the jumbled words, go ahead and try doing this in different ways to optimise the time complexity.

Happy coding!