mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Add clean text output option
- Output valid words for the provided board in a normal text format, one word per line
- Select between just word output and word+path output for easier post-processing
- Also includes sort and de-duplication options
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
"""Boggler Demo"""
|
||||
from pprint import pprint
|
||||
import argparse
|
||||
import json
|
||||
import csv
|
||||
from itertools import chain
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from .boggler_utils import BoggleBoard, build_full_boggle_tree, read_boggle_file
|
||||
@@ -16,6 +20,18 @@ parser.add_argument("wordlists", type=Path,
|
||||
the alphabet")
|
||||
parser.add_argument("max_word_length", nargs="?", type=int, default=16,
|
||||
help="Maximum length of words searched for on provided board")
|
||||
parser.add_argument("-f", "--format", type=str,
|
||||
help="Specify alternative output format including [txt, json]")
|
||||
parser.add_argument("-p", "--include-path", action="store_true", default=False,
|
||||
help="Include full paths for each word in output")
|
||||
parser.add_argument("-s", "--sort", action="store_true", default=False,
|
||||
help="Sort output alphabetically. By default the results are sorted by the starting \
|
||||
block position on the board from top-to-bottom, left-to-right as given in the \
|
||||
board file.")
|
||||
parser.add_argument("-d", "--dedup", action="store_true", default=False,
|
||||
help="Remove duplicates from word-only output. Note that de-duplication does not preserve \
|
||||
the original order of the output, so it is recommended to also use the sort option when \
|
||||
de-duplicating.")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -31,6 +47,32 @@ def main():
|
||||
try:
|
||||
boggle_tree = build_full_boggle_tree(boggle_board, args.wordlists)
|
||||
|
||||
if args.format:
|
||||
match args.format.lower():
|
||||
case "txt":
|
||||
word_paths = [start_block.word_paths for start_block in boggle_tree.values()]
|
||||
data = list(chain(*word_paths))
|
||||
if args.include_path:
|
||||
data = [f"{line[0]} {line[1]}" for line in data]
|
||||
else:
|
||||
data = [x[0] for x in data]
|
||||
if args.dedup:
|
||||
data = list(set(data))
|
||||
|
||||
if args.sort:
|
||||
data.sort()
|
||||
|
||||
for line in data:
|
||||
print(line)
|
||||
|
||||
|
||||
case "json":
|
||||
pass
|
||||
case _:
|
||||
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
||||
sys.exit()
|
||||
|
||||
else:
|
||||
print("\nBOARD")
|
||||
print(boggle_board)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user