-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" |