dict_dl/t.py

72 lines
2.3 KiB
Python
Raw Normal View History

2022-07-08 15:42:55 +00:00
#!/bin/python
2023-09-05 14:00:29 +00:00
"""search the Merriam Webster Thesaurus with ease"""
import argparse
2022-07-08 15:42:55 +00:00
import os
2022-07-15 11:16:05 +00:00
from itertools import zip_longest
2022-07-10 03:16:16 +00:00
2022-07-15 11:16:05 +00:00
from rich.console import Console
from rich.table import Table
2022-07-08 15:42:55 +00:00
2022-07-15 11:16:05 +00:00
from dict_dl import DictFile
2023-09-05 14:00:29 +00:00
import string
2022-07-08 15:42:55 +00:00
2023-09-05 14:00:29 +00:00
letters = string.ascii_lowercase
prefix_length = 3
unusual = (
lambda prefix: not all([c in letters for c in prefix.lower()])
or len(prefix) < prefix_length
)
2022-07-15 11:16:05 +00:00
2023-09-05 14:00:29 +00:00
parser = argparse.ArgumentParser(description="Merriam Webster Thesaurus")
parser.add_argument("-p", "--preview", action="store_true", help="FZF preview")
parser.add_argument("query", type=str, help="query")
args = parser.parse_args()
prefix = args.query[:prefix_length].lower()
if unusual(prefix):
prefix = "_" * prefix_length
2022-07-15 11:16:05 +00:00
d = DictFile(os.path.expandvars(f"$DICT_DL/en_MWThesaurus/{prefix}_MWT.json"))
2023-09-05 14:00:29 +00:00
if args.preview:
for k, v in d[args.query].items():
if k == "type":
word_type = k
else:
syns = v["synonyms"]
nsyns = v["related" if "related" in v else "near synonyms"]
nants = v["near antonyms"]
ants = v["antonyms"]
print(f"> {k}")
if syns:
print(" SYNONYMS\n ", ", ".join(syns))
if nsyns:
print(" NEAR SYNONYMS\n ", ", ".join(nsyns))
if nants:
print(" NEAR ANTONYMS\n ", ", ".join(nants))
if ants:
print(" ANTONYMS\n ", ", ".join(ants))
print()
exit()
print(f"||||||||{args.query}||||||||")
2022-07-15 11:16:05 +00:00
print()
2023-09-05 14:00:29 +00:00
for k, v in d[args.query].items():
if k == "type":
word_type = k
else:
2022-07-15 11:16:05 +00:00
table = Table(title=k)
table.add_column("synonyms", justify="center", style="cyan", no_wrap=True)
table.add_column("near synonyms", justify="center", style="cyan", no_wrap=True)
table.add_column("near antonyms", justify="center", style="cyan", no_wrap=True)
table.add_column("antonyms", justify="center", style="cyan", no_wrap=True)
syns = v["synonyms"]
nsyns = v["related" if "related" in v else "near synonyms"]
ants = v["near antonyms"]
nants = v["antonyms"]
for s, ns, na, a in zip_longest(syns, nsyns, nants, ants, fillvalue=""):
table.add_row(s, ns, na, a)
console = Console()
console.print(table)