Skip to content

Commit

Permalink
[update] using shorter variable names for languages [add] adding a br…
Browse files Browse the repository at this point in the history
…and new BaseTranslator [update] Better object hashing for caches
  • Loading branch information
Animenosekai committed Mar 2, 2023
1 parent c672da4 commit e0e2cba
Show file tree
Hide file tree
Showing 22 changed files with 1,578 additions and 397 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ When something goes wrong or nothing got found, an exception **will** be raised.
>>> from translatepy import Translator
>>> translator = Translator()
>>> translator.translate("Hello", "French")
TranslationResult(service=Yandex, source=Hello, source_language=auto, destination_language=French, result=Bonjour)
TranslationResult(service=Yandex, source=Hello, source_lang=auto, dest_lang=French, result=Bonjour)
>>> translator.language("こんにちは")
LanguageResult(service=Yandex, source=こんにちは, result=Language(jpn))
```
Expand All @@ -164,21 +164,21 @@ You can use each translators separately by using them the same way as you would
>>> from translatepy.translators.google import GoogleTranslate
>>> gtranslate = GoogleTranslate()
>>> gtranslate.translate("Hello World", "Japanese")
TranslationResult(service=Google, source=Hello World, source_language=eng, destination_language=jpn, result=こんにちは世界)
TranslationResult(service=Google, source=Hello World, source_lang=eng, dest_lang=jpn, result=こんにちは世界)
```

And some translators have their own parameters:

```python
>>> gtranslate_china = GoogleTranslate(service_url="translate.google.cn")
>>> gtranslate_china.translate("Hello World", "Japanese")
TranslationResult(service=Google, source=Hello World, source_language=eng, destination_language=jpn, result=こんにちは世界)
TranslationResult(service=Google, source=Hello World, source_lang=eng, dest_lang=jpn, result=こんにちは世界)

# it can even be used by translatepy.Translator
>>> from translatepy import Translator
>>> t = Translator([gtranslate_china])
>>> t.translate("Hello World", "Japanese")
TranslationResult(service=Google, source=Hello World, source_language=eng, destination_language=jpn, result=こんにちは世界)
TranslationResult(service=Google, source=Hello World, source_lang=eng, dest_lang=jpn, result=こんにちは世界)
```

#### The Language Class
Expand Down Expand Up @@ -306,7 +306,7 @@ All of the `translatepy` errors are inherited from `translatepy.exceptions.Trans
>>> t = Translator()
>>> def translate(text, dest):
... try:
... result = t.translate(text, destination_language=dest)
... result = t.translate(text, dest_lang=dest)
except UnknownLanguage as err:
print("An error occured while searching for the language you passed in")
print("Similarity:", round(err.similarity), "%")
Expand Down
20 changes: 10 additions & 10 deletions plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ class TranslatorName(BaseTranslator):
def __init__(self, request: Request = Request()):
self.session = request

def _translate(self, text: str, destination_language: str, source_language: str) -> str:
def _translate(self, text: str, dest_lang: str, source_lang: str) -> str:
"""
This is the translating endpoint
Must return a tuple with (detected_language, result)
"""
# You could use `self.session` to make a request to the endpoint, with all of the parameters
# Then extract the detected language (or use the "source_language" parameter but what if the user pass in "auto")
# Then extract the detected language (or use the "source_lang" parameter but what if the user pass in "auto")
return detected_language, result

def _transliterate(self, text: str, destination_language, source_language: str) -> str:
def _transliterate(self, text: str, dest_lang, source_lang: str) -> str:
"""
This is the transliterating endpoint
Must return a tuple with (detected_language, result)
"""
# You could use `self.session` to make a request to the endpoint, with all of the parameters
# Then extract the detected language (or use the "source_language" parameter but what if the user pass in "auto")
# Then extract the detected language (or use the "source_lang" parameter but what if the user pass in "auto")
return detected_language, result


def _spellcheck(self, text: str, source_language: str) -> str:
def _spellcheck(self, text: str, source_lang: str) -> str:
"""
This is the spellcheking endpoint
Must return a tuple with (detected_language, result)
"""
# You could use `self.session` to make a request to the endpoint, with all of the parameters
# Then extract the detected language (or use the "source_language" parameter but what if the user pass in "auto")
# Then extract the detected language (or use the "source_lang" parameter but what if the user pass in "auto")
# result should be the original text if no correction is made or the corrected text if found
return detected_language, result

Expand All @@ -76,25 +76,25 @@ class TranslatorName(BaseTranslator):
# You could use `self.session` to make a request to the endpoint, with all of the parameters
return result

def _example(self, text: str, destination_language: str, source_language: str) -> Tuple[str, List]:
def _example(self, text: str, dest_lang: str, source_lang: str) -> Tuple[str, List]:
"""
This is the examples endpoint
Must return a tuple with (detected_language, result)
"""
# You could use `self.session` to make a request to the endpoint, with all of the parameters
# Then extract the detected language (or use the "source_language" parameter but what if the user pass in "auto")
# Then extract the detected language (or use the "source_lang" parameter but what if the user pass in "auto")
# the result should be a list of use examples
return detected_language, result

def _dictionary(self, text: str, destination_language: str, source_language: str) -> Tuple[str, List]:
def _dictionary(self, text: str, dest_lang: str, source_lang: str) -> Tuple[str, List]:
"""
This is the dictionary endpoint
Must return a tuple with (detected_language, result)
"""
# You could use `self.session` to make a request to the endpoint, with all of the parameters
# Then extract the detected language (or use the "source_language" parameter but what if the user pass in "auto")
# Then extract the detected language (or use the "source_lang" parameter but what if the user pass in "auto")
# the result should be
return detected_language, result

Expand Down
34 changes: 17 additions & 17 deletions translatepy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def main():

if args.action == 'translate':
try:
result = dl.translate(text=args.text, destination_language=args.dest_lang, source_language=args.source_lang)
result = dl.translate(text=args.text, dest_lang=args.dest_lang, source_lang=args.source_lang)
print(result.as_json(indent=4, ensure_ascii=False))
except UnknownLanguage as err:
print(dumps({
Expand Down Expand Up @@ -166,12 +166,12 @@ def main():

# INTERACTIVE VERSION
if args.action == 'shell':
destination_language = args.dest_lang
# source_language = args.source_lang
dest_lang = args.dest_lang
# source_lang = args.source_lang
try:
destination_language = translatepy.Language(destination_language)
dest_lang = translatepy.Language(dest_lang)
except Exception:
destination_language = None
dest_lang = None
while True:
answers = inquirer.prompt(actions)
action = answers["action"]
Expand All @@ -181,26 +181,26 @@ def main():
def _prompt_for_destination_language():
answers = inquirer.prompt([
inquirer.Text(
name='destination_language',
name='dest_lang',
message=INPUT_PREFIX.format(action="Select Lang.")
)
])
try:
destination_language = translatepy.Language(answers["destination_language"])
print("The selected language is " + destination_language.name)
return destination_language
dest_lang = translatepy.Language(answers["dest_lang"])
print("The selected language is " + dest_lang.name)
return dest_lang
except Exception:
print("\033[93mThe given input doesn't seem to be a valid language\033[0m")
return _prompt_for_destination_language()

if destination_language is None:
if dest_lang is None:
if action == "Translate":
print("In what language do you want to translate in?")
elif action == "Example":
print("What language do you want to use for the example checking?")
else:
print("What language do you want to use for the dictionary checking?")
destination_language = _prompt_for_destination_language()
dest_lang = _prompt_for_destination_language()

print("")
if action == "Translate":
Expand All @@ -210,8 +210,8 @@ def _prompt_for_destination_language():
if input_text == ".quit":
break
try:
result = dl.translate(input_text, destination_language, args.source_lang)
print("Result \033[90m({source} → {dest})\033[0m: {result}".format(source=result.source_language, dest=result.destination_language, result=result.result))
result = dl.translate(input_text, dest_lang, args.source_lang)
print("Result \033[90m({source} → {dest})\033[0m: {result}".format(source=result.source_lang, dest=result.dest_lang, result=result.result))
except Exception:
print_exc()
print("We are sorry but an error occured or no result got returned...")
Expand All @@ -223,8 +223,8 @@ def _prompt_for_destination_language():
if input_text == ".quit":
break
try:
result = dl.transliterate(text=input_text, destination_language=destination_language, source_language=args.source_lang)
print("Result ({lang}): {result}".format(lang=result.source_language, result=result.result))
result = dl.transliterate(text=input_text, dest_lang=dest_lang, source_lang=args.source_lang)
print("Result ({lang}): {result}".format(lang=result.source_lang, result=result.result))
except Exception:
print_exc()
print("We are sorry but an error occured or no result got returned...")
Expand All @@ -237,7 +237,7 @@ def _prompt_for_destination_language():
break
try:
result = dl.spellcheck(input_text, args.source_lang)
print("Result ({lang}): {result}".format(lang=result.source_language, result=result.result))
print("Result ({lang}): {result}".format(lang=result.source_lang, result=result.result))
except Exception:
print_exc()
print("We are sorry but an error occured or no result got returned...")
Expand Down Expand Up @@ -266,7 +266,7 @@ def _prompt_for_destination_language():
if input_text == ".quit":
break
try:
result = dl.example(input_text, destination_language, args.source_lang)
result = dl.example(input_text, dest_lang, args.source_lang)
results = []
if isinstance(result.result, list):
try:
Expand Down
30 changes: 23 additions & 7 deletions translatepy/language.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from re import compile
import re
from translatepy.utils.sanitize import remove_spaces
from typing import Union
import typing

from translatepy.exceptions import UnknownLanguage
from translatepy.utils._language_data import CODES, LANGUAGE_DATA, VECTORS
Expand All @@ -10,7 +10,10 @@
# preparing the vectors
LOADED_VECTORS = [StringVector(language, data=data) for language, data in VECTORS.items()]

LANGUAGE_CLEANUP_REGEX = compile(r"\(.+\)")
LANGUAGE_CLEANUP_REGEX = re.compile(r"\(.+\)")

# Type alias
Number = typing.Union[int, float]


class Scopes():
Expand Down Expand Up @@ -83,6 +86,10 @@ class Language():
"""

class LanguageExtra():
"""
Provides extra information on the language (limited to some languages)
"""

def __init__(self, data: dict) -> None:
self.type = Types().get(data.get("t", None))
self.scope = Scopes().get(data.get("s", None))
Expand All @@ -96,13 +103,13 @@ def as_dict(self) -> dict:
"scope": self.scope.name if self.scope is not None else None
}

def __init__(self, language: str, threshold: Union[int, float] = 93) -> None:
if language is None or remove_spaces(language) == "":
raise UnknownLanguage("N/A", 0, "You need to pass in a language")
def __init__(self, language: typing.Union[str, "Language"], threshold: Number = 93) -> None:
if isinstance(language, Language):
self.id = language.id
self.similarity = language.similarity
else:
if language is None or remove_spaces(language) == "":
raise UnknownLanguage("N/A", 0, "You need to pass in a language")
language = str(language)
normalized_language = remove_spaces(LANGUAGE_CLEANUP_REGEX.sub("", language.lower()))

Expand Down Expand Up @@ -136,15 +143,24 @@ def __init__(self, language: str, threshold: Union[int, float] = 93) -> None:
self.in_foreign_languages["en"] = self.name

def clean_cache(self) -> None:
"""
Cleans the language tokens similarity caches
"""
_languages_cache.clear()

def __repr__(self) -> str:
return "Language({language})".format(language=self.id)

def __str__(self) -> str:
"""
Returns the identifier for this language
"""
return str(self.id)

def as_dict(self, foreign: bool = True) -> dict:
"""
Returns a dictionary representation of the `Language` object
"""
return {
"id": self.id,
"alpha2": self.alpha2,
Expand All @@ -154,4 +170,4 @@ def as_dict(self, foreign: bool = True) -> dict:
"name": self.name,
"extra": self.extra.as_dict(),
"in_foreign_languages": self.in_foreign_languages if foreign else None
}
}
Loading

0 comments on commit e0e2cba

Please sign in to comment.