File size: 2,808 Bytes
77f147b
 
5cbd28e
 
77f147b
1f09504
77f147b
a6888ed
77f147b
 
55109ff
77f147b
 
 
 
 
1f09504
 
b046d4e
 
77f147b
 
 
 
 
 
 
1f09504
 
77f147b
 
1f09504
 
 
 
 
 
 
 
77f147b
b5fa991
 
 
1f09504
b5fa991
 
77f147b
 
 
 
 
1f09504
 
 
77f147b
1f09504
 
 
77f147b
55109ff
77f147b
 
b046d4e
1f09504
 
77f147b
 
1f09504
77f147b
1f09504
77f147b
1f09504
 
 
 
 
af08fe3
1f09504
 
 
 
55109ff
77f147b
 
b046d4e
77f147b
 
 
55109ff
77f147b
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

import requests
from .constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
from .exceptions import (ServerException, TranslationNotFound)

class QCRI(object):
    """
    class that wraps functions, which use the QRCI translator under the hood to translate word(s)
    """

    def __init__(self, api_key=None, source="en", target="en", **kwargs):
        """
        @param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref
        """

        if not api_key:
            raise ServerException(401)
        self.__base_url = BASE_URLS.get("QCRI")
        self.source = source
        self.target = target
        self.api_key = api_key
        self.api_endpoints = {
            "get_languages": "getLanguagePairs",
            "get_domains": "getDomains",
            "translate": "translate",
        }

        self.params = {
            "key": self.api_key
        }

    def _get(self, endpoint, params=None, return_text=True):
        if not params:
            params = self.params
        try:
            res = requests.get(self.__base_url.format(endpoint=self.api_endpoints[endpoint]), params=params)
            return res.text if return_text else res
        except Exception as e:
            raise e

    def get_supported_languages(self, **kwargs):
        # Have no use for this as the format is not what we need
        # Save this for whenever
        pairs = self._get("get_languages")
        # Using a this one instead
        return QCRI_LANGUAGE_TO_CODE

    @property
    def languages(self):
        return self.get_supported_languages()

    def get_domains(self):
        domains = self._get("get_domains")
        return domains

    @property
    def domains(self):
        return self.get_domains()

    def translate(self, text, domain, **kwargs):
        params = {
            "key": self.api_key,
            "langpair": "{}-{}".format(self.source, self.target),
            "domain": domain,
            "text": text
        }
        try:
            response = self._get("translate", params=params, return_text=False)
        except ConnectionError:
            raise ServerException(503)

        else:
            if response.status_code != 200:
                ServerException(response.status_code)
            else:
                res = response.json()
                translation = res.get("translatedText")
                if not translation:
                    raise TranslationNotFound(text)
                return translation

    def translate_batch(self, batch, domain, **kwargs):
        """
        translate a batch of texts
        @domain: domain
        @param batch: list of texts to translate
        @return: list of translations
        """
        return [self.translate(domain, text, **kwargs) for text in batch]