Rom*_*man 5 python xml api translate azure
我最近一直在尝试创建一个软件,可以录制一些语音,将语音更改为文本,并将该文本翻译为另一种语言。到目前为止,我已经完成了前两个目标,但我一直在翻译方面遇到困难。
我一直在尝试使用Microsoft Translator API,并遵循设置环境时的所有说明。我设置了一个 Microsoft Azure Marketplace 帐户,设置了一个项目,启用了 API,并且我已经能够使用简单的 bash 命令来获取我的访问令牌:
curl --data "" 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=mySubscriptionKey'
Run Code Online (Sandbox Code Playgroud)
我使用 requests 和 argparse 库编写了一个小的 python 脚本来发送请求:
request = {
'appid': ('Bearer ' + token),
'text' : txt,
'from' : 'en',
'to' : 'fr'
}
response = requests.get('https://api.microsofttranslator.com/v2/http.svc', params = request)
Run Code Online (Sandbox Code Playgroud)
一切似乎都很顺利,我得到了 200 个响应(我收集的这意味着成功),但是当我尝试查看响应中的文本时,打印出了数百行晦涩的 html。在浏览了几百行之后(其中大部分列出了我选择不将文本翻译成的数十种语言),我找不到任何实际翻译的文本。Microsoft 在其 github上提供的所有示例均使用过时的 DataMarket 网站,Microsoft 正在停止将该网站作为授权链接。此外,我找不到任何实际使用的 API 示例 - 它们都只是授权示例。将令牌与“Try it Out”示例一起使用可以给我正确的结果(尽管作为 xml 文件?),所以这绝对是一个 python 问题。
那么,是否有人以前使用过这项服务并介意了解如何解释或解开此响应?
谢谢你!
我尝试重现您的问题,但失败了,并且我的示例代码工作正常,这是通过遵循文档\nAuthentication Token API for Microsoft Cognitive Services Translator API & Text Translation API/Translate
编写的。
作为参考,这里是我的 Python 示例代码及其下的输出。
\n\nimport requests\n\n# Getting the key from tab Keys on Azure portal\nkey = "xxxxxxxxxxxxxxxxxxxxxxx" \n\n# For gettting access token\n# url4authentication = \'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=%s\' % key\n# resp4authentication = requests.post(url4authentication)\n\nurl4authentication = \'https://api.cognitive.microsoft.com/sts/v1.0/issueToken\'\nheaders4authentication = {\'Ocp-Apim-Subscription-Key\': key}\nresp4authentication = requests.post(url4authentication, headers=headers4authentication)\ntoken = resp4authentication.text\n\n# For calling Translate API\n#text = "happy time"\ntext = """\nEverything 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.\n\nSo, has anyone used this service before and mind shedding some light on how to interpret or unwrap this response?\n\nThank you!\n"""\ncome = "en"\nto = "fr"\n# url4translate = \'https://api.microsofttranslator.com/v2/http.svc/Translate?appid=Bearer %s&text=%s&from=%s&to=%s\' % (token, text, come, to)\n# headers4translate = {\'Accept\': \'application/xml\'}\n# resp4translate = requests.get(url4translate, headers=headers4translate)\nurl4translate = \'https://api.microsofttranslator.com/v2/http.svc/Translate\'\nparams = {\'appid\': \'Bearer \'+token, \'text\': text, \'from\': come, \'to\': to}\nheaders4translate = {\'Accept\': \'application/xml\'}\nresp4translate = requests.get(url4translate, params=params, headers=headers4translate)\nprint(resp4translate.text)\n
Run Code Online (Sandbox Code Playgroud)\n\n输出:
\n\n<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">\nTout semble aller en douceur, et je re\xc3\xa7ois une r\xc3\xa9ponse 200 (qui je suppose signifie succ\xc3\xa8s), mais lorsque j\xe2\x80\x99essaie de regarder le texte dans la r\xc3\xa9ponse, des centaines de lignes html obscur sont imprim\xc3\xa9s. Apr\xc3\xa8s avoir regard\xc3\xa9 \xc3\xa0 travers quelques centaines des lignes (qui \xc3\xa9taient, pour la plupart, r\xc3\xa9pertoriant des dizaines de langues, en que j\xe2\x80\x99ai choisi de ne pas traduire mon texte) je ne pouvais pas trouver n\xe2\x80\x99importe quel texte en fait traduit. Tous les exemples que Microsoft a sur leur github utilisent le site DataMarket d\xc3\xa9pass\xc3\xa9 que Microsoft est en train d\xe2\x80\x99interrompre le lien d\xe2\x80\x99autorisation. En outre, je ne pouvais pas trouver des exemples de l\xe2\x80\x99API effectivement utilis\xc3\xa9s - ils \xc3\xa9taient tous exemples juste autorisation. En utilisant le jeton avec leur exemple \xc2\xab\xc2\xa0Essayer\xc2\xa0\xc2\xbb me donne un r\xc3\xa9sultat correct (m\xc3\xaame si, comme un fichier xml\xc2\xa0?), donc c\xe2\x80\x99est certainement un probl\xc3\xa8me de python.\n\nAinsi, quiconque a utilis\xc3\xa9 ce service avant et l\xe2\x80\x99esprit certains \xc3\xa9clairant sur la fa\xc3\xa7on d\xe2\x80\x99interpr\xc3\xa9ter ou de d\xc3\xa9rouler cette r\xc3\xa9ponse\xc2\xa0?\n\nMerci\xc2\xa0!\n</string>\n
Run Code Online (Sandbox Code Playgroud)\n\n希望能帮助到你。
\n 归档时间: |
|
查看次数: |
1564 次 |
最近记录: |