Translator Text API を使った簡単なアプリを作ってみよう

 

Microsoft Japan Data Platform Tech Sales Team

倉重秀昭

 

先日の記事で Cognitive Services のTranslator Text APIについて紹介しましたが、今回は Cognitive Services の Translator API を使ったアプリケーションの開発方法について紹介をしたいと思います。

Cognitive Services は REST APIの形で提供していますので、アプリケーションから REST API を呼び出すだけで、翻訳や画像認識などの機能を利用できるようになっています。

 

Translator Text APIの仕様は以下のページに記載されています。

https://docs.microsofttranslator.com/text-translate.html#/

ページ下部の「default」をクリックすると、Translator Text APIのメソッドが一覧で表示されますが、TextからTextに翻訳をする場合には一番上に表示されている「/Translate」を利用します。

 

このメソッドを利用する際のポイントは以下の2つです。

(1) Authorization

Translator Text APIを利用するには、以下の2つのうちいづれかの方法でAuthorizationを行う必要があります。

a.  Access Tokenを利用する方法

b.  httpヘッダーにSubscription Keyを埋め込む方法

今回は、簡単に実装できる 「b. httpヘッダーにSubscription Keyを埋め込む方法」でAuthorizationを行いたいと思います。

 

(2) 入力パラメータ

authorization の方法として「httpヘッダーにSubscription Keyを埋め込む」方式を採用する場合、Translator Text APIを呼び出すときに必要なパラメーターは以下となります。

(パラメーターはGet方式で渡します)

パラメーター名 設定内容
text 翻訳したい文章
from 翻訳前の言語
to 翻訳語の言語
category 利用する翻訳エンジン空白 :統計的機械翻訳generalnn:ニューラルネットワーク

 

 

サンプルアプリケーション

次に Translator Text API を呼び出す簡単なサンプルアプリケーションとコードの内容を紹介したいと思います。

 

サンプルアプリケーションは、以下のように翻訳したい英文を入力すると翻訳語の日本語を返してくれる、シンプルなアプリケーションとなっています。

image

 

 

このアプリケーションのソースコートは以下のようなコードとなります。

using System;
using System.Net;
using System.IO;

namespace Azure_TranslatorTextAPISampl

{
class Program
{
static void Main(string[] args)
{

//翻訳前の言語
string fromLanguage = "en";
// 翻訳語の言語
string toLanguage = "ja";
//利用するモデル  :空白 (=統計的機械翻訳) or generalnn(=ニューラルネットワーク)を指定
string ModelType = "generalnn";
//Translator Text API の Subscription Key(Azure Portal(<https://portal.azure.com)から取得)>
string ocpApimSubscriptionKey = "";

//入力を促すメッセージを表示して、翻訳する文章を入力してもらう
Console.WriteLine("翻訳したい英文を入力してください ");
string inputText = Console.ReadLine();
Console.WriteLine("");

//URIの定義(Getでパラメータを送信する為、URLに必要なパラメーターを付加)
string uri = "https://api.microsofttranslator.com/v2/http.svc/Translate?" +
"&text=" + Uri.EscapeDataString(inputText) +
"&from=" + fromLanguage +
"&to=" + toLanguage +
"&category=" + ModelType;

//httpWebRequestの作成
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

//Authorizationのためにhttpヘッダーにサブスクリプションキーを埋め込む
httpWebRequest.Headers.Add("Ocp-Apim-Subscription-Key:" + ocpApimSubscriptionKey);

WebResponse response = null;
try{

//Translator Text APIへのリクエストを実行して結果を取得
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
System.Runtime.Serialization.DataContractSerializer dcs =
new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
string translationText = (string)dcs.ReadObject(stream);

//コンソールに翻訳結果を表示
Console.WriteLine("翻訳結果:");
Console.WriteLine(translationText);
Console.WriteLine("");
}

}
catch(Exception ex)
{
//エラー処理
Console.WriteLine("エラーが発生しました");
Console.WriteLine(ex.Message);
}
finally
{
if (response != null)
{
response.Close();
response = null;
}

Console.WriteLine("終了するには何かキーを押してください...");
Console.ReadKey();
}
}
}
}

 

このサンプルアプリケーションのプロジェクトファイル(Visual Studio 2015)は以下からダウンロードできますので、興味がある方は、実際にビルドして動かしてみてください。

Azure_TranslatorTextAPISample.zip