diff --git a/openlrc/translate.py b/openlrc/translate.py index 48b9b29..399a8af 100644 --- a/openlrc/translate.py +++ b/openlrc/translate.py @@ -8,7 +8,6 @@ from abc import ABC, abstractmethod from typing import Union, List -import deepl import requests from openlrc.chatbot import GPTBot @@ -160,30 +159,31 @@ def translate(self, texts: Union[str, List[str]], src_lang, target_lang): return json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')) - -class DeepLTranslator(Translator): - def __init__(self): - self.key = os.environ['DEEPL_KEY'] - self.translator = deepl.Translator(self.key) - - def _check_limit(self, texts: List[str]): - usage = self.translator.get_usage() - char_num = sum([len(text) for text in texts]) - - if usage.character.count + char_num > usage.character.limit: - raise RuntimeError(f'This translate call would exceed DeepL character limit: {usage.character.limit}') - - def translate(self, texts: Union[str, List[str]], src_lang, target_lang): - if not isinstance(texts, list): - texts = [texts] - - self._check_limit(texts) - - translations = self.translator.translate_text(texts, target_lang=target_lang) - - if not isinstance(translations, list): - translations = [translations] - - translations = [translation.text for translation in translations] - - return translations +# Not integrated by the openlrc main function because of performance +# +# class DeepLTranslator(Translator): +# def __init__(self): +# self.key = os.environ['DEEPL_KEY'] +# self.translator = deepl.Translator(self.key) +# +# def _check_limit(self, texts: List[str]): +# usage = self.translator.get_usage() +# char_num = sum([len(text) for text in texts]) +# +# if usage.character.count + char_num > usage.character.limit: +# raise RuntimeError(f'This translate call would exceed DeepL character limit: {usage.character.limit}') +# +# def translate(self, texts: Union[str, List[str]], src_lang, target_lang): +# if not isinstance(texts, list): +# texts = [texts] +# +# self._check_limit(texts) +# +# translations = self.translator.translate_text(texts, target_lang=target_lang) +# +# if not isinstance(translations, list): +# translations = [translations] +# +# translations = [translation.text for translation in translations] +# +# return translations diff --git a/tests/test_translate.py b/tests/test_translate.py index 63643c1..b28fe8d 100644 --- a/tests/test_translate.py +++ b/tests/test_translate.py @@ -43,3 +43,20 @@ def test_empty_text_list_translation(self): translator = GPTTranslator() translations = translator.translate(texts, 'en', 'es') assert translations == [] + +# Not integrated by the openlrc main function because of performance +# +# class TestDeepLTranslator(unittest.TestCase): +# def test_single_chunk_translation(self): +# text = 'Hello, how are you?' +# translator = DeepLTranslator() +# translation = translator.translate(text, 'en', 'es')[0] +# +# assert get_similarity(translation, 'Hola, ¿cómo estás?') > 0.618 +# +# def test_multiple_chunk_translation(self): +# texts = ['Hello, how are you?', 'I am fine, thank you.'] +# translator = DeepLTranslator() +# translations = translator.translate(texts, 'en', 'es') +# assert get_similarity(translations[0], 'Hola, ¿cómo estás?') > 0.618 +# assert get_similarity(translations[1], 'Estoy bien, gracias.') > 0.618