Kirill commited on
Commit
d3bc8a9
·
1 Parent(s): 15d2b76

changes for Microsoft translator including updated readme and added tests

Browse files
README.rst CHANGED
@@ -431,6 +431,51 @@ Yandex Translator
431
 
432
  translated = YandexTranslator('your_api_key').translate_batch(source="auto", target="de", batch=["hello world", "happy coding"])
433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
 
435
  Usage from Terminal
436
  --------------------
 
431
 
432
  translated = YandexTranslator('your_api_key').translate_batch(source="auto", target="de", batch=["hello world", "happy coding"])
433
 
434
+ Microsoft Translator
435
+ ------------------
436
+
437
+ .. note::
438
+
439
+ You need to require an **api key** if you want to use the microsoft translator.
440
+ visit the official website for more information about how to get one.
441
+ Microsoft offers a free tier 0 subscription (2 million characters per month).
442
+
443
+ - Required and optional attributes
444
+
445
+ There are two required attributes, namely "api_key" (string) and "target" (string or list).
446
+ Attribute "source" is optional.
447
+ Also, Microsoft API accepts a number of other optional attributes, you can find them here: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-translate
448
+ You can simply add them after the required attributes, see the example.
449
+
450
+ .. code-block:: python
451
+
452
+ text = 'happy coding'
453
+ translated = MicrosoftTranslator(api_key='some-key', target='de').translate(text=text)
454
+ translated_two_targets = MicrosoftTranslator(api_key='some-key', target=['de', 'ru']).translate(text=text)
455
+ translated_with_optional_attr = MicrosoftTranslator(api_key='some-key', target='de', textType='html']).translate(text=text)
456
+
457
+ - You can pass languages by name or by abbreviation:
458
+
459
+ .. code-block:: python
460
+
461
+ translated = MicrosoftTranslator(api_key='some-key', target='german').translate(text=text)
462
+
463
+ # Alternatively, you can pass languages by their abbreviation:
464
+ translated = MicrosoftTranslator(api_key='some-key', target='de').translate(text=text)
465
+
466
+ - Translate batch of texts
467
+
468
+ .. code-block:: python
469
+
470
+ texts = ["hallo welt", "guten morgen"]
471
+ translated = MicrosoftTranslator(api_key='some-key', target='english').translate_batch(texts)
472
+
473
+ - Translate from a file:
474
+
475
+ .. code-block:: python
476
+
477
+ translated = MicrosoftTranslator(api_key='some-key', target='german').translate_file('path/to/file')
478
+
479
 
480
  Usage from Terminal
481
  --------------------
deep_translator/tests/test_microsoft_trans.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ """Tests for `deep_translator` package."""
4
+
5
+ import pytest
6
+ from unittest.mock import patch
7
+ import requests
8
+
9
+ from deep_translator import exceptions, MicrosoftTranslator
10
+
11
+
12
+ # mocked request.post
13
+ @patch.object(requests, 'post')
14
+ def test_microsoft_successful_post_mock(mock_request_post):
15
+ returned_json = [{'translations': [{'text': 'See you later!', 'to': 'en'}]}]
16
+ def res():
17
+ r = requests.Response()
18
+ def json_func():
19
+ return returned_json
20
+ r.json = json_func
21
+ return r
22
+ mock_request_post.return_value = res()
23
+ assert MicrosoftTranslator(api_key="an_api_key", target="en").translate("auf wiedersehen!") == "See you later!"
24
+
25
+
26
+ def test_MicrosoftAPIerror():
27
+ with pytest.raises(exceptions.MicrosoftAPIerror):
28
+ MicrosoftTranslator(api_key="empty", target="en").translate("text")
29
+
30
+
31
+ # the remaining tests are actual requests to Microsoft API and use an api key
32
+ # if APIkey variable is None, they are skipped
33
+
34
+ APIkey = None
35
+
36
+
37
+ @pytest.mark.skipif(APIkey is None, reason="api_key is not provided")
38
+ def test_microsoft_successful_post_onetarget():
39
+ posted = MicrosoftTranslator(api_key=APIkey, target="en").translate("auf wiedersehen!")
40
+ assert isinstance(posted, str)
41
+
42
+
43
+ @pytest.mark.skipif(APIkey is None, reason="api_key is not provided")
44
+ def test_microsoft_successful_post_twotargets():
45
+ posted = MicrosoftTranslator(api_key=APIkey, target=["en", "ru"]).translate("auf wiedersehen!")
46
+ assert isinstance(posted, str)
47
+
48
+
49
+ @pytest.mark.skipif(APIkey is None, reason="api_key is not provided")
50
+ def test_incorrect_target_attributes():
51
+ with pytest.raises(exceptions.ServerException):
52
+ MicrosoftTranslator(api_key=APIkey, target="")
53
+ with pytest.raises(exceptions.ServerException):
54
+ MicrosoftTranslator(api_key="", target="nothing")
55
+
56
+
57
+ @pytest.mark.skipif(APIkey is None, reason="api_key is not provided")
58
+ def test_abbreviations():
59
+ m1 = MicrosoftTranslator(api_key=APIkey, source="en", target="fr")
60
+ m2 = MicrosoftTranslator(api_key=APIkey, source="English", target="French")
61
+ assert ''.join(m1.source) == ''.join(m2.source)
62
+ assert ''.join(m1.target) == ''.join(m2.target)