forked from yskim1501/Hackathon2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msft_translate.py
executable file
·53 lines (36 loc) · 1.72 KB
/
msft_translate.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
# -*- coding: utf-8 -*-
import http.client, urllib.parse, uuid, json
class DetectLanguage():
# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = '980ff8a9930940afb0e6465965d74832'
host = 'api.cognitive.microsofttranslator.com'
path = '/translate?api-version=3.0'
host = 'api.cognitive.microsofttranslator.com'
path = '/breaksentence?api-version=3.0'
params = ''
#Let's make this text dynamic.
text = 'bonjour, je suis Pranav'
def breakSentences (content):
host = 'api.cognitive.microsofttranslator.com'
path = '/translate?api-version=3.0'
headers = {
'Ocp-Apim-Subscription-Key': '980ff8a9930940afb0e6465965d74832',
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
conn = http.client.HTTPSConnection(host)
conn.request ("POST", path + params, content, headers)
response = conn.getresponse ()
return response.read ()
requestBody = [{
'Text' : text,
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
result = breakSentences (content)
# Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
# We want to avoid escaping any Unicode characters that result contains. See:
# https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
output = json.dumps(json.loads(result), indent=4, ensure_ascii=False)
print(output)
q = DetectLanguage
q.breakSentences("Hola")