mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Check word prefixes to reduce problem space
Produce a prefix wordlist from a dictionary wordlist using the `filter_prefix.sh` script.
This commit is contained in:
+18
-16
@@ -9,7 +9,7 @@ def readBoggleFile(file):
|
||||
|
||||
def readWordlist(file):
|
||||
with open(file, 'r') as f:
|
||||
return {k:0 for k in f.read().split()}
|
||||
return {k:1 for k in f.read().split()}
|
||||
|
||||
def getAdjacentIndexes(board, row, col):
|
||||
maxHeight = len(board) - 1
|
||||
@@ -34,22 +34,24 @@ def getAdjacentIndexes(board, row, col):
|
||||
|
||||
return indexes
|
||||
|
||||
def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix = '', path = [], foundWords = {}):
|
||||
if depth == 0:
|
||||
def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix = '', path = [], foundWords = {}, prefixes = None, maxDepth = 5, prefixLen = 0):
|
||||
if depth == maxDepth:
|
||||
return {}
|
||||
|
||||
if prefixes != None and depth == prefixLen and prefix not in prefixes:
|
||||
return {}
|
||||
prefix = prefix + board[row][col]
|
||||
|
||||
foundWords = deepcopy(foundWords)
|
||||
path = deepcopy(path)
|
||||
|
||||
prefix = prefix + board[row][col]
|
||||
path.append((row, col))
|
||||
if prefix in wordlist:
|
||||
foundWords[prefix] = 1
|
||||
|
||||
adjacents = [x for x in adjacentIndexes[(row, col)] if x not in path]
|
||||
#print('adj:', adjacents, 'p:', (row,col), 'pre:', prefix)
|
||||
for a in adjacents:
|
||||
newWords = findWordsByIndex(board, wordlist, a[0], a[1], depth-1, adjacentIndexes, prefix, path, foundWords)
|
||||
newWords = findWordsByIndex(board, wordlist, a[0], a[1], depth+1, adjacentIndexes, prefix, path, foundWords, prefixes, maxDepth, prefixLen)
|
||||
for k,v in newWords.items():
|
||||
if k in wordlist and k in foundWords:
|
||||
# Add and increment word counts
|
||||
@@ -60,13 +62,13 @@ def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix =
|
||||
|
||||
return foundWords
|
||||
|
||||
def findWords(board, adjacentIndexes, maxDepth = 5):
|
||||
def findWords(board, adjacentIndexes, maxDepth = 5, prefixes = None, prefixLen = 0):
|
||||
words = {}
|
||||
for row in range(len(board)):
|
||||
for col in range(len(board[0])):
|
||||
# Find words for each row/col starting points
|
||||
wordlist = readWordlist(WORDLISTS_PATH + 'words_' + board[row][col] + '.txt')
|
||||
newWords = findWordsByIndex(board, wordlist, row, col, maxDepth, adjacentIndexes)
|
||||
newWords = findWordsByIndex(board, wordlist, row, col, 0, adjacentIndexes, prefixes = prefixes, maxDepth = maxDepth, prefixLen = prefixLen)
|
||||
for w,v in newWords.items():
|
||||
# Add and increment word counts
|
||||
words[w] = words[w] + v if w in words.keys() else v
|
||||
@@ -75,8 +77,8 @@ def findWords(board, adjacentIndexes, maxDepth = 5):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 2:
|
||||
print('Usage: python3 blogger.py boggle_board_file.txt')
|
||||
if len(sys.argv) != 3:
|
||||
print('Usage: python3 blogger.py BOARD_FILE MAX_WORD_LENGTH')
|
||||
else:
|
||||
board = readBoggleFile(sys.argv[1])
|
||||
print(board)
|
||||
@@ -85,14 +87,14 @@ if __name__ == '__main__':
|
||||
for c in range(len(board)):
|
||||
adjacentIndexes[(r,c)] = getAdjacentIndexes(board, r, c)
|
||||
|
||||
|
||||
adjacentLetters = {k:[board[pos[0]][pos[1]] for pos in v] for (k,v) in adjacentIndexes.items()}
|
||||
|
||||
|
||||
maxSize = 8
|
||||
words = findWords(board, adjacentIndexes, maxSize)
|
||||
words = [w for w in words if len(w) > 2]
|
||||
maxSize = int(sys.argv[2], 10)
|
||||
prefixes = readWordlist('./wordlists/all/prefix4.txt')
|
||||
words = findWords(board, adjacentIndexes, maxSize, prefixes, 4)
|
||||
#words = findWords(board, adjacentIndexes, maxSize)
|
||||
for w in words:
|
||||
print(w)
|
||||
#print([w for w in words if len(w) == 3])
|
||||
|
||||
#words = [w for w in words if len(w) > 2]
|
||||
#print([w for w in words if len(w) == 3])
|
||||
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
|
||||
if [ -z $1 ] || [ -z $2 ]; then
|
||||
echo "Usage: ./filter_prefix.sh WORDLIST PREFIX_LENGTH"
|
||||
exit 0
|
||||
else
|
||||
cat "$1" | pcregrep -o1 "^([a-zA-Z]{$2})" | uniq
|
||||
exit 0
|
||||
fi
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user