

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在與機器人的對話中分析使用者表達用語的情緒
<a name="sentiment"></a>

您可以使用情緒分析來判斷使用者表達用語中表達的情緒。使用情緒資訊，您可以管理對話流程或執行通話後分析。例如，如果使用者情緒為負面，您可以建立流程，將對話轉交給人類客服人員。

Amazon Lex 與 Amazon Comprehend 整合，以偵測使用者情緒。Amazon Comprehend 的回應指出文字的整體情緒是正面、中性、負面還是混合。回應包含使用者表達用語最有可能的情緒，以及每個情緒類別的分數。分數代表正確偵測到的情緒的可能性。

 您可以使用 主控台或使用 Amazon Lex API 為機器人啟用情緒分析。您可以在機器人的別名上啟用情緒分析。在 Amazon Lex 主控台上：

1. 選擇別名。

1. 在**詳細資訊**中，選擇**編輯**。

1. 選擇**啟用情緒分析**以開啟或關閉情緒分析。

1. 選擇 **Confirm (確認)** 以儲存變更。

如果您使用的是 API，請呼叫 [CreateBotAlias](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_CreateBotAlias.html) 操作，並將 `detectSentiment` 欄位設定為 `true`。

啟用情緒分析時，[RecognizeText](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_runtime_RecognizeText.html) 和 [RecognizeUtterance](https://docs.aws.amazon.com/lexv2/latest/APIReference/API_runtime_RecognizeUtterance.html) 操作的回應會傳回`interpretations`結構`sentimentResponse`中呼叫的欄位和其他中繼資料。`sentimentResponse` 欄位有兩個欄位，`sentiment` 和 `sentimentScore`，其中包含情緒分析的結果。如果您使用的是 Lambda 函數， `sentimentResponse` 欄位會包含在傳送至函數的事件資料中。

以下是 `sentimentResponse` 欄位傳回 `RecognizeText` 或 `RecognizeUtterance` 回應一部分的範例。

```
sentimentResponse {
    "sentimentScore": {
        "mixed": 0.030585512690246105,
        "positive": 0.94992071056365967,
        "neutral": 0.0141543131828308,
        "negative": 0.00893945890665054
    },
    "sentiment": "POSITIVE"
}
```

Amazon Lex 代表您呼叫 Amazon Comprehend，以判斷機器人處理的每個表達用語中的情緒。透過啟用情緒分析，即表示您同意 Amazon Comprehend 的服務條款和協議。如需 Amazon Comprehend 定價的詳細資訊，請參閱 [Amazon Comprehend 定價](https://aws.amazon.com/comprehend/pricing/)。

如需 Amazon Comprehend 情緒分析如何運作的詳細資訊，請參閱《*Amazon Comprehend 開發人員指南*》中的[判斷情緒](https://docs.aws.amazon.com/comprehend/latest/dg/how-sentiment.html)。