Skip to content

Commit

Permalink
🔨 Refactor Code
Browse files Browse the repository at this point in the history
  • Loading branch information
diogosilva30 committed Apr 7, 2021
1 parent 0d1880f commit a104f6a
Show file tree
Hide file tree
Showing 32 changed files with 957 additions and 9,872 deletions.
Binary file removed .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from translatepy.translators.yandex import YandexTranslator
from translatepy.translators import (
GoogleV2Translator,
GoogleTranslator,
BingTranslator,
ReversoTranslator,
Translator,
DeepLTranslator,
)
from translatepy.models import Translation


t = BingTranslator()
print(t.get_language("Fren"))
print(
t.translate(
"Olá bom dia. Tudo bem com o senhor? Hoje vou até a uma loja nova.",
destination_language=t.get_language("Fren").language,
)
)
105 changes: 45 additions & 60 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,45 @@
import translatepy

t = translatepy.Translator()

def test_translate():
print("[test] --> Testing Translator.translate")
assert t.translate("Hello", "Japanese", "French") is not None
assert t.translate("", "Japanese", "French") is None
assert t.translate(" ", "Japanese", "French") is None
assert t.translate("Hello", "French", "Japanese") is not None
assert t.translate("Hello", "ja", "fr") is not None

def test_language():
print("[test] --> Testing Translator.language")
assert t.language("Hello") is not None

def test_Language():
print("[test] --> Testing translatepy.Language")
assert translatepy.Language("French").language == "fr"
assert translatepy.Language("Japanese").language == "ja"
assert translatepy.Language("en").language == "en"
assert translatepy.Language("japanese").language == "ja"
assert translatepy.AUTOMATIC.japanese == "自動"


def test_spellcheck():
print("[test] --> Testing Translator.spellcheck")
assert t.spellcheck("Hello") is not None
assert t.spellcheck("God morning") is not None

def test_example():
print("[test] --> Testing Translator.example")
assert t.example("Hello", "japanese") is not None

def test_dictionary():
print("[test] --> Testing Translator.dictionary")
#assert t.dictionary("Hello", "Japanese") is not None
# I can't test dictionary as DeepL is very strict on their rate-limit

def test_imports():
print("[test] --> Testing imports")
from translatepy.data import data
from translatepy.models import exceptions
from translatepy.models import languages
from translatepy.models import userAgents
from translatepy.translators import bing
from translatepy.translators import deepl
from translatepy.translators import google
from translatepy.translators import reverso
from translatepy.translators import unselected
from translatepy.translators import yandex
from translatepy.utils import annotations
from translatepy.utils import similarity
from translatepy import translate
import translatepy

def test_yandex_sid():
print("[test] --> Testing Translator.yandex_translate._sid")
t = translatepy.Translator()
assert t.yandex_translate._sid == translatepy.Translator().yandex_translate._sid
from translatepy.translators import *
from translatepy.models import Translation


def test_translators_translation():
"""
Tests that all `Translators` are correctly translating.
"""
translators = [
BingTranslator,
DeepLTranslator,
GoogleTranslator,
GoogleV2Translator,
ReversoTranslator,
Translator,
# YandexTranslator,
]

for translator_class in translators:
# Test every translator
# Create a sample string (portuguese)
sample_str = "Olá! Hoje está um belo dia para passear."

# Intantiate the translator
translator = translator_class()
# Try every language supported in the translator
for lang in translator.supported_languages:
print(f"Trying translator {str(translator)} - lang {lang}")

result = translator.translate(
text=sample_str,
destination_language=translator.get_language(lang).language.code,
)

# Check that result is a `Translation`
assert isinstance(result, Translation)

# Check if source language was correctly identified
assert result.source_language == "pt"

# Check that destination language is correct
assert result.destination_language == lang


test_translators_translation()
Loading

0 comments on commit a104f6a

Please sign in to comment.