2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
google translator API
|
|
|
|
"""
|
|
|
|
import json
|
2022-05-01 12:00:20 +00:00
|
|
|
from typing import List, Optional
|
|
|
|
|
2021-11-29 19:28:30 +00:00
|
|
|
import requests
|
2022-05-01 12:00:20 +00:00
|
|
|
|
|
|
|
from deep_translator.base import BaseTranslator
|
|
|
|
from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
|
|
|
|
from deep_translator.exceptions import TranslationNotFound
|
|
|
|
from deep_translator.validate import is_input_valid
|
2021-11-29 19:28:30 +00:00
|
|
|
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
class PapagoTranslator(BaseTranslator):
|
2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
class that wraps functions, which use google translate under the hood to translate text(s)
|
|
|
|
"""
|
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
client_id: Optional[str] = None,
|
|
|
|
secret_key: Optional[str] = None,
|
|
|
|
source: str = "auto",
|
|
|
|
target: str = "en",
|
|
|
|
**kwargs,
|
|
|
|
):
|
2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
@param source: source language to translate from
|
|
|
|
@param target: target language to translate to
|
|
|
|
"""
|
|
|
|
if not client_id or not secret_key:
|
2022-05-01 12:00:20 +00:00
|
|
|
raise Exception(
|
|
|
|
"Please pass your client id and secret key! visit the papago website for more infos"
|
|
|
|
)
|
2021-11-29 19:28:30 +00:00
|
|
|
|
|
|
|
self.client_id = client_id
|
|
|
|
self.secret_key = secret_key
|
2022-05-01 12:00:20 +00:00
|
|
|
super().__init__(
|
|
|
|
base_url=BASE_URLS.get("PAPAGO_API"),
|
|
|
|
source=source,
|
|
|
|
target=target,
|
|
|
|
languages=PAPAGO_LANGUAGE_TO_CODE,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2021-11-29 19:28:30 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate(self, text: str, **kwargs) -> str:
|
2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
function that uses google translate to translate a text
|
|
|
|
@param text: desired text to translate
|
|
|
|
@return: str: translated text
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
if is_input_valid(text):
|
|
|
|
payload = {"source": self._source, "target": self._target, "text": text}
|
|
|
|
headers = {
|
|
|
|
"X-Naver-Client-Id": self.client_id,
|
|
|
|
"X-Naver-Client-Secret": self.secret_key,
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
|
|
}
|
|
|
|
response = requests.post(self._base_url, headers=headers, data=payload)
|
|
|
|
if response.status_code != 200:
|
|
|
|
raise Exception(
|
|
|
|
f"Translation error! -> status code: {response.status_code}"
|
|
|
|
)
|
|
|
|
res_body = json.loads(response.text)
|
|
|
|
if "message" not in res_body:
|
|
|
|
raise TranslationNotFound(text)
|
|
|
|
|
|
|
|
msg = res_body.get("message")
|
|
|
|
result = msg.get("result", None)
|
|
|
|
if not result:
|
|
|
|
raise TranslationNotFound(text)
|
|
|
|
translated_text = result.get("translatedText")
|
|
|
|
return translated_text
|
|
|
|
|
|
|
|
def translate_file(self, path: str, **kwargs) -> str:
|
2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
translate directly from file
|
|
|
|
@param path: path to the target file
|
|
|
|
@type path: str
|
|
|
|
@param kwargs: additional args
|
|
|
|
@return: str
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
return self._translate_file(path, **kwargs)
|
2021-11-29 19:28:30 +00:00
|
|
|
|
2022-05-01 12:00:20 +00:00
|
|
|
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
2021-11-29 19:28:30 +00:00
|
|
|
"""
|
|
|
|
translate a list of texts
|
|
|
|
@param batch: list of texts you want to translate
|
|
|
|
@return: list of translations
|
|
|
|
"""
|
2022-05-01 12:00:20 +00:00
|
|
|
return self._translate_batch(batch, **kwargs)
|