=
commited on
Commit
·
43c88a7
1
Parent(s):
932a348
start working on language detection
Browse files- deep_translator/__init__.py +3 -1
- deep_translator/configs.py +11 -0
- deep_translator/detection.py +21 -0
- deep_translator/google_trans.py +9 -0
- deep_translator/mymemory.py +4 -0
- 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,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
'Authorization': 'Bearer d359b68fbc02599044843e28800477f5',
|
| 10 |
+
}
|
| 11 |
+
}
|
deep_translator/detection.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
response = requests.post(config['url'],
|
| 10 |
+
json={'q': text},
|
| 11 |
+
headers=config['headers'])
|
| 12 |
+
|
| 13 |
+
body = response.json().get('data')
|
| 14 |
+
detections = body.get('detections')
|
| 15 |
+
lang = detections[0].get('language', None)
|
| 16 |
+
if lang:
|
| 17 |
+
return lang
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# lang = detect_language('你好可爱')
|
| 21 |
+
# print("detected lang: ", lang)
|
deep_translator/google_trans.py
CHANGED
|
@@ -74,6 +74,12 @@ class GoogleTranslator(BaseTranslator):
|
|
| 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)
|
|
@@ -112,3 +118,6 @@ class GoogleTranslator(BaseTranslator):
|
|
| 112 |
except Exception as e:
|
| 113 |
raise e
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
raise e
|
| 120 |
|
| 121 |
+
|
| 122 |
+
if __name__ == '__main__':
|
| 123 |
+
res = GoogleTranslator(target='de').translate("you are cute")
|
deep_translator/mymemory.py
CHANGED
|
@@ -51,6 +51,7 @@ class MyMemoryTranslator(BaseTranslator):
|
|
| 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!")
|
|
@@ -88,3 +89,6 @@ class MyMemoryTranslator(BaseTranslator):
|
|
| 88 |
except Exception as e:
|
| 89 |
raise e
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
| 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!")
|
|
|
|
| 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 |
+
|
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 |
+
|