-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsmt.py
240 lines (187 loc) · 9.5 KB
/
msmt.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/python
import urllib, urllib2
import json
import re
import requests
from lxml import etree
from datetime import datetime
def datestring(display_format="%a, %d %b %Y %H:%M:%S", datetitme_object=None):
"""Convert the datetime object (defaults to now, in utc) into a string, in the given display format"""
datetime_object = datetime.utcnow()
return datetime.strftime(datetime_object, display_format)
def get_access_token (client_id, client_secret):
"""Make an HTTP POST request to the token service, and return the access_token,
as described in number 3, here: http://msdn.microsoft.com/en-us/library/hh454949.aspx
"""
data = urllib.urlencode({
'client_id' : client_id,
'client_secret' : client_secret,
'grant_type' : 'client_credentials',
'scope' : 'http://api.microsofttranslator.com'
})
try:
request = urllib2.Request('https://datamarket.accesscontrol.windows.net/v2/OAuth2-13')
request.add_data(data)
#request = urllib2.Request("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13")
#request = urllib2.Request("http://api.microsofttranslator.com/v2/Http.svc/Translate")
#request = urllib2.Request("http://api.microsofttranslator.com/V2/Http.svc/Translate")
#request = urllib2.Request("http://api.microsofttranslator.com/V2/Http.svc/Translate")
response = urllib2.urlopen(request)
response_data = json.loads(response.read())
if response_data.has_key('access_token'):
return response_data['access_token']
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print datestring(), 'Could not connect to the server:', e.reason
elif hasattr(e, 'code'):
print datestring(), 'Server error: ', e.code
except TypeError:
print datestring(), 'Bad data from server'
def to_bytestring(s):
"""Convert the given unicode string to a byetstring, using utf-8 encoding,
unless it's already a bytestring"""
if s:
if isinstance(s, str):
return s
else:
return s.encode("utf-8")
def translate (access_token, text, to_lang, from_lang=None):
"""Use the HTTP Interface to translate text, as described here:
http://msdn.microsoft.com/en-us/library/ff512387.aspx
and return an xml string if successful
"""
if not access_token:
print 'Sorry, the access token is invalid'
else:
data = { 'text' : to_bytestring(text), 'to' : to_lang }
data['from'] = from_lang
try:
#request = urllib2.Request("http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + urllib.urlencode(text) + "&from=" + from_lang + "&to=" + to_lang)
#print "request:", request.get_full_url()
#request = urllib2.Request("http://api.microsofttranslator.com/V2/Http.svc/Translate?" + urllib.urlencode(data))
request = urllib2.Request('http://api.microsofttranslator.com/v2/Http.svc/Translate?'+urllib.urlencode(data))
request.add_header('Authorization', 'Bearer '+access_token)
#request.add_header('Authorization', access_token)
#print "Request:", request.get_full_url()
response = urllib2.urlopen(request)
xml = response.read()
#return get_text_from_msmt_xml(xml)
return xml
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print datestring(), 'Could not connect to the server:', e.reason
elif hasattr(e, 'code'):
print datestring(), 'Server error: ', e.code
def translateArray(access_token, texts, to_lang, from_lang=None):
translateArraySourceTexts = texts
uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray2";
body = ""
body += "<TranslateArrayRequest>"
body += "<AppId />"
body += "<From>" + from_lang + "</From>"
body += "<Options>"
body += " <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />"
body += "<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">text/plain</ContentType>"
body += "<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />"
body += "<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />"
body += "<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />"
body += "<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />"
body += "</Options>"
body += "<Texts>"
for text in texts:
body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">" + text + "</string>"
#body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{3}</string>"
#body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{4}</string>"
body += "</Texts>"
body += "<To>" + to_lang + "</To>"
body += "</TranslateArrayRequest>"
#print "from_lang:", from_lang
#reqBody = body.format(body, from_lang, "text/plain", translateArraySourceTexts[0], to_lang);
#print body
#request.add_header
headers = {'Authorization': 'Bearer '+access_token, "content-type": "text/xml"}
response = requests.post(uri, data=body, headers=headers)
xml = response.content
return xml
def translateArray2(access_token, texts, to_lang, from_lang=None):
"""Use the HTTP Interface to translate text, as described here:
http://msdn.microsoft.com/en-us/library/ff512387.aspx
and return an xml string if successful
"""
if not access_token:
print 'Sorry, the access token is invalid'
else:
#data = { 'texts' : texts, 'to' : to_lang }
#data['from'] = from_lang
headers = {'Authorization': 'Bearer '+access_token, "content-type": "application/json" }
options = {
'Category': "general",
'Contenttype': "text/plain",
'Uri': '',
'User': 'default',
'State': ''
}
params = {
'texts': json.dumps(texts),
'to': to_lang,
'from': from_lang,
'options': json.dumps(options),
}
try:
#translate_array_url = "http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray"
translate_array_url = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray"
#url = translation_url = translate_array_url + "?" + urllib.urlencode(params)
response = requests.post(url=translate_array_url, data=params, headers=headers)
#request = urllib2.Request(translate_array_url, data=urllib.urlencode(data), headers=headers)
#print "Request:", request.get_full_url()
#response = urllib2.urlopen(request)
#xml = response.read()
return response.content
#return get_text_from_msmt_xml(xml)
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print datestring(), 'Could not connect to the server:', e.reason
elif hasattr(e, 'code'):
print datestring(), 'Server error: ', e.code
def get_tr(xml):
translations = []
doc = etree.fromstring(xml)
respArray = doc.findall("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}TranslateArray2Response")
print "len(respArray):", len(respArray)
for resp in respArray:
translatedText = resp.find("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}TranslatedText")
try:
translations.append(translatedText.xpath("./text()")[0])
except IndexError:
translations.append("")
return translations
#return re.findall("<TranslatedText>(.*?)</TranslatedText>", xml)
def get_alignment(xml):
alignments = []
doc = etree.fromstring(xml)
respArray = doc.findall("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}TranslateArray2Response")
for resp in respArray:
alignment = resp.find("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}Alignment")
#print alignment
try:
alignments.append(alignment.xpath("./text()")[0])
except IndexError:
alignments.append("")
return alignments
#return re.findall("<Alignment>(.*?)</Alignment>", xml)
"""
translateArrayResponse.find("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}Alignment")
alignments = translateArrayResponse.findall("{http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2}Alignment")
alignments[0].xpath('./text()')
"""
def get_text_from_msmt_xml (xml):
"""Parse the xml string returned by the MS machine translation API, and return just the text"""
text = []
doc = etree.fromstring(xml)
#for elem in doc.xpath('/foo:string', namespaces={'foo': 'http://schemas.microsoft.com/2003/10/Serialization/'}):
for elem in doc.xpath('/TranslatedText'):
if elem.text:
elem_text = ' '.join(elem.text.split())
if len(elem_text) > 0:
text.append(elem_text)
return ' '.join(text)