top of page

Anagram Script

 

Anagram script that takes letters and creates combinations of words with length equal or less then the amount of letters provided.

Purpose: While watching a video, thought it would be interesting to create a script that can solve what the characters were doing in seconds instead of days.


Improvements To be made

  1. Ability to choose to keep words that have no definitions - Done

  2. Check for special character (input handling) - Done

  3. Option to create all words up to length

 

Modules Used
Using the built in itertools module for permutations iteration.

Enchant module for spell checking against specified dicitionary.

 

import itertools
import enchant

Using the Modules

For itertools using the permutation function to create the words of a certain length while ensuring no difference if capitals or lowercase are used then storing the word in a list.

For enchant using the english dictionary function it will check each word and see if it exists which if it does will append to another list.

words = list(itertools.permutations(letter.lower(), int(length)))
spcheck = enchant.Dict("en_US")

For Loops

Then using two for loops.

One to store all the words generated by itertools permutation.

The second to check each one with the enchant dictionary to see if it exists and if it does append it to another list.

for i in range(0, len(words)):
    #(words[i]) #each letter in list example: ('t', 'a', 'p')
    #(''.join(words[i])) #joins each letter to become the word

    allwords.append(''.join(words[i])) #add the words to a list

for w in range(0, len(allwords)):
    if spcheck.check(allwords[w]): #check if the word is an english word if true add to new list
        realwords.append(allwords[w]) #add to list2

Check and print

As a precautionary an if statement is used to check if the number of characters in the word is greater then or equal to the length stated. If so then continue while removing duplicates and printing out the list.

Else statement will print out telling you that the length input must be less then the amount of letters.

if len(letter) >= int(length):
    realwords = list(set(realwords)) #to remove duplicates and keep type as list
    print(realwords
else:
   
print("The length must be less than the amount of letters.")

bottom of page