nidhal baccouri
commited on
Commit
·
714079e
1
Parent(s):
97bacde
added papago
Browse files- deep_translator/papago.py +24 -36
deep_translator/papago.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
"""
|
2 |
google translator API
|
3 |
"""
|
|
|
4 |
|
5 |
from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
|
6 |
from deep_translator.exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
|
@@ -72,31 +73,29 @@ class PapagoTranslator(object):
|
|
72 |
@return: str: translated text
|
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 |
-
else:
|
99 |
-
return element.get_text(strip=True)
|
100 |
|
101 |
def translate_file(self, path, **kwargs):
|
102 |
"""
|
@@ -146,22 +145,11 @@ class PapagoTranslator(object):
|
|
146 |
"""
|
147 |
if not batch:
|
148 |
raise Exception("Enter your text list that you want to translate")
|
149 |
-
|
150 |
-
print("Please wait.. This may take a couple of seconds because deep_translator sleeps "
|
151 |
-
"for two seconds after each request in order to not spam the google server.")
|
152 |
arr = []
|
153 |
for i, text in enumerate(batch):
|
154 |
|
155 |
translated = self.translate(text)
|
156 |
arr.append(translated)
|
157 |
-
print("sentence number ", i+1, " has been translated successfully")
|
158 |
-
sleep(2)
|
159 |
-
|
160 |
return arr
|
161 |
|
162 |
|
163 |
-
|
164 |
-
if __name__ == '__main__':
|
165 |
-
t = PapagoTranslator(source="en", target="de").translate("cute")
|
166 |
-
print(t)
|
167 |
-
|
|
|
1 |
"""
|
2 |
google translator API
|
3 |
"""
|
4 |
+
import json
|
5 |
|
6 |
from deep_translator.constants import BASE_URLS, PAPAGO_LANGUAGE_TO_CODE
|
7 |
from deep_translator.exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
|
|
|
73 |
@return: str: translated text
|
74 |
"""
|
75 |
|
76 |
+
payload = {
|
77 |
+
"source": self._source,
|
78 |
+
"target": self._target,
|
79 |
+
"text": text
|
80 |
+
}
|
81 |
+
headers = {
|
82 |
+
'X-Naver-Client-Id': self.client_id,
|
83 |
+
'X-Naver-Client-Secret': self.secret_key,
|
84 |
+
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
85 |
+
}
|
86 |
+
response = requests.post(self.__base_url, headers=headers, data=payload)
|
87 |
+
if response.status_code != 200:
|
88 |
+
raise Exception(f'Translation error! -> status code: {response.status_code}')
|
89 |
+
res_body = json.loads(response.text)
|
90 |
+
if "message" not in res_body:
|
91 |
+
raise TranslationNotFound(text)
|
92 |
+
|
93 |
+
msg = res_body.get("message")
|
94 |
+
result = msg.get("result", None)
|
95 |
+
if not result:
|
96 |
+
raise TranslationNotFound(text)
|
97 |
+
translated_text = result.get("translatedText")
|
98 |
+
return translated_text
|
|
|
|
|
99 |
|
100 |
def translate_file(self, path, **kwargs):
|
101 |
"""
|
|
|
145 |
"""
|
146 |
if not batch:
|
147 |
raise Exception("Enter your text list that you want to translate")
|
|
|
|
|
|
|
148 |
arr = []
|
149 |
for i, text in enumerate(batch):
|
150 |
|
151 |
translated = self.translate(text)
|
152 |
arr.append(translated)
|
|
|
|
|
|
|
153 |
return arr
|
154 |
|
155 |
|
|
|
|
|
|
|
|
|
|