File size: 3,285 Bytes
b1f5dde
fa90b1d
b1f5dde
fa90b1d
 
 
714079e
7a05cd7
 
 
 
 
78d0ff1
 
3aa1058
70f6ed6
b1f5dde
2bbc526
b1f5dde
 
 
 
78d0ff1
7ade3be
 
 
 
 
 
78d0ff1
b1f5dde
 
 
 
9759c6a
dbaf8a8
78d0ff1
 
9759c6a
97bacde
 
2bbc526
f89616a
2bbc526
 
 
78d0ff1
2bbc526
b1f5dde
7ade3be
b1f5dde
 
 
 
 
76aa3b2
cb15d3a
 
 
 
 
70f6ed6
78d0ff1
 
 
70f6ed6
cb15d3a
 
 
3aa1058
70f6ed6
78d0ff1
 
70f6ed6
 
 
b1f5dde
70f6ed6
 
 
 
 
 
b1f5dde
7ade3be
b1f5dde
 
 
 
 
 
 
2bbc526
b1f5dde
7ade3be
b1f5dde
 
 
 
 
2bbc526
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
papago translator API
"""

__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"

import json
from typing import List, Optional

import requests

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, request_failed


class PapagoTranslator(BaseTranslator):
    """
    class that wraps functions, which use google translate under the hood to translate text(s)
    """

    def __init__(
        self,
        client_id: Optional[str] = None,
        secret_key: Optional[str] = None,
        source: str = "auto",
        target: str = "en",
        **kwargs,
    ):
        """
        @param source: source language to translate from
        @param target: target language to translate to
        """
        if not client_id or not secret_key:
            raise Exception(
                "Please pass your client id and secret key! visit the papago website for more infos"
            )

        self.client_id = client_id
        self.secret_key = secret_key
        super().__init__(
            base_url=BASE_URLS.get("PAPAGO_API"),
            source=source,
            target=target,
            languages=PAPAGO_LANGUAGE_TO_CODE,
            **kwargs,
        )

    def translate(self, text: str, **kwargs) -> str:
        """
        function that uses google translate to translate a text
        @param text: desired text to translate
        @return: str: translated text
        """
        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 request_failed(status_code=response.status_code):
                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:
        """
        translate directly from file
        @param path: path to the target file
        @type path: str
        @param kwargs: additional args
        @return: str
        """
        return self._translate_file(path, **kwargs)

    def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
        """
        translate a list of texts
        @param batch: list of texts you want to translate
        @return: list of translations
        """
        return self._translate_batch(batch, **kwargs)