Saminul
commited on
Commit
·
1d42175
1
Parent(s):
ffa7560
initial setup
Browse files- deep_translator/__init__.py +2 -0
- deep_translator/constants.py +24 -0
- deep_translator/libre.py +60 -0
- examples/libre.py +7 -0
deep_translator/__init__.py
CHANGED
|
@@ -11,6 +11,7 @@ from .deepl import DeepL
|
|
| 11 |
from .detection import single_detection, batch_detection
|
| 12 |
from .microsoft import MicrosoftTranslator
|
| 13 |
from .papago import PapagoTranslator
|
|
|
|
| 14 |
|
| 15 |
# TODO: Discussion: These should be declared in setup.cfg, setting them here is redundant
|
| 16 |
__author__ = """Nidhal Baccouri"""
|
|
@@ -26,6 +27,7 @@ __all__ = [
|
|
| 26 |
"MicrosoftTranslator",
|
| 27 |
"QCRI",
|
| 28 |
"DeepL",
|
|
|
|
| 29 |
"single_detection",
|
| 30 |
"batch_detection"
|
| 31 |
]
|
|
|
|
| 11 |
from .detection import single_detection, batch_detection
|
| 12 |
from .microsoft import MicrosoftTranslator
|
| 13 |
from .papago import PapagoTranslator
|
| 14 |
+
from .libre import LibreTranslator
|
| 15 |
|
| 16 |
# TODO: Discussion: These should be declared in setup.cfg, setting them here is redundant
|
| 17 |
__author__ = """Nidhal Baccouri"""
|
|
|
|
| 27 |
"MicrosoftTranslator",
|
| 28 |
"QCRI",
|
| 29 |
"DeepL",
|
| 30 |
+
"LibreTranslator"
|
| 31 |
"single_detection",
|
| 32 |
"batch_detection"
|
| 33 |
]
|
deep_translator/constants.py
CHANGED
|
@@ -258,3 +258,27 @@ QCRI_CODE_TO_LANGUAGE = {
|
|
| 258 |
QCRI_LANGUAGE_TO_CODE = {
|
| 259 |
v: k for k, v in QCRI_CODE_TO_LANGUAGE.items()
|
| 260 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
QCRI_LANGUAGE_TO_CODE = {
|
| 259 |
v: k for k, v in QCRI_CODE_TO_LANGUAGE.items()
|
| 260 |
}
|
| 261 |
+
|
| 262 |
+
LIBRE_CODES_TO_LANGUAGES = {
|
| 263 |
+
'en': 'English',
|
| 264 |
+
'ar': 'Arabic',
|
| 265 |
+
'zh': 'Chinese',
|
| 266 |
+
'fr': 'French',
|
| 267 |
+
'de': 'German',
|
| 268 |
+
'hi': 'Hindi',
|
| 269 |
+
'id': 'Indonesian',
|
| 270 |
+
'ga': 'Irish',
|
| 271 |
+
'it': 'Italian',
|
| 272 |
+
'ja': 'Japanese',
|
| 273 |
+
'ko': 'Korean',
|
| 274 |
+
'pl': 'Polish',
|
| 275 |
+
'pt': 'Portuguese',
|
| 276 |
+
'ru': 'Russian',
|
| 277 |
+
'es': 'Spanish',
|
| 278 |
+
'tr': 'Turkish',
|
| 279 |
+
'vi': 'Vietnamese'
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
LIBRE_LANGUAGES_TO_CODES = {
|
| 283 |
+
v: k for k, v in LIBRE_CODE_TO_LANGUAGE.items()
|
| 284 |
+
}
|
deep_translator/libre.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
LibreTranslate API
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from requests.models import Response
|
| 6 |
+
from .parent import BaseTranslator
|
| 7 |
+
import requests
|
| 8 |
+
from bs4 import BeautifulSoup
|
| 9 |
+
from .constants import LIBRE_LANGUAGES_TO_CODES, LIBRE_CODES_TO_LANGUAGES
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class LibreTranslator(BaseTranslator):
|
| 13 |
+
"""
|
| 14 |
+
class that wraps functions, which use libre translator under the hood to translate text(s)
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def __init__(self, base_url, api_key, source="auto", target="en", proxies=None, **kwargs):
|
| 18 |
+
"""
|
| 19 |
+
@param source: source language to translate from
|
| 20 |
+
@param target: target language to translate to
|
| 21 |
+
"""
|
| 22 |
+
_languages = LIBRE_LANGUAGES_TO_CODES
|
| 23 |
+
supported_languages = list(_languages.keys())
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
@staticmethod
|
| 27 |
+
def get_supported_languages(as_dict=False, **kwargs):
|
| 28 |
+
"""
|
| 29 |
+
return the supported languages by the libre translator
|
| 30 |
+
@param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
|
| 31 |
+
@return: list or dict
|
| 32 |
+
"""
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def _map_language_to_code(self, *languages):
|
| 36 |
+
"""
|
| 37 |
+
map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
|
| 38 |
+
@param languages: list of languages
|
| 39 |
+
@return: mapped value of the language or raise an exception if the language is not supported
|
| 40 |
+
"""
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
+
def is_language_supported(self, language, **kwargs):
|
| 44 |
+
"""
|
| 45 |
+
check if the language is supported by the translator
|
| 46 |
+
@param language: a string (if 1 lang) or a list (if multiple langs)
|
| 47 |
+
@return: bool or raise an Exception
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
def translate(self, text, **kwargs):
|
| 51 |
+
"""
|
| 52 |
+
function that uses microsoft translate to translate a text
|
| 53 |
+
@param text: desired text to translate
|
| 54 |
+
@return: str: translated text
|
| 55 |
+
"""
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
response = requests.post("https://libretranslate.de/detect",data={'q': 'bonjour'})
|
| 59 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 60 |
+
print(soup)
|
examples/libre.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from deep_translator import LibreTranslator
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# res = LibreTranslator(source='de', target='en').translate('laufen', return_all=False)
|
| 6 |
+
|
| 7 |
+
# print(res)
|