Skip to content

Commit

Permalink
v1.0.0, Fixes: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
mebaysan committed Oct 7, 2022
1 parent af55654 commit 97a9531
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 2 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Usage](#usage)
- [translate](#translate)
- [sentence](#sentence)
- [synonym](#synonym)
- [Error Handling](#error-handling)
- [Help about parameters](#help-about-parameters)

Expand Down Expand Up @@ -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

```
Expand All @@ -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

```
Expand Down
2 changes: 2 additions & 0 deletions tureng_cli/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -10,4 +11,5 @@ def cli():

cli.add_command(translate)
cli.add_command(sentence)
cli.add_command(synonym)
cli()
2 changes: 2 additions & 0 deletions tureng_cli/cli.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -11,4 +12,5 @@ def cli():
if __name__ == "__main__":
cli.add_command(translate)
cli.add_command(sentence)
cli.add_command(synonym)
cli()
48 changes: 48 additions & 0 deletions tureng_cli/commands/synonym.py
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)
2 changes: 2 additions & 0 deletions tureng_cli/utilities/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 4 additions & 1 deletion tureng_cli/utilities/helpers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Empty file.
43 changes: 43 additions & 0 deletions tureng_cli/utilities/synonym/helpers.py
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!"

0 comments on commit 97a9531

Please sign in to comment.