Skip to content

Commit

Permalink
add subcommands to teanglann
Browse files Browse the repository at this point in the history
  • Loading branch information
sopermaf committed Dec 9, 2023
1 parent 5fec6d2 commit 1795b64
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__pycache__

.venv/
.venv*/
.vscode/settings.json
*.egg-info
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
hooks:
- id: flake8
args: ["--config=setup.cfg"]
additional_dependencies: [flake8-bugbear, flake8-pie, flake8-simplify]
additional_dependencies: [flake8-bugbear, flake8-pie, flake8-simplify, flake8-newspaper-style]

# sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date
ci:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Description

This is a script designed to return a random word in Irish
This is a library designed to return a random word in Irish
from http://www.teanglann.ie to help learns of Irish improve
their vocabulary.

Expand Down
27 changes: 24 additions & 3 deletions src/teanglann/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@
from . import words


@click.command()
@click.group(invoke_without_command=True)
@click.pass_context
@click.version_option()
def main() -> None:
def cli(ctx) -> None:
"""Teanglann."""
if ctx.invoked_subcommand is None:
random_word()


@cli.command()
def random_word():
"""Retrieve a word randomly from teanglann.ie"""
word = words.get_random_definition()
click.echo(word)


@cli.command()
@click.argument("word_to_translate", nargs=1)
def translate(word_to_translate: str):
"""Search a word in Irish and get an English definition"""
click.echo(f"Searching for translation: {word_to_translate}")
translation = words.get_translation(word_to_translate)

if translation:
click.echo(translation)
else:
click.echo("No translation found", err=True)


if __name__ == "__main__":
main(prog_name="random_irish_word") # pragma: no cover
cli(prog_name="teanglann") # pragma: no cover
91 changes: 19 additions & 72 deletions src/teanglann/words.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import random
import string
import sys

import requests
from bs4 import BeautifulSoup

letters = [char for char in string.ascii_lowercase]
probs = [
_letter_cumulative_probabilities = [
0.059927035685846036,
0.06883460804829719,
0.13853960258523307,
Expand Down Expand Up @@ -36,76 +34,48 @@
]


def write_random_word_file():
"""Get a random word and save to a txt file"""
word = get_random_definition()
with open("irish_word.txt", "w+") as word_file:
word_file.write(word)


# TODO: move this to __main__.py
def interactive_main():
"""Random word or search for -t and word"""
if "-t" in sys.argv:
word_to_trans = sys.argv[2]
translate_word(word_to_trans)
else:
word = get_random_definition()
print(word)


def get_random_definition():
def get_random_definition() -> str:
"""Get a random irish word and its definition"""
word = get_random_word()
return get_translation(word)


def get_random_word():
def get_random_word() -> str:
"""
Get a random word from http://www.teanglann.ie
in Irish. Uses a cumulative probability distribution
based on the number of entries for each letter.
Args:
None
Returns:
A string of a random Irish word without translation
A random Irish word without translation
"""
letter = random.choices(letters, probs)[0]

# Cumulative Probability Based on Num of Dictionary entries

website = "https://www.teanglann.ie/en/fgb/_" + letter
result = requests.get(website)
# get the resulting page of words for a given random letter
letter, *_ = random.choices(
string.ascii_lowercase, _letter_cumulative_probabilities
)
result = requests.get(f"https://www.teanglann.ie/en/fgb/_{letter}")
result.raise_for_status()

# pick a word randomly for the given letter
soup = BeautifulSoup(result.content, "html.parser")
samples = soup.find_all("span", class_="abcItem")
word = random.choice(samples)
return word.a.text


def translate_word(word):
translation = get_translation(word)

if not translation:
print("No translation found for %s" % word)
else:
print(translation)


def get_translation(word):
def get_translation(word: str) -> str:
"""
Get the translation of a given `word`
from http://www.teanglann.ie
Get the translation of a given `word` from http://www.teanglann.ie
Args:
word - str of Irish word
word: Irish language word
Returns:
Str of dictionary entry
dictionary definiton in English
"""
link = "https://www.teanglann.ie/en/fgb/" + word
word_result = requests.get(link)
word_result = requests.get(f"https://www.teanglann.ie/en/fgb/{word}")
word_result.raise_for_status()

word_page = BeautifulSoup(word_result.content, "html.parser")
trans = word_page.find_all("div", class_="entry")
all_trans = []
Expand All @@ -120,26 +90,3 @@ def get_translation(word):
trans += "(AUTOSEARCH) " + get_translation(word)

return trans


def get_num_entries(letter):
"""
Get number of entries that start with `letter`
in https://www.teanglann.ie.
Args:
str: first letter of entries to count
Returns:
int: number of entries
"""
website = "https://www.teanglann.ie/en/fgb/_" + letter
result = requests.get(website)
soup = BeautifulSoup(result.content, "html.parser")
samples = soup.find_all("span", class_="abcItem")
return len(samples)


if __name__ == "__main__":
interactive_main()

0 comments on commit 1795b64

Please sign in to comment.