-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_translator.py
63 lines (54 loc) · 1.88 KB
/
ms_translator.py
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
# -*- coding: utf-8 -*-
import uuid
import json
import logging
try:
from httplib import HTTPConnection
except:
from http.client import HTTPConnection
HTTPConnection.debuglevel = 5
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("translator")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
# **********************************************
# *** Update or verify the following values. ***
# **********************************************
# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = '<subscription key>'
# URL
host = 'api.cognitive.microsofttranslator.com'
url_path = '/translate?api-version=3.0'+"&to=zh"
# Translate to Chinese
params = "&to=zh"
headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# Description: translate english sentences into chinese sentences
# Input: original byte stream
# feature flag: feature_flag
# sleep seconds: sleep_seconds
# Output: chinese byte stream
def ms_translate(content_text):
try:
conn = HTTPConnection(host)
conn.connect()
conn.set_debuglevel(1)
text = '[{"Text": "Hello"}]'
conn.putrequest('POST', url_path)
conn.putheader('Content-Length', len(text))
conn.putheader('Ocp-Apim-Subscription-Key', subscriptionKey)
conn.putheader('Content-type', 'application/json')
conn.putheader('X-ClientTraceId', str(uuid.uuid4()))
conn.endheaders()
conn.send(text)
output = json.loads(conn.getresponse())[0]['translations'][0]['text']
print(output)
return output
except Exception as e:
logging.error(e)
if __name__ == '__main__':
print(ms_translate('Hello'))