=
commited on
Commit
·
09af6bc
1
Parent(s):
fa1a9e9
added batch detection
Browse files- README.rst +18 -7
- deep_translator/__init__.py +3 -2
- deep_translator/detection.py +32 -12
README.rst
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
|
2 |
deep_translator
|
3 |
-
|
4 |
|
5 |
|
6 |
.. image:: https://img.shields.io/pypi/v/deep_translator.svg
|
@@ -113,7 +113,7 @@ This includes the google, pons, linguee and mymemory translator (at least for no
|
|
113 |
translators will be integrated in the future.
|
114 |
|
115 |
Imports
|
116 |
-
|
117 |
|
118 |
.. code-block:: python
|
119 |
|
@@ -121,11 +121,12 @@ Imports
|
|
121 |
PonsTranslator,
|
122 |
LingueeTranslator,
|
123 |
MyMemoryTranslator,
|
124 |
-
|
|
|
125 |
|
126 |
|
127 |
Check Supported Languages
|
128 |
-
|
129 |
|
130 |
.. note::
|
131 |
|
@@ -141,7 +142,7 @@ Check Supported Languages
|
|
141 |
langs_dict = GoogleTranslator.get_supported_languages(as_dict=True) # output: {arabic: ar, french: fr, english:en etc...}
|
142 |
|
143 |
Language Detection
|
144 |
-
|
145 |
|
146 |
.. note::
|
147 |
|
@@ -149,11 +150,21 @@ Language Detection
|
|
149 |
Therefore, you will need to get your own api_key if you want to use the language detection function.
|
150 |
I figured out you can get one for free here: https://detectlanguage.com/documentation
|
151 |
|
|
|
|
|
152 |
.. code-block:: python
|
153 |
|
154 |
-
lang =
|
155 |
print(lang) # output: fr
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
Google Translate
|
159 |
-----------------
|
|
|
1 |
+
##################
|
2 |
deep_translator
|
3 |
+
##################
|
4 |
|
5 |
|
6 |
.. image:: https://img.shields.io/pypi/v/deep_translator.svg
|
|
|
113 |
translators will be integrated in the future.
|
114 |
|
115 |
Imports
|
116 |
+
""""""""
|
117 |
|
118 |
.. code-block:: python
|
119 |
|
|
|
121 |
PonsTranslator,
|
122 |
LingueeTranslator,
|
123 |
MyMemoryTranslator,
|
124 |
+
single_detection,
|
125 |
+
batch_detection)
|
126 |
|
127 |
|
128 |
Check Supported Languages
|
129 |
+
""""""""""""""""""""""""""
|
130 |
|
131 |
.. note::
|
132 |
|
|
|
142 |
langs_dict = GoogleTranslator.get_supported_languages(as_dict=True) # output: {arabic: ar, french: fr, english:en etc...}
|
143 |
|
144 |
Language Detection
|
145 |
+
"""""""""""""""""""
|
146 |
|
147 |
.. note::
|
148 |
|
|
|
150 |
Therefore, you will need to get your own api_key if you want to use the language detection function.
|
151 |
I figured out you can get one for free here: https://detectlanguage.com/documentation
|
152 |
|
153 |
+
- Single Text Detection
|
154 |
+
|
155 |
.. code-block:: python
|
156 |
|
157 |
+
lang = single_detection('bonjour la vie', api_key='your_api_key')
|
158 |
print(lang) # output: fr
|
159 |
|
160 |
+
- Batch Detection
|
161 |
+
|
162 |
+
.. code-block:: python
|
163 |
+
|
164 |
+
lang = batch_detection(['bonjour la vie', 'hello world'], api_key='your_api_key')
|
165 |
+
print(lang) # output: [fr, en]
|
166 |
+
|
167 |
+
|
168 |
|
169 |
Google Translate
|
170 |
-----------------
|
deep_translator/__init__.py
CHANGED
@@ -4,7 +4,7 @@ from .google_trans import GoogleTranslator
|
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
6 |
from .mymemory import MyMemoryTranslator
|
7 |
-
from .detection import
|
8 |
|
9 |
|
10 |
__author__ = """Nidhal Baccouri"""
|
@@ -15,4 +15,5 @@ __all__ = [GoogleTranslator,
|
|
15 |
PonsTranslator,
|
16 |
LingueeTranslator,
|
17 |
MyMemoryTranslator,
|
18 |
-
|
|
|
|
4 |
from .pons import PonsTranslator
|
5 |
from .linguee import LingueeTranslator
|
6 |
from .mymemory import MyMemoryTranslator
|
7 |
+
from .detection import single_detection, batch_detection
|
8 |
|
9 |
|
10 |
__author__ = """Nidhal Baccouri"""
|
|
|
15 |
PonsTranslator,
|
16 |
LingueeTranslator,
|
17 |
MyMemoryTranslator,
|
18 |
+
single_detection,
|
19 |
+
batch_detection]
|
deep_translator/detection.py
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
import requests
|
2 |
from deep_translator.configs import config
|
|
|
3 |
|
4 |
|
5 |
-
def
|
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")
|
@@ -13,20 +12,41 @@ def detect_language(text, api_key=None):
|
|
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 |
-
|
26 |
-
lang = detections[0].get('language', None)
|
27 |
-
if lang:
|
28 |
-
return lang
|
29 |
|
30 |
-
except
|
31 |
print("Error occured while requesting from server: ", e.args)
|
32 |
raise e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
from deep_translator.configs import config
|
3 |
+
from requests.exceptions import HTTPError
|
4 |
|
5 |
|
6 |
+
def get_request_body(text, api_key, *args):
|
7 |
+
|
|
|
|
|
8 |
if not api_key:
|
9 |
raise Exception("you need to get an API_KEY for this to work. "
|
10 |
"Get one for free here: https://detectlanguage.com/documentation")
|
|
|
12 |
raise Exception("Please provide an input text")
|
13 |
|
14 |
else:
|
|
|
|
|
|
|
15 |
try:
|
16 |
+
headers = config['headers']
|
17 |
+
headers['Authorization'] = headers['Authorization'].format(api_key)
|
18 |
response = requests.post(config['url'],
|
19 |
json={'q': text},
|
20 |
headers=headers)
|
21 |
|
22 |
body = response.json().get('data')
|
23 |
+
return body
|
|
|
|
|
|
|
24 |
|
25 |
+
except HTTPError as e:
|
26 |
print("Error occured while requesting from server: ", e.args)
|
27 |
raise e
|
28 |
+
|
29 |
+
|
30 |
+
def single_detection(text, api_key=None, detailed=False, *args, **kwargs):
|
31 |
+
"""
|
32 |
+
function responsible for detecting the language from a text
|
33 |
+
"""
|
34 |
+
body = get_request_body(text, api_key)
|
35 |
+
detections = body.get('detections')
|
36 |
+
if detailed:
|
37 |
+
return detections[0]
|
38 |
+
|
39 |
+
lang = detections[0].get('language', None)
|
40 |
+
if lang:
|
41 |
+
return lang
|
42 |
+
|
43 |
+
|
44 |
+
def batch_detection(text_list, api_key, detailed=False, *args):
|
45 |
+
body = get_request_body(text_list, api_key)
|
46 |
+
detections = body.get('detections')
|
47 |
+
res = [obj[0] for obj in detections]
|
48 |
+
if detailed:
|
49 |
+
return res
|
50 |
+
else:
|
51 |
+
return [obj['language'] for obj in res]
|
52 |
+
|