Merge pull request #5 from nidhaloff/feature/autodetection
Browse files- deep_translator/__init__.py +3 -1
- deep_translator/configs.py +7 -0
- deep_translator/detection.py +32 -0
- deep_translator/google_trans.py +0 -1
- deep_translator/mymemory.py +0 -2
- deep_translator/utils.py +21 -0
deep_translator/__init__.py
CHANGED
@@ -4,6 +4,7 @@ from .google_trans import GoogleTranslator
|
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
6 |
from .mymemory import MyMemoryTranslator
|
|
|
7 |
|
8 |
|
9 |
__author__ = """Nidhal Baccouri"""
|
@@ -13,4 +14,5 @@ __version__ = '1.1.2'
|
|
13 |
__all__ = [GoogleTranslator,
|
14 |
PonsTranslator,
|
15 |
LingueeTranslator,
|
16 |
-
MyMemoryTranslator
|
|
|
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
6 |
from .mymemory import MyMemoryTranslator
|
7 |
+
from .detection import detect_language
|
8 |
|
9 |
|
10 |
__author__ = """Nidhal Baccouri"""
|
|
|
14 |
__all__ = [GoogleTranslator,
|
15 |
PonsTranslator,
|
16 |
LingueeTranslator,
|
17 |
+
MyMemoryTranslator,
|
18 |
+
detect_language]
|
deep_translator/configs.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
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 |
+
if not text:
|
13 |
+
raise Exception("Please provide an input text")
|
14 |
+
|
15 |
+
else:
|
16 |
+
headers = config['headers']
|
17 |
+
headers['Authorization'] = headers['Authorization'].format(api_key)
|
18 |
+
|
19 |
+
try:
|
20 |
+
response = requests.post(config['url'],
|
21 |
+
json={'q': text},
|
22 |
+
headers=headers)
|
23 |
+
|
24 |
+
body = response.json().get('data')
|
25 |
+
detections = body.get('detections')
|
26 |
+
lang = detections[0].get('language', None)
|
27 |
+
if lang:
|
28 |
+
return lang
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
print(e.args)
|
32 |
+
raise
|
deep_translator/google_trans.py
CHANGED
@@ -111,4 +111,3 @@ class GoogleTranslator(BaseTranslator):
|
|
111 |
|
112 |
except Exception as e:
|
113 |
raise e
|
114 |
-
|
|
|
111 |
|
112 |
except Exception as e:
|
113 |
raise e
|
|
deep_translator/mymemory.py
CHANGED
@@ -50,7 +50,6 @@ class MyMemoryTranslator(BaseTranslator):
|
|
50 |
response = requests.get(self.__base_url,
|
51 |
params=self._url_params,
|
52 |
headers=self.headers)
|
53 |
-
|
54 |
data = response.json()
|
55 |
if not data:
|
56 |
raise Exception("Translation was not found in response!")
|
@@ -87,4 +86,3 @@ class MyMemoryTranslator(BaseTranslator):
|
|
87 |
|
88 |
except Exception as e:
|
89 |
raise e
|
90 |
-
|
|
|
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
|
|
deep_translator/utils.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
|
5 |
+
|
6 |
+
# res = requests.get('https://translate.google.com/?sl=auto&tl=en#view=home&op=translate&sl=auto&tl=de&text=cute')
|
7 |
+
# soup = BeautifulSoup(res.text, 'html.parser')
|
8 |
+
# print(soup)
|
9 |
+
# a = soup.find('div', {'class': 'goog-inline-block jfk-button jfk-button-standard jfk-button-collapse-right jfk-button-checked'})
|
10 |
+
|
11 |
+
# print(a)
|
12 |
+
from bs4 import BeautifulSoup
|
13 |
+
from requests_html import HTMLSession
|
14 |
+
|
15 |
+
session = HTMLSession()
|
16 |
+
r = session.get('https://translate.google.com/m?hl=en&sl=auto&tl=de&ie=UTF-8&prev=_m&q=here+is+a+sentence')
|
17 |
+
r.html.render()
|
18 |
+
|
19 |
+
print(r.html.html)
|
20 |
+
#soup = BeautifulSoup(r.html.html, 'html.parser')
|
21 |
+
|