0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Microsoft Translator テキスト API の使い方 (python3)

Last updated at Posted at 2018-02-26

Microsoft Translator テキスト API の使用例です。
日本語から、ドイツ語への翻訳です。

translation.py
# ! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	translation.py
#
#                   Feb/26/2018
#
# ------------------------------------------------------------------
import  sys
import  requests
from get_token import get_token_proc
# ------------------------------------------------------------------
def translate_proc(token,text_in):
	url = "https://api.microsofttranslator.com/v2/http.svc/Translate?text="
	url += text_in + "&to=de" + "&category=generalnn"
#
	headers = {"Authorization": "Bearer " + token}

	rr=requests.get(url,headers=headers)
	sys.stderr.write(str(rr.status_code) + "\n")
#
	return rr.text

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
token = get_token_proc()

text_in="今日はいい天気です。"
print(text_in)
result = translate_proc(token,text_in)
print(result)

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

次の key1 は、自分のものと置き換えて下さい。

get_token.py
# -*- coding: utf-8 -*-
#
#	get_token.py
#
#                   Sep/21/2017
#
# ------------------------------------------------------------------
import  sys
import  requests
# ------------------------------------------------------------------
def get_token_proc():
	key1 = "e968e25ffc2146f00000000000000aaa"
	url="https://api.cognitive.microsoft.com/sts/v1.0/issueToken"

	headers= {"Ocp-Apim-Subscription-Key": key1}

	rr = requests.post(url,headers=headers,data="")
	sys.stderr.write(str(rr.status_code) + "\n")
#
	return rr.text
# ------------------------------------------------------------------
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?