nidhal baccouri
commited on
Commit
·
9759c6a
1
Parent(s):
790386c
updated yandex
Browse files- deep_translator/constants.py +1 -1
- deep_translator/papago.py +4 -1
- deep_translator/yandex.py +9 -12
deep_translator/constants.py
CHANGED
@@ -8,7 +8,7 @@ BASE_URLS = {
|
|
8 |
"MYMEMORY": "http://api.mymemory.translated.net/get",
|
9 |
"QCRI": "https://mt.qcri.org/api/v1/{endpoint}?",
|
10 |
"DEEPL": "https://api.deepl.com/{version}/",
|
11 |
-
"DEEPL_FREE": "https://api-free.deepl.com",
|
12 |
"MICROSOFT_TRANSLATE": "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0",
|
13 |
"PAPAGO": "https://papago.naver.com/",
|
14 |
"PAPAGO_API": "https://openapi.naver.com/v1/papago/n2mt"
|
|
|
8 |
"MYMEMORY": "http://api.mymemory.translated.net/get",
|
9 |
"QCRI": "https://mt.qcri.org/api/v1/{endpoint}?",
|
10 |
"DEEPL": "https://api.deepl.com/{version}/",
|
11 |
+
"DEEPL_FREE": "https://api-free.deepl.com/v2/",
|
12 |
"MICROSOFT_TRANSLATE": "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0",
|
13 |
"PAPAGO": "https://papago.naver.com/",
|
14 |
"PAPAGO_API": "https://openapi.naver.com/v1/papago/n2mt"
|
deep_translator/papago.py
CHANGED
@@ -16,11 +16,14 @@ class PapagoTranslator(object):
|
|
16 |
_languages = PAPAGO_LANGUAGE_TO_CODE
|
17 |
supported_languages = list(_languages.keys())
|
18 |
|
19 |
-
def __init__(self, client_id, secret_key, source="auto", target="en"):
|
20 |
"""
|
21 |
@param source: source language to translate from
|
22 |
@param target: target language to translate to
|
23 |
"""
|
|
|
|
|
|
|
24 |
self.__base_url = BASE_URLS.get("PAPAGO_API")
|
25 |
self.client_id = client_id
|
26 |
self.secret_key = secret_key
|
|
|
16 |
_languages = PAPAGO_LANGUAGE_TO_CODE
|
17 |
supported_languages = list(_languages.keys())
|
18 |
|
19 |
+
def __init__(self, client_id=None, secret_key=None, source="auto", target="en"):
|
20 |
"""
|
21 |
@param source: source language to translate from
|
22 |
@param target: target language to translate to
|
23 |
"""
|
24 |
+
if not client_id or not secret_key:
|
25 |
+
raise Exception("Please pass your client id and secret key! visit the papago website for more infos")
|
26 |
+
|
27 |
self.__base_url = BASE_URLS.get("PAPAGO_API")
|
28 |
self.client_id = client_id
|
29 |
self.secret_key = secret_key
|
deep_translator/yandex.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
Yandex translator API
|
3 |
"""
|
4 |
import requests
|
5 |
-
from requests import exceptions
|
6 |
from deep_translator.constants import BASE_URLS
|
7 |
from deep_translator.exceptions import (RequestError,
|
8 |
ServerException, TranslationNotFound, TooManyRequests)
|
@@ -13,13 +12,15 @@ class YandexTranslator(object):
|
|
13 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
14 |
"""
|
15 |
|
16 |
-
def __init__(self, api_key=None):
|
17 |
"""
|
18 |
@param api_key: your yandex api key
|
19 |
"""
|
20 |
if not api_key:
|
21 |
raise ServerException(401)
|
22 |
self.__base_url = BASE_URLS.get("YANDEX")
|
|
|
|
|
23 |
|
24 |
self.api_key = api_key
|
25 |
self.api_version = "v1.5"
|
@@ -79,11 +80,11 @@ class YandexTranslator(object):
|
|
79 |
raise ServerException(501)
|
80 |
return language
|
81 |
|
82 |
-
def translate(self,
|
83 |
params = {
|
84 |
"text": text,
|
85 |
"format": "plain",
|
86 |
-
"lang": target if source == "auto" else "{}-{}".format(source, target),
|
87 |
"key": self.api_key
|
88 |
}
|
89 |
try:
|
@@ -105,11 +106,9 @@ class YandexTranslator(object):
|
|
105 |
|
106 |
return response['text']
|
107 |
|
108 |
-
def translate_file(self,
|
109 |
"""
|
110 |
translate from a file
|
111 |
-
@param source: source language
|
112 |
-
@param target: target language
|
113 |
@param path: path to file
|
114 |
@return: translated text
|
115 |
"""
|
@@ -117,16 +116,14 @@ class YandexTranslator(object):
|
|
117 |
with open(path) as f:
|
118 |
text = f.read()
|
119 |
|
120 |
-
return self.translate(
|
121 |
except Exception as e:
|
122 |
raise e
|
123 |
|
124 |
-
def translate_batch(self,
|
125 |
"""
|
126 |
translate a batch of texts
|
127 |
-
@param source: source language
|
128 |
-
@param target: target language
|
129 |
@param batch: list of texts to translate
|
130 |
@return: list of translations
|
131 |
"""
|
132 |
-
return [self.translate(
|
|
|
2 |
Yandex translator API
|
3 |
"""
|
4 |
import requests
|
|
|
5 |
from deep_translator.constants import BASE_URLS
|
6 |
from deep_translator.exceptions import (RequestError,
|
7 |
ServerException, TranslationNotFound, TooManyRequests)
|
|
|
12 |
class that wraps functions, which use the yandex translator under the hood to translate word(s)
|
13 |
"""
|
14 |
|
15 |
+
def __init__(self, api_key=None, source="en", target="de"):
|
16 |
"""
|
17 |
@param api_key: your yandex api key
|
18 |
"""
|
19 |
if not api_key:
|
20 |
raise ServerException(401)
|
21 |
self.__base_url = BASE_URLS.get("YANDEX")
|
22 |
+
self.source = source
|
23 |
+
self.target = target
|
24 |
|
25 |
self.api_key = api_key
|
26 |
self.api_version = "v1.5"
|
|
|
80 |
raise ServerException(501)
|
81 |
return language
|
82 |
|
83 |
+
def translate(self, text, proxies=None):
|
84 |
params = {
|
85 |
"text": text,
|
86 |
"format": "plain",
|
87 |
+
"lang": self.target if self.source == "auto" else "{}-{}".format(self.source, self.target),
|
88 |
"key": self.api_key
|
89 |
}
|
90 |
try:
|
|
|
106 |
|
107 |
return response['text']
|
108 |
|
109 |
+
def translate_file(self, path):
|
110 |
"""
|
111 |
translate from a file
|
|
|
|
|
112 |
@param path: path to file
|
113 |
@return: translated text
|
114 |
"""
|
|
|
116 |
with open(path) as f:
|
117 |
text = f.read()
|
118 |
|
119 |
+
return self.translate(text)
|
120 |
except Exception as e:
|
121 |
raise e
|
122 |
|
123 |
+
def translate_batch(self, batch):
|
124 |
"""
|
125 |
translate a batch of texts
|
|
|
|
|
126 |
@param batch: list of texts to translate
|
127 |
@return: list of translations
|
128 |
"""
|
129 |
+
return [self.translate(text) for text in batch]
|