Skip to content

Commit

Permalink
[add] Implementing translator choice from #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Animenosekai committed Feb 14, 2021
1 parent 971c309 commit 8758dac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
25 changes: 16 additions & 9 deletions translatepy/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from translatepy.translators.bing import BingTranslate
from translatepy.translators.yandex import YandexTranslate
from translatepy.translators.reverso import ReversoTranslate
from translatepy.translators.unselected import Unselected
#"""
"""DEBUG
from models.languages import Language
Expand Down Expand Up @@ -48,11 +49,11 @@ class Translator():
"""
A class which groups all of the APIs
"""
def __init__(self, yandex_sid_refresh=False) -> None:
self.google_translate = GoogleTranslate()
self.yandex_translate = YandexTranslate(sid_refresh=yandex_sid_refresh)
self.bing_translate = BingTranslate()
self.reverso_translate = ReversoTranslate()
def __init__(self, use_google=True, use_yandex=True, use_bing=True, use_reverso=True, yandex_sid_refresh=False) -> None:
self.google_translate = (GoogleTranslate() if use_google else Unselected())
self.yandex_translate = (YandexTranslate(sid_refresh=yandex_sid_refresh) if use_yandex else Unselected())
self.bing_translate = (BingTranslate() if use_bing else Unselected())
self.reverso_translate = (ReversoTranslate() if use_reverso else Unselected())

def translate(self, text, destination_language, source_language=None):
global TRANSLATION_CACHES
Expand All @@ -78,7 +79,7 @@ def translate(self, text, destination_language, source_language=None):
lang, response = self.reverso_translate.translate(text, destination_language, source_language)
if response is None:
lang, response = self.yandex_translate.translate(text, destination_language, source_language)
if response is None:
if response is None and isinstance(self.yandex_translate, Unselected):
return None
try:
lang = Language(lang)
Expand Down Expand Up @@ -122,7 +123,8 @@ def transliterate(self, text, source_language=None):
return TRANSLITERATION_CACHES[_cache_key]

lang, response = self.yandex_translate.transliterate(text, source_language)
if response is None:

if response is None and isinstance(self.yandex_translate, Unselected):
return None

try:
Expand Down Expand Up @@ -152,6 +154,8 @@ def spellcheck(self, text, source_language=None):
try:
lang = Language(lang)
except: pass
if response is None and isinstance(self.yandex_translate, Unselected):
return None

SPELLCHECK_CACHES[str({"t": str(text), "s": str(source_language)})] = response
SPELLCHECK_CACHES[str({"t": str(text), "s": str(lang)})] = response
Expand Down Expand Up @@ -187,8 +191,8 @@ def language(self, text):
response = self.reverso_translate.language(text)
if response is None:
response = self.yandex_translate.language(text)
if response is None:
return response
if response is None and isinstance(self.yandex_translate, Unselected):
return None
else:
try:
response = Language(response)
Expand Down Expand Up @@ -232,6 +236,9 @@ def example(self, text, destination_language, source_language=None):
return EXAMPLE_CACHES[_cache_key]

lang, response = self.bing_translate.example(text, destination_language, source_language)
if response is None and isinstance(self.bing_translate, Unselected):
return None

try:
lang = Language(lang)
except: pass
Expand Down
24 changes: 24 additions & 0 deletions translatepy/translators/unselected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Unselected():
"""
When the user decides not to use the translator
"""
def __init__(self, *args, **kwargs) -> None:
pass

def translate(self, *args, **kwargs):
return None, None

def transliterate(self, *args, **kwargs):
return None, None

def spellcheck(self, *args, **kwargs):
return None, None

def language(self, *args, **kwargs):
return None

def example(self, *args, **kwargs):
return None, None

def __repr__(self) -> str:
return "N/A"

0 comments on commit 8758dac

Please sign in to comment.