AWSのAmazon Translateが拍子抜けするくらい簡単だった

カテゴリー:  Tech タグ:  natural language

仕事で必要になってサンプルでAWSで昨年日本語も使用可能になったAmazon Translateを試してみました。

驚くほど簡単でした。

まず、Pythonにboto3をインストールします。

$ pip install boto3

次にAWSのアクセスキーの設定をやっていなければセットしておきます。

$ aws configure
AWS Access Key ID [None]: <管理コンソースを見てセット>
AWS Secret Access Key [None]: <管理コンソールを見てせっと>
Default region name [None]: ap-northeast-1
Default output format [None]: json

次にAmazon Translateを呼び出すプログラムを作成します。source_textにセットされた 文章を日本語にします。

import boto3

source_text = """
UNESCO decided July 6 to add to the World Heritage list two tumulus clusters in western Japan that represent an ancient burial system and the hierarchy of society at the time.

The sites, comprised of 49 tombs in Osaka Prefecture and collectively called the Mozu-Furuichi tumulus clusters, include the country’s largest keyhole-shaped mound, named after Emperor Nintoku, who is said to have reigned in the fourth century.

The Emperor Nintoku mausoleum, officially called Daisen Kofun, is part of the Mozu cluster in Sakai and is 486 meters long. It is said to be one of the three largest mounded tombs in the world, along with the mausoleum of the first Qin emperor in China and the Great Pyramid of Giza in Egypt.
"""
translate = boto3.client("translate", region_name="us-west-2")
response = translate.translate_text(
    Text=source_text,
    SourceLanguageCode="auto",
    TargetLanguageCode="ja"
)

print(response['TranslatedText'])

出てきた日本語訳は以下のようなものです。

ユネスコは7月6日に、古代埋葬システムと当時の社会の階層を代表する西日本の2つの古墳群を世界遺産に登録することを決定した。

大阪府における 49 墓であり、総称して百津古市古墳群と呼ばれる遺跡には、4 世紀に治世したという仁徳天皇にちなみに名付けられた国内最大の前方円墳がある。

仁徳天皇霊廟は、正式に大山古墳と呼ばれ、堺の百合団体の一部で、全長 486 メートル。 中国初の秦皇帝の霊廟、エジプトのギザ大ピラミッドとともに、世界三大の墓の一つと言われています。

なかなかお手軽でいろんな物に翻訳を組み込みたくなります。

コメント

Comments powered by Disqus