-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
73 lines (62 loc) · 2.1 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!python3
# -*- coding: utf-8 -*-
import configparser
import os, requests, uuid, json
from utils import chunkstring, read_txt_to_string_in_chunks, write_string_to_txt_file
# https://docs.microsoft.com/en-us/azure/cognitive-services/Translator/quickstart-translate?pivots=programming-language-python
def translate(text):
# grab MS credentials:
config = configparser.ConfigParser()
config.read("config.cnf")
subscription_key = config.get('Translation', 'API_key')
endpoint = config.get('Translation', 'endpoint_url')
path = '/translate?api-version=3.0'
params = '&to=en'
constructed_url = endpoint + path + params
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
translated_text = ''
for chunk in chunkstring(text):
text_to_translate = [{
'text': chunk
}]
# post request
request = requests.post(constructed_url, headers=headers, json=text_to_translate)
response = request.json()
try:
translated_text += json.loads(
json.dumps(
response,
sort_keys=True,
indent=4,
ensure_ascii=False,
separators=(',', ': ')))[0]['translations'][0]['text']
except KeyError: # doesn't seem to be critical.
print('skipping these many characters: ', len(chunk))
print('keyerror, whatever that means.')
continue
return translated_text
'''
# example output from endpoint:
[
{
"detectedLanguage": {
"language": "en",
"score": 1.0
},
"translations": [
{
"text": "Hallo Welt!",
"to": "de"
},
{
"text": "Salve, mondo!",
"to": "it"
}
]
}
]
'''