Figure out all the 5 letter words (part 1)

Published April 27, 2022, 1:15 p.m.

Welcome to the first *real* tutorial about making a Wordle game with python!  To make a game like this you will need a few things.

  1. A list of 5 letter words
  2. A way to parse through words to discern right, wrong or misplaced letters
  3. A way to color the screen to display the output from #2

To get started we need a list of 5 letter words.  We can find what we want on google if we like. (Careful, you can also find future solutions to all the official wordle games!).  However, why not make the words in python?

I am a big believer in revealing the process.  Therefore, I am going to take you through one of my failures in the off-chance that some of the things I show can be useful at some point for you.  Certainly there are myriad ways to get a list of english dictionary 5-letter words.  Some will be much better than others.  First I will show you a terrible way to achieve that end.  Then I will show you a much better way. 

In essence, the "terrible" way is to generate a list of letters, have python perform all 5 letter permutations of those letters and check them against a dictionary to see if they are real words.  This is wasteful and time consuming but it shows off a python ability to generate permutations of items in a list that may be valuable for some application in the future.

The dictionary that has a convenient word-checking feature comes from a package called pyenchant.  If you don't have that package, you will open a terminal window and install it with pip.

>>> pip install pyenchant

This package contains some word checking tools.  It is odd (to me) that the package is called 'pyenchant' but in the code when you want to import it you simply import 'enchant'.

While we are importing things we may as well import another package that we will soon use called nltk (stands for Natural Language Toolkit).

>>> pip install nltk

Once those are installed we can get rolling!  We are going to make a new file called "make_the_words.py".  At the top of our code we will import a few items:

from itertools import permutations
import enchant
import string

The string import conveniently hosts lists of letters that can be called upon.  My crazy idea was that most 5 letter words would only have 2 of the same consonents and possibly 3 of the same vowels.  So all I needed to do was generate a list of the alphabet twice and add to it another list of vowels.

alphabet = list(string.ascii_lowercase)
vowels = list("aeiouy")
alphabet += alphabet
alphabet += vowels

The final alphabet list looks thus:

>>> alphabet
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'e', 'i', 'o', 'u', 'y']
>>>

Sorting the list confirms that we have the letters we want:

>>> alphabet.sort()
>>> alphabet
['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h', 'i', 'i', 'i', 'j', 'j', 'k', 'k', 'l', 'l', 'm', 'm', 'n', 'n', 'o', 'o', 'o', 'p', 'p', 'q', 'q', 'r', 'r', 's', 's', 't', 't', 'u', 'u', 'u', 'v', 'v', 'w', 'w', 'x', 'x', 'y', 'y', 'y', 'z', 'z']
>>>

Now, you might already realize that even with this absurd convention we will miss words.  You might even say it was an "error" on my part that the word "error" would not be generated from this list since "error" has three 'r's and my list only has two.  Other inefficiencies are present (no need for two 'q's (probably)).

Moving on. Attempting permutations with this many letter would grind my computer to a halt.  To show you the process, I will just use a smaller subset of letters:

my_name = list("charles")
perm_list = ["".join(y) for y in list(permutations(my_name,5))]

This has created a list of all 5 letter permuations of the seven letters in my name.  Most of these letter combos are gibberish.  However, some of them are real words.

Next we can invoke our enchant dictionary.  We want to use the "en_US" variety of the dictionary:

d=enchant.Dict('en_US')

This has a nice feature called "check" which allows one to check a string to see if it is a dictionary word.

>>> d=enchant.Dict('en_US')
>>> d.check("word")
True
>>> d.check("asdfg")
False
>>>

What we now want to do is to check all of the letter combos in our "perm_list" against the dictionary and only look at the words that pass the test:

 

for word in perm_list:
    if d.check(word):
        print(word)

This then prints to the screen all the letter combos that check out as real english dictionary words.  Some of my favorite words were: laser, clash, crash, cesar.  Some of my least favorite were: leach, aches.  Now this appeared to run pretty quickly but remember we were only using 7 letters rather than 60 or so.  It runs *waaaaay* slower with that many letters to chew on.  Anyway, that is the end of this video.  Watch the next one to see the "good" way to get all the 5 letter words!  Thanks for watching!

skip_nextFigure out all the 5 letter words (part 2)
  • Introduction to PyWordle

  • Get the latest version of Python (3.10.4)

  • Figure out all the 5 letter words (part 1)
    (currently viewing)
  • Figure out all the 5 letter words (part 2)

  • Color letters with colorama!

  • Get words from our word list!

  • Select a Random Wordle

  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory