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):
|
def readWordlist(file):
|
||||||
with open(file, 'r') as f:
|
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):
|
def getAdjacentIndexes(board, row, col):
|
||||||
maxHeight = len(board) - 1
|
maxHeight = len(board) - 1
|
||||||
@@ -34,22 +34,24 @@ def getAdjacentIndexes(board, row, col):
|
|||||||
|
|
||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix = '', path = [], foundWords = {}):
|
def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix = '', path = [], foundWords = {}, prefixes = None, maxDepth = 5, prefixLen = 0):
|
||||||
if depth == 0:
|
if depth == maxDepth:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
if prefixes != None and depth == prefixLen and prefix not in prefixes:
|
||||||
|
return {}
|
||||||
|
prefix = prefix + board[row][col]
|
||||||
|
|
||||||
foundWords = deepcopy(foundWords)
|
foundWords = deepcopy(foundWords)
|
||||||
path = deepcopy(path)
|
path = deepcopy(path)
|
||||||
|
|
||||||
prefix = prefix + board[row][col]
|
|
||||||
path.append((row, col))
|
path.append((row, col))
|
||||||
if prefix in wordlist:
|
if prefix in wordlist:
|
||||||
foundWords[prefix] = 1
|
foundWords[prefix] = 1
|
||||||
|
|
||||||
adjacents = [x for x in adjacentIndexes[(row, col)] if x not in path]
|
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:
|
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():
|
for k,v in newWords.items():
|
||||||
if k in wordlist and k in foundWords:
|
if k in wordlist and k in foundWords:
|
||||||
# Add and increment word counts
|
# Add and increment word counts
|
||||||
@@ -60,13 +62,13 @@ def findWordsByIndex(board, wordlist, row, col, depth, adjacentIndexes, prefix =
|
|||||||
|
|
||||||
return foundWords
|
return foundWords
|
||||||
|
|
||||||
def findWords(board, adjacentIndexes, maxDepth = 5):
|
def findWords(board, adjacentIndexes, maxDepth = 5, prefixes = None, prefixLen = 0):
|
||||||
words = {}
|
words = {}
|
||||||
for row in range(len(board)):
|
for row in range(len(board)):
|
||||||
for col in range(len(board[0])):
|
for col in range(len(board[0])):
|
||||||
# Find words for each row/col starting points
|
# Find words for each row/col starting points
|
||||||
wordlist = readWordlist(WORDLISTS_PATH + 'words_' + board[row][col] + '.txt')
|
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():
|
for w,v in newWords.items():
|
||||||
# Add and increment word counts
|
# Add and increment word counts
|
||||||
words[w] = words[w] + v if w in words.keys() else v
|
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 __name__ == '__main__':
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 3:
|
||||||
print('Usage: python3 blogger.py boggle_board_file.txt')
|
print('Usage: python3 blogger.py BOARD_FILE MAX_WORD_LENGTH')
|
||||||
else:
|
else:
|
||||||
board = readBoggleFile(sys.argv[1])
|
board = readBoggleFile(sys.argv[1])
|
||||||
print(board)
|
print(board)
|
||||||
@@ -85,14 +87,14 @@ if __name__ == '__main__':
|
|||||||
for c in range(len(board)):
|
for c in range(len(board)):
|
||||||
adjacentIndexes[(r,c)] = getAdjacentIndexes(board, r, c)
|
adjacentIndexes[(r,c)] = getAdjacentIndexes(board, r, c)
|
||||||
|
|
||||||
|
|
||||||
adjacentLetters = {k:[board[pos[0]][pos[1]] for pos in v] for (k,v) in adjacentIndexes.items()}
|
adjacentLetters = {k:[board[pos[0]][pos[1]] for pos in v] for (k,v) in adjacentIndexes.items()}
|
||||||
|
|
||||||
|
maxSize = int(sys.argv[2], 10)
|
||||||
maxSize = 8
|
prefixes = readWordlist('./wordlists/all/prefix4.txt')
|
||||||
words = findWords(board, adjacentIndexes, maxSize)
|
words = findWords(board, adjacentIndexes, maxSize, prefixes, 4)
|
||||||
words = [w for w in words if len(w) > 2]
|
#words = findWords(board, adjacentIndexes, maxSize)
|
||||||
for w in words:
|
for w in words:
|
||||||
print(w)
|
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