=
commited on
Commit
·
7f8aad3
1
Parent(s):
43c88a7
added language detection
Browse files- deep_translator/configs.py +2 -6
- deep_translator/detection.py +20 -11
- deep_translator/google_trans.py +0 -10
- deep_translator/mymemory.py +0 -6
deep_translator/configs.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
-
|
2 |
config = {
|
3 |
-
|
4 |
-
"api_key": None,
|
5 |
-
"api_version": '0.2',
|
6 |
-
"url": 'https://ws.detectlanguage.com/0.2/detect',
|
7 |
"headers": {
|
8 |
'User-Agent': 'Detect Language API Python Client 1.4.0',
|
9 |
-
|
10 |
}
|
11 |
}
|
|
|
|
|
1 |
config = {
|
2 |
+
"url": 'https://ws.detectlanguage.com/0.2/detect',
|
|
|
|
|
|
|
3 |
"headers": {
|
4 |
'User-Agent': 'Detect Language API Python Client 1.4.0',
|
5 |
+
'Authorization': 'Bearer {}',
|
6 |
}
|
7 |
}
|
deep_translator/detection.py
CHANGED
@@ -2,20 +2,29 @@ import requests
|
|
2 |
from deep_translator.configs import config
|
3 |
|
4 |
|
5 |
-
def detect_language(text):
|
6 |
"""
|
7 |
function responsible for detecting the language from a text
|
8 |
"""
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
if lang:
|
17 |
-
return lang
|
18 |
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from deep_translator.configs import config
|
3 |
|
4 |
|
5 |
+
def detect_language(text, api_key=None):
|
6 |
"""
|
7 |
function responsible for detecting the language from a text
|
8 |
"""
|
9 |
+
if not api_key:
|
10 |
+
raise Exception("you need to get an API_KEY for this to work. "
|
11 |
+
"Get one for free here: https://detectlanguage.com/documentation")
|
12 |
|
13 |
+
else:
|
14 |
+
headers = config['headers']
|
15 |
+
headers['Authorization'] = headers['Authorization'].format(api_key)
|
|
|
|
|
16 |
|
17 |
+
try:
|
18 |
+
response = requests.post(config['url'],
|
19 |
+
json={'q': text},
|
20 |
+
headers=headers)
|
21 |
|
22 |
+
body = response.json().get('data')
|
23 |
+
detections = body.get('detections')
|
24 |
+
lang = detections[0].get('language', None)
|
25 |
+
if lang:
|
26 |
+
return lang
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
print(e.args)
|
30 |
+
raise
|
deep_translator/google_trans.py
CHANGED
@@ -74,12 +74,6 @@ class GoogleTranslator(BaseTranslator):
|
|
74 |
params=self._url_params)
|
75 |
|
76 |
soup = BeautifulSoup(response.text, 'html.parser')
|
77 |
-
|
78 |
-
print(soup)
|
79 |
-
src_lang = soup.findAll('a', {"class": "s1"})
|
80 |
-
for el in src_lang:
|
81 |
-
print(el)
|
82 |
-
exit()
|
83 |
element = soup.find(self._element_tag, self._element_query)
|
84 |
if not element:
|
85 |
raise ElementNotFoundInGetRequest(element)
|
@@ -117,7 +111,3 @@ class GoogleTranslator(BaseTranslator):
|
|
117 |
|
118 |
except Exception as e:
|
119 |
raise e
|
120 |
-
|
121 |
-
|
122 |
-
if __name__ == '__main__':
|
123 |
-
res = GoogleTranslator(target='de').translate("you are cute")
|
|
|
74 |
params=self._url_params)
|
75 |
|
76 |
soup = BeautifulSoup(response.text, 'html.parser')
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
element = soup.find(self._element_tag, self._element_query)
|
78 |
if not element:
|
79 |
raise ElementNotFoundInGetRequest(element)
|
|
|
111 |
|
112 |
except Exception as e:
|
113 |
raise e
|
|
|
|
|
|
|
|
deep_translator/mymemory.py
CHANGED
@@ -50,8 +50,6 @@ class MyMemoryTranslator(BaseTranslator):
|
|
50 |
response = requests.get(self.__base_url,
|
51 |
params=self._url_params,
|
52 |
headers=self.headers)
|
53 |
-
|
54 |
-
print(response.text)
|
55 |
data = response.json()
|
56 |
if not data:
|
57 |
raise Exception("Translation was not found in response!")
|
@@ -88,7 +86,3 @@ class MyMemoryTranslator(BaseTranslator):
|
|
88 |
|
89 |
except Exception as e:
|
90 |
raise e
|
91 |
-
|
92 |
-
if __name__ == '__main__':
|
93 |
-
res = MyMemoryTranslator(source="auto", target='de').translate("bonjour la vie")
|
94 |
-
|
|
|
50 |
response = requests.get(self.__base_url,
|
51 |
params=self._url_params,
|
52 |
headers=self.headers)
|
|
|
|
|
53 |
data = response.json()
|
54 |
if not data:
|
55 |
raise Exception("Translation was not found in response!")
|
|
|
86 |
|
87 |
except Exception as e:
|
88 |
raise e
|
|
|
|
|
|
|
|