2017-01-22 24 views
3

私は最近、スピーチを録音し、スピーチをテキストに変更し、そのテキストを別の言語に翻訳するソフトウェアを作成しようとしています。これまでのところ、私は最初の2つの目標を達成しましたが、私は本当に翻訳に苦労しています。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の行が出力されます。数百の行(ほとんどの場合、私がテキストを翻訳しないことを選んだ何十もの言語を列挙していた)を見て、私は実際に翻訳されたテキストを見つけることができませんでした。 Microsoftがtheir githubに持っているすべての例は、Microsoftが承認リンクとして廃止中の古いDataMarket Webサイトを使用しています。さらに、実際に使用されている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

それは大いに役立ちます、ありがとうございます!問題はリクエストのヘッダー引数を含まず、 'params'引数だけだったため、Microsoftから戻ってきた出力は 'application/xml'形式ではなくhtmlの大部分でした。ありがとうございました! – Roman