File size: 5,094 Bytes
1d42175
 
 
 
 
5a8d2be
 
 
 
 
9a69d6b
 
1d42175
 
 
 
 
 
5a8d2be
 
1d42175
50c16a8
1d42175
 
5a8d2be
 
1d42175
 
5a8d2be
 
 
 
9a69d6b
 
 
 
5a8d2be
 
 
1d42175
 
 
 
 
 
 
5a8d2be
1d42175
5a8d2be
1d42175
 
5a8d2be
1d42175
 
5a8d2be
 
 
 
 
1d42175
5a8d2be
1d42175
 
5a8d2be
1d42175
 
9a69d6b
 
 
 
1d42175
 
 
 
 
 
 
5a8d2be
9a69d6b
 
 
5a8d2be
 
 
 
 
50c16a8
5a8d2be
50c16a8
 
 
5a8d2be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50c16a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
LibreTranslate API
"""

import requests
from .parent import BaseTranslator
from .constants import BASE_URLS,LIBRE_LANGUAGES_TO_CODES, LIBRE_CODES_TO_LANGUAGES
from .exceptions import (ServerException,
                        TranslationNotFound,
                        LanguageNotSupportedException,
                        AuthorizationException,
                        NotValidPayload)


class LibreTranslator(BaseTranslator):
    """
    class that wraps functions, which use libre translator under the hood to translate text(s)
    """
    _languages = LIBRE_LANGUAGES_TO_CODES
    supported_languages = list(_languages.keys())

    def __init__(self,source="auto", target="en", base_url = BASE_URLS.get("LIBRE_FREE"), api_key=None, **kwargs):
        """
        @param source: source language to translate from
        List of LibreTranslate nedpoints can be found at : https://github.com/LibreTranslate/LibreTranslate#mirrors
        Some require an API key
        @param target: target language to translate to
        """
        if base_url == BASE_URLS.get("LIBRE") and not api_key:
            raise ServerException(401)
        self.__base_url = base_url
        self.api_key = api_key
        if source == "auto":
            self.source = "auto"
        else:
            self.source = self._map_language_to_code(source)
        self.target = self._map_language_to_code(target)


    @staticmethod
    def get_supported_languages(as_dict=False, **kwargs):
        """
        return the supported languages by the libre translator
        @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
        @return: list or dict
        """
        return [*LibreTranslator._languages.keys()] if not as_dict else LibreTranslator._languages
    
    def _map_language_to_code(self, language, **kwargs):
        """
        map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
        @param language: a string for 1 language
        @return: mapped value of the language or raise an exception if the language is not supported
        """
        if language in self._languages.keys():
            return self._languages[language]
        elif language in self._languages.values():
            return language
        raise LanguageNotSupportedException(language)
    
    def _is_language_supported(self, language, **kwargs):
        """
        check if the language is supported by the translator
        @param language: a string for 1 language
        @return: bool or raise an Exception
        """
        if language == 'auto' or language in self._languages.keys() or language in self._languages.values():
            return True
        else:
            raise LanguageNotSupportedException(language)
    
    def translate(self, text, **kwargs):
        """
        function that uses microsoft translate to translate a text
        @param text: desired text to translate
        @return: str: translated text
        """
         # Create the request parameters.
        if type(text) != str or text == "":
            raise NotValidPayload(text)

        translate_endpoint = 'translate'
        params = {
            "q": text,
            "source": self.source,
            "target": self.target,
            "format": 'text'
        }
        # Add API Key if required
        if self.api_key: 
            params["api_key"] = self.api_key
        # Do the request and check the connection.
        try:
            response = requests.post(self.__base_url + translate_endpoint, params=params)
        except ConnectionError:
            raise ServerException(503)
        # If the answer is not success, raise server exception.
        
        if response.status_code == 403:
            raise AuthorizationException(self.api_key)
        elif response.status_code != 200:
            raise ServerException(response.status_code)
        # Get the response and check is not empty.
        res = response.json()
        if not res:
            raise TranslationNotFound(text)
        # Process and return the response.
        return res['translatedText']

    def translate_file(self, path, **kwargs):
        """
        translate directly from file
        @param path: path to the target file
        @type path: str
        @param kwargs: additional args
        @return: str
        """
        try:
            with open(path) as f:
                text = f.read().strip()
            return self.translate(text)
        except Exception as e:
            raise e

    def translate_batch(self, batch=None, **kwargs):
        """
        translate a list of texts
        @param batch: list of texts you want to translate
        @return: list of translations
        """
        if not batch:
            raise Exception("Enter your text list that you want to translate")
        arr = []
        for i, text in enumerate(batch):
            translated = self.translate(text, **kwargs)
            arr.append(translated)
        return arr