来自Microsoft Translate API的Python响应

罗曼

我最近一直在尝试创建一些软件,该软件将记录一些语音,将语音更改为文本,然后将该文本翻译为另一种语言。到目前为止,我已经实现了前两个目标,但是我一直在努力地进行翻译。

我一直在尝试使用Microsoft Translator API,并按照所有说明进行环境设置。我设置了一个Microsoft Azure Marketplace帐户,设置了一个项目,启用了API,并且能够使用一个简单的bash命令来获取访问令牌:

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

我使用请求和发送请求的argparse库编写了一个小的python脚本:

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

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

一切似乎都进行得很顺利,我得到了200条响应(我收集的结果表示成功),但是当我尝试查看响应中的文本时,会打印出数百行晦涩的html。在浏览了几百行之后(大部分情况下列出了我选择不将文本翻译成的几十种语言),我找不到任何实际翻译的文本。Microsoft在其github上拥有的所有示例均使用Microsoft正在停止使用的过时的DataMarket网站作为授权链接。而且,我找不到实际使用的API的任何示例-它们全都是授权示例。将令牌与他们的“试用”示例结合使用可为我提供正确的结果(尽管是xml文件?),所以这绝对是python问题。

因此,有没有人曾经使用过这项服务,并且介意对如何解释或解开此响应进行一些了解?

谢谢!

彼得潘

我试图重现您的问题,但是失败了,并且我的示例代码可以正常工作,可通过遵循文档Microsoft认知服务翻译器API文本翻译API的/TranslateAuthentication Token API进行编写

作为参考,这是我的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>

希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章