dict_dl/t.py

37 lines
1.1 KiB
Python
Raw Normal View History

2022-07-08 15:42:55 +00:00
#!/bin/python
import os
2022-07-10 03:16:16 +00:00
import sys
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
2022-07-08 15:42:55 +00:00
2022-07-15 11:16:05 +00:00
if len(sys.argv) < 2:
query = next(sys.stdin).strip()
else:
query = sys.argv[1].strip()
prefix = query[:3]
d = DictFile(os.path.expandvars(f"$DICT_DL/en_MWThesaurus/{prefix}_MWT.json"))
print(f"||||||||{query}||||||||")
print()
2022-07-10 04:35:09 +00:00
for k, v in d[query].items():
2022-07-08 15:42:55 +00:00
if k != "type":
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)