72 lines
2.3 KiB
Python
Executable File
72 lines
2.3 KiB
Python
Executable File
#!/bin/python
|
|
"""search the Merriam Webster Thesaurus with ease"""
|
|
import argparse
|
|
import os
|
|
from itertools import zip_longest
|
|
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
|
|
from dict_dl import DictFile
|
|
import string
|
|
|
|
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
|
|
)
|
|
|
|
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
|
|
d = DictFile(os.path.expandvars(f"$DICT_DL/en_MWThesaurus/{prefix}_MWT.json"))
|
|
|
|
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}||||||||")
|
|
print()
|
|
for k, v in d[args.query].items():
|
|
if k == "type":
|
|
word_type = k
|
|
else:
|
|
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)
|