diff --git a/README.md b/README.md index b45b96a..918b464 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ - [Usage](#usage) - [translate](#translate) - [sentence](#sentence) + - [synonym](#synonym) - [Error Handling](#error-handling) - [Help about parameters](#help-about-parameters) @@ -114,7 +115,7 @@ Meanings of "hurricane" in Turkish English Dictionary : 15 result(s) This command is using for showing sentence examples. There are basically 2 parameters by using the CLI: -- `-w` or `--word` for words to translate in both language Turkish or English +- `-w` or `--word` for words to get the related sentences with it - `-n` or `--n-results` to limit the result of the translation rows ``` @@ -135,6 +136,26 @@ Hurricane sentence example ``` + +## synonym + +This command is using for showing synonyms of the word. + +There are basically 2 parameters by using the CLI: +- `-w` or `--word` for words to get related synonyms +- `-n` or `--n-results` to limit the result of the translation rows + +``` +>>> python -m tureng_cli synonym -w "Hurricane" -n 3 + +Hurricane synonyms +# Synonym Defination +1 + cyclone + (Meteorol.) A system of rotating winds over a vast area, spinning inward to a low pressure center (counterclockwise in the N Hemisphere) and generally causing stormy weather: commonly called a low, since it coexists with low barometric pressure +2 + typhoon + A violent cyclonic storm occurring in the western Pacific Ocean. +3 + wind + The wind instruments of an orchestra, or the players of these instruments + +``` + ## Error Handling ``` diff --git a/tureng_cli/__main__.py b/tureng_cli/__main__.py index 48d7935..b1ee8ba 100644 --- a/tureng_cli/__main__.py +++ b/tureng_cli/__main__.py @@ -1,6 +1,7 @@ import click from tureng_cli.commands.translate import translate from tureng_cli.commands.sentence import sentence +from tureng_cli.commands.synonym import synonym @click.group() @@ -10,4 +11,5 @@ def cli(): cli.add_command(translate) cli.add_command(sentence) +cli.add_command(synonym) cli() diff --git a/tureng_cli/cli.py b/tureng_cli/cli.py index 2cde0c2..1ab6ea0 100755 --- a/tureng_cli/cli.py +++ b/tureng_cli/cli.py @@ -1,6 +1,7 @@ import click from tureng_cli.commands.translate import translate from tureng_cli.commands.sentence import sentence +from tureng_cli.commands.synonym import synonym @click.group() @@ -11,4 +12,5 @@ def cli(): if __name__ == "__main__": cli.add_command(translate) cli.add_command(sentence) + cli.add_command(synonym) cli() diff --git a/tureng_cli/commands/synonym.py b/tureng_cli/commands/synonym.py new file mode 100644 index 0000000..ce3f900 --- /dev/null +++ b/tureng_cli/commands/synonym.py @@ -0,0 +1,48 @@ +import click +import requests +from bs4 import BeautifulSoup +from tureng_cli.utilities.constants import CRACKER_HEADER +from tureng_cli.utilities.helpers import get_english_synonym_url +from tureng_cli.utilities.synonym.helpers import ( + extract_synonym_header_text, + extract_synonym_table_headers, + extract_synonym_table_rows, + check_is_there_synonym_result, + extract_synonym_no_synonym_header_text, + extract_synonym_no_synonym_possible_words, +) + + +@click.command() +@click.option( + "-w", + "--word", + prompt="Word (English)", + help="A word to show the synonyms that are created by using the word.", +) +@click.option("-n", "--n-result", prompt="N Rows", help="N rows to show.", default=15) +def synonym(word, n_result): + """Command to show the synonyms + + Args: + word (str): English word + n (int): The range of the list of synonymd results + """ + target_url = get_english_synonym_url(word) + res = requests.get(target_url, headers=CRACKER_HEADER) + soup = BeautifulSoup(res.text, "html.parser") + if check_is_there_synonym_result(soup): + header_text = extract_synonym_header_text(soup) + table_header_text = extract_synonym_table_headers(soup) + table_rows_texts = extract_synonym_table_rows(soup, word) + + # echo outputs + click.echo(header_text) + click.echo(table_header_text) + click.echo("\n".join(table_rows_texts[:n_result])) + else: + + header_text = extract_synonym_no_synonym_header_text(soup, word) + possible_words_list = extract_synonym_no_synonym_possible_words(soup) + click.echo(header_text) + click.echo(possible_words_list) diff --git a/tureng_cli/utilities/constants.py b/tureng_cli/utilities/constants.py index 407cb23..73c9fef 100644 --- a/tureng_cli/utilities/constants.py +++ b/tureng_cli/utilities/constants.py @@ -2,6 +2,8 @@ SENTENCE_URL_EN = "https://sentence.yourdictionary.com/" +SYNONYM_URL_EN = "https://thesaurus.yourdictionary.com/" + CRACKER_HEADER = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" } diff --git a/tureng_cli/utilities/helpers.py b/tureng_cli/utilities/helpers.py index e985bfc..de99b1f 100644 --- a/tureng_cli/utilities/helpers.py +++ b/tureng_cli/utilities/helpers.py @@ -1,4 +1,4 @@ -from .constants import BASE_URL_TR,SENTENCE_URL_EN +from .constants import BASE_URL_TR, SENTENCE_URL_EN, SYNONYM_URL_EN def get_turkish_translate_url(word): @@ -8,3 +8,6 @@ def get_turkish_translate_url(word): def get_english_sentence_url(word): return SENTENCE_URL_EN + word + +def get_english_synonym_url(word): + return SYNONYM_URL_EN + word diff --git a/tureng_cli/utilities/synonym/__init__.py b/tureng_cli/utilities/synonym/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tureng_cli/utilities/synonym/helpers.py b/tureng_cli/utilities/synonym/helpers.py new file mode 100644 index 0000000..32df4ee --- /dev/null +++ b/tureng_cli/utilities/synonym/helpers.py @@ -0,0 +1,43 @@ +def extract_synonym_header_text(soup): + # get header text + header = soup.find("h1", class_="source-heading") + header_text = header.string + return header_text + + +def extract_synonym_table_headers(soup): + table_header_text = " \t".join(["#", "Synonym", "Defination"]) + return table_header_text + + +def extract_synonym_table_rows(soup, word): + # get table + table = soup.find_all("div", class_="single-synonym-wrapper")[0] + rows = table.find_all("div", class_="single-synonym") + + # table rows + table_rows_texts = [] + for table_row in rows: + row_id = len(table_rows_texts) + 1 + row_synonym = table_row.find_all("div", class_="synonym-link-wrapper")[0].text + row_defination = table_row.find_all("span")[0].text + row_text = "\t+ ".join([str(row_id), row_synonym, row_defination]) + table_rows_texts.append(row_text) + + return table_rows_texts + + +def check_is_there_synonym_result(soup): + # get tables to check is there a result table + result = soup.find_all("div", class_="single-synonym-wrapper") + return True if len(result) >= 1 else False + + +def extract_synonym_no_synonym_header_text(soup, word): + # get header text + header_text = f"synonym examples for: {word}" + return header_text + + +def extract_synonym_no_synonym_possible_words(soup): + return "No results found!"