File size: 2,480 Bytes
b9b22c3 7050235 35019e5 7050235 35019e5 7050235 35019e5 7050235 35019e5 7050235 b9b22c3 7050235 b9b22c3 7050235 35019e5 b9b22c3 35019e5 b9b22c3 35019e5 7050235 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
from deep_translator import GoogleTranslator
from transformers import pipeline
import translators as ts
class MangaTranslator:
def __init__(self):
self.target = "en"
self.source = "ja"
self.translators = {
"google": self._translate_with_google,
"hf": self._translate_with_hf,
"baidu": self._translate_with_baidu,
"bing": self._translate_with_bing
}
def translate(self, text, method="google"):
"""
Translates the given text to the target language using the specified method.
Args:
text (str): The text to be translated.
method (str):"google" for Google Translator,
"hf" for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
"baidu" for Baidu Translate
"bing" for Microsoft Bing Translator
Returns:
str: The translated text.
"""
translator_func = self.translators.get(method)
if translator_func:
return translator_func(self._preprocess_text(text))
else:
raise ValueError("Invalid translation method.")
def _translate_with_google(self, text):
translator = GoogleTranslator(source=self.source, target=self.target)
translated_text = translator.translate(text)
return translated_text if translated_text is not None else text
def _translate_with_hf(self, text):
pipe = pipeline("translation", model=f"Helsinki-NLP/opus-mt-ja-en")
translated_text = pipe(text)[0]["translation_text"]
return translated_text if translated_text is not None else text
def _translate_with_baidu(self, text):
translated_text = ts.translate_text(text, translator="baidu",
from_language="jp",
to_language=self.target)
return translated_text if translated_text is not None else text
def _translate_with_bing(self, text):
translated_text = ts.translate_text(text, translator="bing",
from_language=self.source,
to_language=self.target)
return translated_text if translated_text is not None else text
def _preprocess_text(self, text):
preprocessed_text = text.replace(".", ".")
return preprocessed_text
|