Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
agmmnn committed Jul 7, 2023
1 parent e0a25e2 commit 0e5fc48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
60 changes: 39 additions & 21 deletions src/synonym_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rich import box
from rich import print as rprint


class Synonym:
def __init__(self, word):
self.word = quote(word)
Expand All @@ -24,23 +25,27 @@ def fetch_data(self):

def parse_data(self, response_text):
soup = BeautifulSoup(response_text, "html.parser")
script = soup.find('script', {'id': 'preloaded-state'})
script = soup.find("script", {"id": "preloaded-state"})
if script is not None:
script_text = script.string
start = script_text.find('{')
end = script_text.rfind('}') + 1
start = script_text.find("{")
end = script_text.rfind("}") + 1
json_text = script_text[start:end]
self.data = json.loads(json_text)
data = json.loads(json_text)
self.data = data
return data

def extract_info(self):
if self.data:
results_data = self.data['tuna']['resultsData']
results_data = self.data["tuna"]["resultsData"]
if results_data is None:
rprint(f"\n[cayn]Oops, it looks like thesaurus.com doesn't recognize '{self.word}' as a valid word.")
rprint(
f"\n[cayn]Oops, it looks like thesaurus.com doesn't recognize '{self.word}' as a valid word."
)
return []
definition_data = results_data['definitionData']
definitions = definition_data['definitions']

definition_data = results_data["definitionData"]
definitions = definition_data["definitions"]
return self.format_definitions(definitions)
return []

Expand All @@ -56,11 +61,19 @@ def format_definitions(self, definitions):

definitions_synonyms_antonyms = []
for definition in definitions:
def_text = definition.get('definition', 'N/A')
pos = definition.get('pos', 'N/A')
synonyms = [colors[syn['similarity']] + syn['term'] + "[/]" for syn in definition.get('synonyms', [])]
antonyms = [colors[ant['similarity']] + ant['term'] + "[/]" for ant in definition.get('antonyms', [])]
definitions_synonyms_antonyms.append((f"{pos}. {def_text}", synonyms, antonyms))
def_text = definition.get("definition", "N/A")
pos = definition.get("pos", "N/A")
synonyms = [
colors[syn["similarity"]] + syn["term"] + "[/]"
for syn in definition.get("synonyms", [])
]
antonyms = [
colors[ant["similarity"]] + ant["term"] + "[/]"
for ant in definition.get("antonyms", [])
]
definitions_synonyms_antonyms.append(
(f"{pos}. {def_text}", synonyms, antonyms)
)
return definitions_synonyms_antonyms

def rich(self):
Expand All @@ -73,16 +86,21 @@ def rich(self):
def display_definitions(self, definitions_synonyms_antonyms):
console = Console()
for definition, synonyms, antonyms in definitions_synonyms_antonyms:
def_split = definition.split('.')
def_split = definition.split(".")
part_of_speech = def_split[0]
def_text = def_split[1].strip()
table = Table(box=box.SQUARE)
table.add_column("[blue]❯ " + def_text + " [gray50](" + part_of_speech + ")")
table.add_row("🔵[cyan3]Synonyms:[/cyan3] " +
"[grey50],[/grey50] ".join(synonyms))
table = Table(box=box.SQUARE, leading=1)
table.add_column(
"[blue]❯ " + def_text + " [gray50](" + part_of_speech + ")"
)
table.add_row(
"🔵[cyan3]synonyms:[/cyan3] " + "[grey50],[/grey50] ".join(synonyms)
)
if antonyms:
table.add_row("🟤[grey74]Antonyms:[/grey74] " +
"[grey74],[/grey74] ".join(antonyms))
table.add_row(
"🟤[grey74]antonyms:[/grey74] "
+ "[grey74],[/grey74] ".join(antonyms)
)
console.print(table)
console.print(
f"[grey42][link=https://www.thesaurus.com/browse/{self.word}]thesaurus.com↗[/link]",
Expand Down
9 changes: 5 additions & 4 deletions src/synonym_cli/tur.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@


def tur_main(word):
q_es = "🔵"
q_zit = "🟤"
url = "https://radyal-api.vercel.app/api/esanlam?word=" + quote(word)
r = requests.get(url)
data = r.json()
print(data["synonyms"]) if data["synonyms"] else None
print(data["antonyms"]) if data["antonyms"] else None
if not "error" in data:
print("🔵" + ", ".join(data["synonyms"])) if data["synonyms"] else None
print("🟤" + ", ".join(data["antonyms"])) if data["antonyms"] else None
else:
print(data["error"])

0 comments on commit 0e5fc48

Please sign in to comment.