来自 Microsoft Translate API 的 Python 响应 [英] Response from Microsoft Translate API in Python

查看:16
本文介绍了来自 Microsoft Translate API 的 Python 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试创建一个软件,可以记录一些语音,将语音转换为文本,并将该文本翻译成另一种语言.到目前为止,我已经完成了前两个目标,但我一直在为翻译而苦苦挣扎.

I've recently been trying to create a some software that will record some speech, change the speech to text, and translate that text to another language. So far, I have accomplished the first two objectives, but I have been really struggling with translation.

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

I have been trying to use the Microsoft Translator API, and have followed all of the instructions in setting my environment up. I set up a Microsoft Azure Marketplace account, set up a project, enabled the API, and I have been able to use a simple bash command to get my access token:

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

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

I have written a small python script using the requests and argparse libraries that sends the request:

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 上的所有示例都使用微软正在停止作为授权链接的过时DataMarket网站.此外,我找不到任何实际使用的 API 示例——它们都只是授权示例.将令牌与他们的试用"示例一起使用给了我正确的结果(尽管作为 xml 文件?),所以这绝对是一个 python 问题.

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?

谢谢!

推荐答案

我试图重现您的问题,但失败了,我的示例代码工作正常,通过遵循文档用于 Microsoft 认知服务翻译器 API 的身份验证令牌 API &文本翻译 API /Translate.

I tried to reproduce your issue, but failed, and my sample code works fine, which be writen via follow the documents Authentication Token API for Microsoft Cognitive Services Translator API & Text Translation API /Translate.

作为参考,这里是我在 Python 中的示例代码,以及它下面的输出.

As reference, here is my sample code in Python, and the output under it.

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>

希望有帮助.

这篇关于来自 Microsoft Translate API 的 Python 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆