教程:获取话语的情绪Tutorial: Get sentiment of utterance

在本教程中,我们将创建一个应用,用于演示如何从话语中确定积极、消极和中性的情绪。In this tutorial, create an app that demonstrates how to determine positive, negative, and neutral sentiment from utterances. 情绪是从整个话语确定的。Sentiment is determined from the entire utterance.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建新应用Create a new app
  • 将情绪分析添加为发布设置Add sentiment analysis as publish setting
  • 训练应用Train app
  • 发布应用Publish app
  • 从终结点获取获取话语的情绪Get sentiment of utterance from endpoint

在本文中,可以使用免费 LUIS 帐户来创作 LUIS 应用程序。For this article, you can use the free LUIS account in order to author your LUIS application.

情绪分析是一项发布设置Sentiment analysis is a publish setting

以下陈述显示了情绪的示例:The following utterances show examples of sentiment:

情绪Sentiment 得分Score 陈述Utterance
积极positive 0.910.91 John W. Smith 在巴黎的演讲做得很好。John W. Smith did a great job on the presentation in Paris.
积极positive 0.840.84 西雅图工程师对 Parker 的推销工作做得很好。The Seattle engineers did fabulous work on the Parker sales pitch.

情绪分析是一种适用于所有话语的发布设置。Sentiment analysis is a publish setting that applies to every utterance. 不需要在陈述中查找指示情绪的单词并对其进行标记。You do not have to find the words indicating sentiment in the utterance and mark them.

因为它是发布设置,所以不会在意向或实体页上看到它。Because it is a publish setting, you do not see it on the intents or entities pages. 可以在交互式测试窗格中或在终结点 URL 处进行测试时看到它。You can see it in the interactive test pane or when testing at the endpoint URL.

创建新应用Create a new app

  1. 使用 URL https://luis.azure.cn 登录到 LUIS 门户。Sign in to the LUIS portal with the URL of https://luis.azure.cn.

  2. 选择“创建新应用”。Select Create new app.

    语言理解 (LUIS) 我的应用页的屏幕截图Screenshot of Language Understanding (LUIS) My Apps page

  3. 在弹出的对话框中输入名称 HumanResources,保留默认的区域性“英语”。In the pop-up dialog, enter the name HumanResources and keep the default culture, English. 将说明保留为空。Leave the description empty.

    创建 LUIS 新 HumanResources 应用

  4. 选择“完成” 。Select Done. 接下来,应用会显示具有 None 意向的“意向”页。Next, the app shows the Intents page with the None Intent.

添加 PersonName 预生成实体Add PersonName prebuilt entity

  1. 在左侧导航菜单中选择“实体”。Select Entities from the left navigation menu.

  2. 选择“添加预生成的实体”按钮。Select Add prebuilt entity button.

  3. 从预生成的实体列表中选择以下实体,然后选择“完成”:Select the following entity from the list of prebuilt entities then select Done:

创建用于确定员工反馈的意向Create an intent to determine employee feedback

添加一个新意向来从公司的成员捕获员工反馈。Add a new intent to capture employee feedback from members of the company.

  1. LUIS 的“生成”部分包含你的人力资源应用。Make sure your Human Resources app is in the Build section of LUIS. 在右上方的菜单栏中选择“生成”可切换到此部分。You can change to this section by selecting Build on the top, right menu bar.

  2. 选择“创建新意向”。Select Create new intent.

  3. 将新意向命名为 EmployeeFeedbackName the new intent name EmployeeFeedback.

    以 EmployeeFeedback 为名称创建新意向的对话框

  4. 添加几项陈述来指明员工某项工作做得很好或者某个方面需要改进:Add several utterances that indicate an employee doing something well or an area that needs improvement:

    陈述Utterances
    John Smith 做了很好的工作来欢迎一位同事从产假归来。John Smith did a nice job of welcoming back a co-worker from maternity leave
    Jill Jones 在一位同事悲伤时做了很好的安慰工作。Jill Jones did a great job of comforting a co-worker in her time of grief.
    Bob Barnes 没有文书工作所需的所有发票。Bob Barnes didn't have all the required invoices for the paperwork.
    Todd Thomas 在一个月后上交了没有签名的必需表单Todd Thomas turned in the required forms a month late with no signatures
    Katherine Kelly 没有赶上重要的市场营销场外会议。Katherine Kelly didn't make it to the important marketing off-site meeting.
    Denise Dillard 错过了六月份的评审会议。Denise Dillard missed the meeting for June reviews.
    Mark Mathews 在哈佛进行了令人震撼的推销Mark Mathews rocked the sales pitch at Harvard
    Walter Williams 在斯坦福的演讲做得很好Walter Williams did a great job on the presentation at Stanford

    选择“查看选项”,然后选择“显示实体值”来查看名称。Select the View options, select Show entity values to see the names.

    在 EmployeeFeedback 意向中包含了示例陈述的 LUIS 应用的屏幕截图Screenshot of LUIS app with example utterances in EmployeeFeedback intent

将话语示例添加到 None 意向Add example utterances to the None intent

客户端应用程序需要知道某个话语是否对应用程序没有意义或不合适。The client application needs to know if an utterance is not meaningful or appropriate for the application. 在创建过程中,“None”意向将添加到每个应用程序,以确定客户端应用程序是否无法回答某个话语。The None intent is added to each application as part of the creation process to determine if an utterance can't be answered by the client application.

如果 LUIS 对于某个话语返回“None”意向,客户端应用程序可以询问用户是否要结束聊天或提供更多指示以继续聊天。If LUIS returns the None intent for an utterance, your client application can ask if the user wants to end the conversation or give more directions for continuing the conversation.

Caution

不要将“None”意向留空。Do not leave the None intent empty.

  1. 在左侧面板中选择“意向”。Select Intents from the left panel.

  2. 选择“None”意向。Select the None intent. 添加用户可能会输入但与人力资源应用无关的 3 个话语:Add three utterances that your user might enter but are not relevant to your Human Resources app:

    示例陈述Example utterances
    Barking dogs are annoyingBarking dogs are annoying
    Order a pizza for meOrder a pizza for me
    Penguins in the oceanPenguins in the ocean

训练应用,以便可以测试对意向所做的更改Train the app so the changes to the intent can be tested

  1. 在 LUIS 网站的右上方,选择“训练”按钮。In the top right side of the LUIS website, select the Train button.

    “训练”按钮

  2. 当网站顶部出现确认成功的绿色状态栏时,表示训练已完成。Training is complete when you see the green status bar at the top of the website confirming success.

    已训练状态栏

将应用配置为包含情绪分析Configure app to include sentiment analysis

  1. 在右上角的导航栏中选择“管理”,然后从左侧菜单中选择“发布设置”。Select Manage in the top right navigation, then select Publish settings from the left menu.

  2. 选择“情绪分析”以启用此设置。Select Sentiment Analysis to enable this setting.

    开启“情绪分析”作为发布设置

发布应用,以便可以从终结点查询已训练的模型Publish the app so the trained model is queryable from the endpoint

若要在聊天机器人或其他客户端应用程序中接收 LUIS 预测,需要将应用发布到终结点。In order to receive a LUIS prediction in a chat bot or other client application, you need to publish the app to the endpoint.

  1. 在右上方的导航栏中选择“发布”。Select Publish in the top right navigation.

    右上方菜单中的“LUIS 发布到终结点”按钮

  2. 选择“生产”槽和“发布”按钮。Select the Production slot and the Publish button.

    LUIS 发布到终结点

  3. 当网站顶部出现确认成功的绿色状态栏时,表示发布已完成。Publishing is complete when you see the green status bar at the top of the website confirming success.

    LUIS 发布到终结点

  4. 选择绿色状态栏中的“终结点”链接,转到“密钥和终结点”页。Select the endpoints link in the green status bar to go to the Keys and endpoints page. 终结点 URL 列在底部。The endpoint URLs are listed at the bottom.

从终结点获取话语的情绪Get the sentiment of an utterance from the endpoint

  1. 在“管理”部分(右上方菜单)的“密钥和终结点”页(左侧菜单)中,选择页面底部的终结点 URL。In the Manage section (top right menu), on the Keys and endpoints page (left menu), select the endpoint URL at the bottom of the page. 此操作会打开另一个浏览器标签页,其地址栏中包含终结点 URL。This action opens another browser tab with the endpoint URL in the address bar.

    终结点 URL 如 https://<region>.api.cognitive.azure.cn/luis/v2.0/apps/<appID>?verbose=true&subscription-key=<YOUR_KEY>&<optional-name-value-pairs>&q=<user-utterance-text> 所示。The endpoint URL looks like https://<region>.api.cognitive.azure.cn/luis/v2.0/apps/<appID>?verbose=true&subscription-key=<YOUR_KEY>&<optional-name-value-pairs>&q=<user-utterance-text>.

  2. 将光标定位到地址中 URL 的末尾,并输入 Jill Jones work with the media team on the public portal was amazingGo to the end of the URL in the address and enter Jill Jones work with the media team on the public portal was amazing. 最后一个查询字符串参数为 q,表示陈述查询 (query)。The last querystring parameter is q, the utterance query. 此陈述不同于标记的任何陈述,因此,它非常适合用于测试,测试结果应返回包含所提取的情绪分析的 EmployeeFeedback 意向。This utterance is not the same as any of the labeled utterances so it is a good test and should return the EmployeeFeedback intent with the sentiment analysis extracted.

    {
      "query": "Jill Jones work with the media team on the public portal was amazing",
      "topScoringIntent": {
        "intent": "EmployeeFeedback",
        "score": 0.9616192
      },
      "intents": [
        {
          "intent": "EmployeeFeedback",
          "score": 0.9616192
        },
        {
          "intent": "None",
          "score": 0.09347677
        }
      ],
      "entities": [
        {
          "entity": "jill jones",
          "type": "builtin.personName",
          "startIndex": 0,
          "endIndex": 9
        }
      ],
      "sentimentAnalysis": {
        "label": "positive",
        "score": 0.8694164
      }
    }
    

    sentimentAnalysis 是积极的,评分为 86%。The sentimentAnalysis is positive with a score of 86%.

清理资源Clean up resources

不再需要 LUIS 应用时,请将其删除。When no longer needed, delete the LUIS app. 为此,请在左上角的菜单中选择“我的应用”。To do so, select My apps from the top left menu. 在应用列表中选择应用名称右侧的省略号 (...),然后选择“删除”。Select the ellipsis (...) to the right of the app name in the app list, select Delete. 在弹出的“删除应用?”对话框中,选择“确定”。On the pop-up dialog Delete app?, select Ok.

后续步骤Next steps

本教程将情绪分析添加为发布设置,以从整个话语中提取情绪值。This tutorial adds sentiment analysis as a publish setting to extract sentiment values from the utterance as a whole.