Literary Scrabble with Python
Literary scrabble is a game of scrabble where you can only play words that appear in selected literary classics. Every word that is played must also appear in the SOWPODS list. In this article, a simulation of the game is implemented in python.
Side note: All the literary classics can be found here. Clone and enjoy.
Task 1
Create a function that takes a literary classic as an argument and creates a sorted word list (descending order) that contains no duplicates.
To create the word list (requirements):
- All characters must be converted to uppercase.
- All hyphens and underscores must be replaced with a single space.
- All hyphenated words must be split into separate words.
- All contractions and possessives must be stripped off.
- All punctuations, whitespace characters, and digits must be removed.
- Each word must also occur in the official SOWPODS list.
Example
The create_wordlist function takes the location of a literary classic and returns a list of sorted unique words in the classic.
Creating the function
It is always a good idea to create a docstring for your python functions. It improves the understanding of your function's functionality.
Intuition
The text file contains multiple lines of text and each line is made up of multiple texts or strings. Every line can be viewed as a sentence and since sentences are made up of spaces, it is easy to extract the words that make up the sentence using the split() method of strings in python.
So for each line, sentences are broken down into individual words, and each word is made to satisfy the requirements above.
Task 2
Check if a word can be formed from some letters. Note that each letter can be used only once, like in a real scrabble game.
Example,
Note how 'TOEE' cannot be formed from 'AEOPT' but 'TOE' can.
Creating the function
Intuition
Create a list of letters. If any character appears in both a word and the list of letters, remove that character from the list of letters.
Task 3
Calculate the word score of a scrabble word.
Example
The scrabble letter distribution which can be found here is needed to create this function.
Creating the function
Intuition
Create a dictionary for the letter distribution (letters as keys and scores as values). For any scrabble word, find the sum of scores of the individual letters.
Task 4
Create a function that returns a dictionary of valid words created from some letters provided.
Example
With the letters 'AEOPT' what valid words can be created?
Creating the function
Intuition
The word_match function checks if a word can be created from some letters. The word_score function calculates the score of words.
Bringing it all together: The Fun parts
Given some letters and a literary classic, what is the highest scoring word you can play?
Example
Creating the function
Intuition
The create_wordlist() function creates a word list from a literary classic. The find_words() function returns a dictionary of valid words and their individual scores. The only thing left to do is return the word with the highest score, nothing a sorting algorithm can't do.
Concluding...
You've just played all of your letters. According to the rules of literary scrabble, if you're out of letters you can choose to switch to a new literary novel but you have to do so before you choose your new letters.
You've noticed that no one has played "Q" or "Z" for a while, which may mean there are still some left. This is good since these are high-value letters, but not good if your next story does not have many words that contain those letters.
For some literary classic, how do you count words which contain special characters like "Q" or "Z"?
Example
Given a list of literary classics, count the number of words that contain a "Q" or a "Z".
Creating the function...
Intuition
Use the create_wordlist() function to break a classic into a list of words. Find out if each word contains a "Q" or a "Z" and increment some counter accordingly.