Amazon Translate
Mix.install([
{:ex_aws, "~> 2.5", override: true},
{:ex_aws_translate, "~> 0.3"},
{:jason, "~> 1.4"},
{:poison, "~> 5.0"},
{:hackney, "~> 1.20"},
{:sweet_xml, "~> 0.7"},
{:kino, "~> 0.14"}
])
準備
alias ExAws.Translate
認証
access_key_id_input = Kino.Input.password("ACCESS_KEY_ID")
secret_access_key_input = Kino.Input.password("SECRET_ACCESS_KEY")
region_input = Kino.Input.text("REGION", default: "us-east-1")
[
access_key_id_input,
secret_access_key_input,
region_input
]
|> Kino.Layout.grid(columns: 3)
auth_config = [
access_key_id: Kino.Input.read(access_key_id_input),
secret_access_key: Kino.Input.read(secret_access_key_input),
region: Kino.Input.read(region_input)
]
Kino.nothing()
利用可能な言語一覧の取得
languages =
%ExAws.Operation.JSON{
stream_builder: nil,
http_method: :post,
parser: nil,
path: "/",
data: %{},
params: %{},
headers: [
{"Content-Type", "application/x-amz-json-1.1"},
{"X-Amz-Target", "AWSShineFrontendService_20170701.ListLanguages"}
],
service: :translate,
before_request: nil
}
|> ExAws.request!(auth_config)
|> Map.get("Languages")
|> Enum.map(&{&1["LanguageCode"], &1["LanguageName"]})
Enum.count(languages)
言語の選択
src_lang_select = Kino.Input.select("翻訳元言語", languages, default: "en")
src_lang = Kino.Input.read(src_lang_select)
dst_lang_select = Kino.Input.select("翻訳先言語", languages, default: "ja")
dst_lang = Kino.Input.read(dst_lang_select)
翻訳
"Hello"
|> Translate.translate_text(src_lang, dst_lang)
|> ExAws.request!(auth_config)
text_input =
Kino.Input.textarea("TEXT",
default:
~s/Elixir is a dynamic, functional language for building scalable and maintainable applications. Elixir runs on the Erlang VM, known for creating low-latency, distributed, and fault-tolerant systems. These capabilities and Elixir tooling allow developers to be productive in several domains, such as web development, embedded software, data pipelines, and multimedia processing, across a wide range of industries./
)
text_input
|> Kino.Input.read()
|> Translate.translate_text(src_lang, dst_lang)
|> ExAws.request!(auth_config)
|> Map.get("TranslatedText")
# 翻訳元言語の自動識別
text_input
|> Kino.Input.read()
|> Translate.translate_text("auto", dst_lang)
|> ExAws.request!(auth_config)
|> Map.get("TranslatedText")
用語登録
text =
"Temakizushi are cones made of nori seaweed and filled with sushi rice, seafood and vegetables."
text
|> Translate.translate_text("auto", dst_lang)
|> ExAws.request!(auth_config)
|> Map.get("TranslatedText")
sushi_terminology_data =
("en,ja\n" <>
"cones,円錐")
|> Base.encode64()
Translate.import_terminology(
"sushi_terminology",
%{
file: sushi_terminology_data,
format: "CSV"
},
"OVERWRITE"
)
|> ExAws.request!(auth_config)
text
|> Translate.translate_text("auto", dst_lang, terminology_names: ["sushi_terminology"])
|> ExAws.request!(auth_config)
|> Map.get("TranslatedText")