

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Liquid を使用してオートメーションを追加する
<a name="sms-custom-templates-step2-automate"></a>

カスタムテンプレートシステムは、オートメーションに [Liquid](https://shopify.github.io/liquid/) を使用します。これはオープンソースのインラインマークアップ言語です。Liquid では、一重波括弧とパーセント記号で囲まれたテキストは、制御フローやイテレーションなどを実行する 1 つの手順またはタグになります。**二重波括弧で囲まれたテキストは、値を出力する 1 つの変数または*オブジェクト*です。

最も一般的な Liquid の使用法としては、入力マニフェストファイルからのデータを解析して、関連する変数を抽出し、タスクを作成します。Ground Truth は、前注釈の Lambda が指定されていない限り、タスクを自動的に生成します。Ground Truth または [前注釈 Lambda](sms-custom-templates-step3-lambda-requirements.md#sms-custom-templates-step3-prelambda) によって返される `taskInput` オブジェクトは、テンプレートの `task.input` オブジェクトです。

入力マニフェストのプロパティは、`event.dataObject` としてテンプレートに渡されます。

**Example マニフェストデータオブジェクト**  

```
{
  "source": "This is a sample text for classification",
  "labels": [ "angry" , "sad" , "happy" , "inconclusive" ],
  "header": "What emotion is the speaker feeling?"
}
```

**Example 変数を使用したサンプル HTML**  

```
<crowd-classifier 
  name='tweetFeeling'
  categories='{{ task.input.labels | to_json }}'
  header='{{ task.input.header }}' >
<classification-target>
  {{ task.input.source }}
</classification-target>
```

上記の `labels` プロパティに ` | to_json` が追加されている点に注意してください。これは入力マニフェスト配列を JSON 形式に変換するためのフィルターです。可変フィルターについては次のセクションで説明します。

次のリストには、テンプレート入力データの処理を自動化するのに便利な 2 種類の Liquid タグが含まれています。以下のタグタイプのいずれかを選択すると、Liquid のドキュメントにリダイレクトされます。
+ [フロー制御](https://shopify.github.io/liquid/tags/control-flow/): `if/else`、`unless`、`case/when` のようなプログラミングロジック演算子が含まれています。
+ [イテレーション](https://shopify.github.io/liquid/tags/iteration/): FOR LOOP などのステートメントを使用して、コードのブロックを繰り返し実行できます。

  Liquid 要素を使用して FOR LOOP を作成する HTML テンプレートの例については、GitHub で [translation-review-and-correction.liquid.html](https://github.com/aws-samples/amazon-sagemaker-ground-truth-task-uis/blob/8ae02533ea5a91087561b1daecd0bc22a37ca393/text/translation-review-and-correction.liquid.html) を参照してください。

詳細とドキュメントについては、[Liquid のホームページ](https://shopify.github.io/liquid/)を参照してください。

## 可変フィルター
<a name="sms-custom-templates-step2-automate-filters"></a>

標準の [Liquid フィルター](https://shopify.github.io/liquid/filters/abs/)とアクションに加えて、Ground Truth には追加のフィルターがいくつか用意されています。フィルターを適用するには、パイプ (`|`) 文字を変数名の後ろに配置してからフィルター名を指定します。フィルターは次の形式で連鎖できます。

**Example**  

```
{{ <content> | <filter> | <filter> }}
```

### 自動的なエスケープおよび明示的なエスケープ
<a name="sms-custom-templates-step2-automate-filters-autoescape"></a>

デフォルトでは、変数テキストと HTML との間の混乱を避けるために、入力はエスケープされた HTML になります。`escape` フィルターを明示的に追加すると、エスケープ処理の完了しているテンプレートのソースをユーザーが読み取ることをより明確に指定できます。

### escape\$1once
<a name="sms-custom-templates-step2-automate-escapeonce"></a>

`escape_once` を使用すると、既にエスケープしたコードは確実に再度エスケープされなくなります。これにより、例えば、&amp; は &amp;amp; になりません。

### skip\$1autoescape
<a name="sms-custom-templates-step2-automate-skipautoescape"></a>

`skip_autoescape` は、コンテンツが HTML として使用されるよう設計されている場合に便利です。例えば、いくつかの段落のテキストとイメージが境界ボックスの詳細な手順にある場合です。

**`skip_autoescape` の使用は慎重に**  
テンプレートにおけるベストプラクティスは、`skip_autoescape` を使用して関数コードやマークアップを渡さないようにすることです。ただし、渡す内容を厳重に管理できることが確実な場合を除きます。ユーザー入力を渡している場合は、ワーカーをクロスサイトスクリプティング攻撃に対して開放している可能性があります。

### to\$1json
<a name="sms-custom-templates-step2-automate-tojson"></a>

`to_json` は、JSON (JavaScript Object Notation) に供給する内容をエンコードします。オブジェクトを供給している場合は、シリアル化されます。

### grant\$1read\$1access
<a name="sms-custom-templates-step2-automate-grantreadaccess"></a>

`grant_read_access` は S3 URI を使用し、そのリソースに対する存続期間の短いアクセストークンを使用する HTTPS URL にエンコードします。これにより、通常であればパブリックにアクセス可能でない、S3 バケットに保存された写真、音声、または動画オブジェクトをワーカーに表示できるようになります。

### s3\$1presign
<a name="sms-custom-templates-step2-automate-s3"></a>

 `s3_presign` フィルターは `grant_read_access` フィルターと同様に機能します。`s3_presign` は Amazon S3 URI を使用し、そのリソースに対する存続期間の短いアクセストークンを使用する HTTPS URL にエンコードします。これにより、ワーカーに公開していない S3 バケットに保存されている写真、音声、動画などのオブジェクトをワーカーに表示できるようになります。

**Example 変数フィルターの**  
Input  

```
auto-escape: {{ "Have you read 'James & the Giant Peach'?" }}
explicit escape: {{ "Have you read 'James & the Giant Peach'?" | escape }}
explicit escape_once: {{ "Have you read 'James &amp; the Giant Peach'?" | escape_once }}
skip_autoescape: {{ "Have you read 'James & the Giant Peach'?" | skip_autoescape }}
to_json: {{ jsObject | to_json }}                
grant_read_access: {{ "s3://amzn-s3-demo-bucket/myphoto.png" | grant_read_access }}
s3_presign: {{ "s3://amzn-s3-demo-bucket/myphoto.png" | s3_presign }}
```

**Example**  
Output  

```
auto-escape: Have you read &#39;James &amp; the Giant Peach&#39;?
explicit escape: Have you read &#39;James &amp; the Giant Peach&#39;?
explicit escape_once: Have you read &#39;James &amp; the Giant Peach&#39;?
skip_autoescape: Have you read 'James & the Giant Peach'?
to_json: { "point_number": 8, "coords": [ 59, 76 ] }
grant_read_access: https://s3.amazonaws.com/amzn-s3-demo-bucket/myphoto.png?<access token and other params>
s3_presign: https://s3.amazonaws.com/amzn-s3-demo-bucket/myphoto.png?<access token and other params>
```

**Example 自動分類テンプレートの**  
このシンプルなテキスト分類サンプルを自動化するには、ツイートテキストを変数に置き換えます。  
以下のテキスト分類テンプレートには、自動化が追加されています。変更および追加は太字で表示されています。  

```
<script src="https://assets.crowd.aws/crowd-html-elements.js"></script>
<crowd-form>
  <crowd-classifier 
    name="tweetFeeling"
    categories="['positive', 'negative', 'neutral', 'cannot determine']"
    header="Which term best describes this tweet?" 
  >
    <classification-target>
       {{ task.input.source }}
    </classification-target>

    <full-instructions header="Analyzing a sentiment">
      Try to determine the feeling the author 
      of the tweet is trying to express. 
      If none seem to match, choose "other."
    </full-instructions>

    <short-instructions>
      Pick the term best describing the sentiment 
      of the tweet. 
    </short-instructions>

  </crowd-classifier>
</crowd-form>
```
以前のサンプルにあったツイートテキストは、オブジェクトに置き換えられています。`entry.taskInput` オブジェクトは `source` (または前注釈 Lambda で指定する別の名前) をテキストのプロパティ名として使用し、これは二重波括弧で囲まれていることから直接 HTML に挿入されます。