2017-01-22 91 views
3

我最近一直在嘗試創建一些軟件來記錄某些語音,將語音更改爲文本並將該文本翻譯爲另一種語言。到目前爲止,我已經完成了前兩個目標,但我一直在努力翻譯。Python中的Microsoft Translate API的響應

我一直在嘗試使用Microsoft Translator API,並按照所有說明來設置我的環境。我設置了一個微軟的Azure Marketplace帳戶,建立一個項目,使API,我已經能夠用簡單的bash命令,讓我的訪問令牌:

curl --data "" 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=mySubscriptionKey' 

我一直在使用寫了一個小python腳本發送請求的請求和argparse庫:

request = { 
    'appid': ('Bearer ' + token), 
    'text' : txt, 
    'from' : 'en', 
    'to' : 'fr' 
} 

response = requests.get('https://api.microsofttranslator.com/v2/http.svc', params = request) 

似乎一切都順利,我也得到一個200響應(這是我收集意味着成功),但是當我嘗試看看文中的響應,數百行不明確的html被打印出來。在查看了幾百行(大部分是列出數十種語言,我選擇不將其翻譯爲我的文本)後,我找不到任何實際翻譯的文本。微軟在their github上的所有例子都使用過時的DataMarket網站,微軟正在停止授權鏈接。而且,我找不到實際使用的API的任何示例 - 它們都只是授權示例。使用帶有「Try it Out」示例的標記爲我提供了正確的結果(儘管它是一個xml文件?),所以這絕對是一個python問題。

那麼,有沒有人使用過這個服務,並且介紹瞭如何解釋或解開這個響應的一些想法?

謝謝!

回答

3

我試圖重現您的問題,但失敗了,我的示例代碼工作正常,這是通過遵循文檔 Authentication Token API for Microsoft Cognitive Services Translator API & Text Translation API /Translate寫。

作爲參考,這裏是我在Python中的示例代碼,以及它下面的輸出。

import requests 

# Getting the key from tab Keys on Azure portal 
key = "xxxxxxxxxxxxxxxxxxxxxxx" 

# For gettting access token 
# url4authentication = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=%s' % key 
# resp4authentication = requests.post(url4authentication) 

url4authentication = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken' 
headers4authentication = {'Ocp-Apim-Subscription-Key': key} 
resp4authentication = requests.post(url4authentication, headers=headers4authentication) 
token = resp4authentication.text 

# For calling Translate API 
#text = "happy time" 
text = """ 
Everything seems to go smoothly, and I get a 200 response (which I gather means success), yet when I try to look at the text in the response, hundreds of lines of obscure html are printed out. After looking through a few hundred of the lines (which were, for the most part, listing the dozens of languages I chose NOT to translate my text into) I couldn't find any actually translated text. All of the examples that Microsoft has on their github use the outdated DataMarket website that Microsoft is in the process of discontinuing as the authorization link. Moreover, I couldn't find any examples of the API actually being used - they were all just authorization examples. Using the token with their 'Try it Out' example gives me the correct result (though as an xml file?), so this is definitely a python problem. 

So, has anyone used this service before and mind shedding some light on how to interpret or unwrap this response? 

Thank you! 
""" 
come = "en" 
to = "fr" 
# url4translate = 'https://api.microsofttranslator.com/v2/http.svc/Translate?appid=Bearer %s&text=%s&from=%s&to=%s' % (token, text, come, to) 
# headers4translate = {'Accept': 'application/xml'} 
# resp4translate = requests.get(url4translate, headers=headers4translate) 
url4translate = 'https://api.microsofttranslator.com/v2/http.svc/Translate' 
params = {'appid': 'Bearer '+token, 'text': text, 'from': come, 'to': to} 
headers4translate = {'Accept': 'application/xml'} 
resp4translate = requests.get(url4translate, params=params, headers=headers4translate) 
print(resp4translate.text) 

輸出:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"> 
Tout semble aller en douceur, et je reçois une réponse 200 (qui je suppose signifie succès), mais lorsque j’essaie de regarder le texte dans la réponse, des centaines de lignes html obscur sont imprimés. Après avoir regardé à travers quelques centaines des lignes (qui étaient, pour la plupart, répertoriant des dizaines de langues, en que j’ai choisi de ne pas traduire mon texte) je ne pouvais pas trouver n’importe quel texte en fait traduit. Tous les exemples que Microsoft a sur leur github utilisent le site DataMarket dépassé que Microsoft est en train d’interrompre le lien d’autorisation. En outre, je ne pouvais pas trouver des exemples de l’API effectivement utilisés - ils étaient tous exemples juste autorisation. En utilisant le jeton avec leur exemple « Essayer » me donne un résultat correct (même si, comme un fichier xml ?), donc c’est certainement un problème de python. 

Ainsi, quiconque a utilisé ce service avant et l’esprit certains éclairant sur la façon d’interpréter ou de dérouler cette réponse ? 

Merci ! 
</string> 

希望它能幫助。

+0

非常有幫助,謝謝!問題是我沒有在請求中包含'headers'參數,但只包含'params'參數,所以我從Microsoft獲得的輸出是大量的html,而不是'application/xml'格式。再次感謝你! – Roman