Handle blocks with empty strings and non-existant wordlists

This commit is contained in:
2022-09-20 22:45:57 -04:00
parent 96da732dd9
commit 8f75604d15
+14 -6
View File
@@ -197,9 +197,9 @@ class WordNode:
class WordTree: class WordTree:
'''A tree populated by WordNode(s) to complete words from a given root letter and wordlist''' '''A tree populated by WordNode(s) to complete words from a given root letter and wordlist'''
def __init__(self, alphabet: str, root: WordNode, words: list[str] = None, def __init__(self, alphabet: list, root: WordNode, words: list[str] = None,
max_word_len = 16) -> WordTree: max_word_len = 16) -> WordTree:
self.__alphabet: str = alphabet self.__alphabet: list = alphabet
self.__wordlist: list[str] = words self.__wordlist: list[str] = words
self.__root: WordNode = root self.__root: WordNode = root
self.__max_word_len: int = max_word_len self.__max_word_len: int = max_word_len
@@ -216,7 +216,7 @@ class WordTree:
self.__insert_word(word) self.__insert_word(word)
@property @property
def alphabet(self) -> str: def alphabet(self) -> list:
'''Getter for alphabet property''' '''Getter for alphabet property'''
return self.__alphabet return self.__alphabet
@@ -278,7 +278,7 @@ class WordTree:
continue continue
try: try:
alpha_index = [x[0] for x in self.alphabet].index(letter) alpha_index = self.alphabet.index(letter)
except ValueError: # letters not in given alphabet except ValueError: # letters not in given alphabet
return False return False
@@ -383,14 +383,22 @@ def build_full_boggle_tree(board: BoggleBoard, wordlist_path: str) -> dict[str,
print("Reading in wordlists...") print("Reading in wordlists...")
for letters in alphabet: for letters in alphabet:
if letters[0] in index: if letters == "":
# Skip wordlist read for blocks with empty string
index[""] = {}
continue
elif letters[0] in index:
index[letters] = index[letters[0]] index[letters] = index[letters[0]]
continue continue
filename = "words_" + letters[0] + ".txt" filename = "words_" + letters[0] + ".txt"
print(f">> {letters}: {filename}") try:
wordlist = read_wordlist(path.join(path.abspath(wordlist_path), filename)) wordlist = read_wordlist(path.join(path.abspath(wordlist_path), filename))
index[letters] = wordlist index[letters] = wordlist
print(f">> {letters}: {filename}")
except FileNotFoundError:
print(f">> {letters}: -- Skipping -- no wordlist found for {letters}")
index[letters] = {}
print("Generating WordTrees...") print("Generating WordTrees...")
params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()] params = [ [alphabet, board, cell, index[cell.letters] ] for cell in board.board.values()]