Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/wordhunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ def wordSearch2(start_index, G, dictionary, words, frames, prefix="", visited=No

return frames

# early stopping using trigram matching
def wordSearch3(start_index, G, dictionary, trigram_dict ,words, frames, prefix="", visited=None):
if visited==None:
visited = []

start = G.get_node_val(start_index)
prefix += start
visited.append(start_index)

if dictionary.search(prefix) and not prefix in words:
words.append(prefix)

for n in G.get_neighbours(start_index):
letter = G.get_node_val(n)
if dictionary.is_prefix(prefix + letter) and not n in visited:

if len(prefix + letter) >= 3:
trigram = (prefix + letter)[-3:]
if not trigram_dict.search(trigram):
continue

wordSearch3(start_index=n,
G=G,
dictionary=dictionary,
trigram_dict=trigram_dict,
frames=frames,
prefix=prefix,
visited=visited.copy(),
words=words)
else:
# only add meaningful frames
if not n in visited:
visited2 = visited.copy()
visited2.append(n)
frames.append((visited2, prefix + letter, words.copy()))

return frames




def solveBoard(n, G, dictionary, words):
for i in range(n * n):
wordSearch(i, G, dictionary, words)
Expand All @@ -64,6 +105,10 @@ def solveBoard2(n, G, dictionary, words, frames):
for i in range(n * n):
wordSearch2(i, G, dictionary, words, frames)

def solveBoard3(n, G, dictionary, trigram_dict ,words, frames):
for i in range(n * n):
wordSearch3(i, G, dictionary, trigram_dict, words, frames)

def createDictionary(filepath, dictionary):
f = open(filepath, 'r')
lines = f.readlines()
Expand All @@ -75,6 +120,7 @@ def createDictionary(filepath, dictionary):
board_letters = "mdacofhraueumnne"
G = create_board(board_letters, 4)
dictionary = createDictionary('vocabulary/scrabble_wordbank_2019.txt', Trie())
trigram_dict = createDictionary('vocabulary/trigram_table.txt', Trie())

# prints board
print_board(4, G)
Expand All @@ -99,6 +145,6 @@ def createDictionary(filepath, dictionary):
font_scale = 2
# frames_data = [[0, 1, 4, 9], [1, 2, 5, 9]] # JUST FOR TESTING

solveBoard2(4, G, dictionary, words, frames)
solveBoard3(4, G, dictionary, trigram_dict, words, frames)
rendered_frames = draw_frames(frames, img_dims, lett_per_row, thickness, font_face, font_scale, board_letters, font_face)
create_mp4(rendered_frames, img_dims)
Loading