mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-26 03:29:26 -04:00
Add JSON output option
This commit is contained in:
+29
-35
@@ -44,47 +44,41 @@ def main():
|
|||||||
print("Invalid MAX_WORD_LENGTH. Please try again with a valid integer.")
|
print("Invalid MAX_WORD_LENGTH. Please try again with a valid integer.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
try:
|
boggle_tree = build_full_boggle_tree(boggle_board, args.wordlists)
|
||||||
boggle_tree = build_full_boggle_tree(boggle_board, args.wordlists)
|
|
||||||
|
|
||||||
if args.format:
|
if args.format:
|
||||||
match args.format.lower():
|
match args.format.lower():
|
||||||
case "txt":
|
case "txt":
|
||||||
word_paths = [start_block.word_paths for start_block in boggle_tree.values()]
|
word_paths = [start_block.word_paths for start_block in boggle_tree.values()]
|
||||||
data = list(chain(*word_paths))
|
data = list(chain(*word_paths))
|
||||||
if args.include_path:
|
if args.include_path:
|
||||||
data = [f"{line[0]} {line[1]}" for line in data]
|
data = [f"{line[0]} {line[1]}" for line in data]
|
||||||
else:
|
else:
|
||||||
data = [x[0] for x in data]
|
data = [x[0] for x in data]
|
||||||
if args.dedup:
|
if args.dedup:
|
||||||
data = list(set(data))
|
data = list(set(data))
|
||||||
|
|
||||||
if args.sort:
|
if args.sort:
|
||||||
data.sort()
|
data.sort()
|
||||||
|
|
||||||
for line in data:
|
for line in data:
|
||||||
print(line)
|
print(line)
|
||||||
|
|
||||||
|
case "json":
|
||||||
|
data = { str(k):v.word_paths for k,v in boggle_tree.items()}
|
||||||
|
print(json.dumps(data, indent=2, sort_keys=True))
|
||||||
|
case _:
|
||||||
|
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
case "json":
|
else:
|
||||||
pass
|
print("\nBOARD")
|
||||||
case _:
|
print(boggle_board)
|
||||||
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
else:
|
for start_pos, tree in boggle_tree.items():
|
||||||
print("\nBOARD")
|
print(f"\nStarting @ {start_pos}...")
|
||||||
print(boggle_board)
|
for word in tree.word_paths:
|
||||||
|
print(f"{word[0]: <{boggle_board.max_word_len}}: {word[1]}")
|
||||||
for start_pos, tree in boggle_tree.items():
|
|
||||||
print(f"\nStarting @ {start_pos}...")
|
|
||||||
for word in tree.word_paths:
|
|
||||||
print(f"{word[0]: <{boggle_board.max_word_len}}: {word[1]}")
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
print("The [MAX_WORD_LENGTH] argument must be an integer.")
|
|
||||||
print("Please try again.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'''Boggler Utils'''
|
'''Boggler Utils'''
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
#from pathlib import Path
|
||||||
from os import path
|
from os import path
|
||||||
from multiprocessing import Pool
|
from multiprocessing import Pool
|
||||||
import logging as log
|
import logging as log
|
||||||
@@ -12,7 +13,7 @@ class BoardCell:
|
|||||||
adjacent_cells: list[BoardCell] = None) -> BoardCell:
|
adjacent_cells: list[BoardCell] = None) -> BoardCell:
|
||||||
self.__row: int = row
|
self.__row: int = row
|
||||||
self.__col: int = col
|
self.__col: int = col
|
||||||
self.__pos: (int, int) = (self.__row, self.__col)
|
self.__pos: tuple(int, int) = (self.__row, self.__col)
|
||||||
self.__letters: str = letters
|
self.__letters: str = letters
|
||||||
self.__adjacent_cells: list[BoardCell] = adjacent_cells
|
self.__adjacent_cells: list[BoardCell] = adjacent_cells
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ class BoardCell:
|
|||||||
return self.__col
|
return self.__col
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def pos(self) -> (int, int):
|
def pos(self) -> tuple[int, int]:
|
||||||
'''Getter for pos property'''
|
'''Getter for pos property'''
|
||||||
return self.__pos
|
return self.__pos
|
||||||
|
|
||||||
@@ -168,7 +169,7 @@ class WordNode:
|
|||||||
return self.__parent
|
return self.__parent
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def board_pos(self) -> (int, int):
|
def board_pos(self) -> tuple[int, int]:
|
||||||
'''Getter for board_pos property'''
|
'''Getter for board_pos property'''
|
||||||
return self.__board_pos
|
return self.__board_pos
|
||||||
|
|
||||||
@@ -253,7 +254,10 @@ class WordTree:
|
|||||||
self.__word_paths = value
|
self.__word_paths = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ", ".join(self.wordlist)
|
return ", ".join([str(x) for x in self.word_paths])
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
def insert_node(self, letters: str, parent: WordNode, is_word: bool = False,
|
def insert_node(self, letters: str, parent: WordNode, is_word: bool = False,
|
||||||
children: dict[str, WordNode] = None, board_pos = None):
|
children: dict[str, WordNode] = None, board_pos = None):
|
||||||
@@ -408,6 +412,15 @@ def read_boggle_file(file):
|
|||||||
with open(file, 'r', encoding='utf-8') as file:
|
with open(file, 'r', encoding='utf-8') as file:
|
||||||
return [x.rstrip().split(',') for x in file.readlines()]
|
return [x.rstrip().split(',') for x in file.readlines()]
|
||||||
|
|
||||||
|
def write_wordlist(filename: Path, filetype: str = "txt", sort: bool = False):
|
||||||
|
match filetype:
|
||||||
|
case "txt":
|
||||||
|
pass
|
||||||
|
case "json":
|
||||||
|
pass
|
||||||
|
case _:
|
||||||
|
pass
|
||||||
|
|
||||||
def find_paths_by_word(board_letters, dictionary_path, max_len):
|
def find_paths_by_word(board_letters, dictionary_path, max_len):
|
||||||
'''Return list of paths by word'''
|
'''Return list of paths by word'''
|
||||||
boggle_board = BoggleBoard(board_letters, max_len)
|
boggle_board = BoggleBoard(board_letters, max_len)
|
||||||
|
|||||||
Reference in New Issue
Block a user