教程:使用角色提取与上下文相关的模式Tutorial: Extract contextually related patterns using roles

在本教程中,使用模式从格式正确的模板话语中提取数据。In this tutorial, use a pattern to extract data from a well-formatted template utterance. 模板话语使用简单的实体和角色提取相关的数据,例如源位置和目标位置。The template utterance uses a simple entity and roles to extract related data such as origin location and destination location. 当使用模式时,意向需要较少的示例话语。When using patterns, fewer example utterances are needed for the intent.

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

  • 导入示例应用Import example app
  • 创建新实体Create new entities
  • 创建新意向Create new intent
  • 定型Train
  • 发布Publish
  • 从终结点获取意向和实体Get intents and entities from endpoint
  • 使用角色创建模式Create pattern with roles
  • 创建城市的短语列表Create phrase list of Cities
  • 从终结点获取意向和实体Get intents and entities from endpoint

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

在模式中使用角色Using roles in patterns

角色的用途是在言语中提取与上下文相关的实体。The purpose of roles is to extract contextually related entities in an utterance. Move new employee Robert Williams from Sacramento and San Francisco 话语中,原城市和目的地城市值彼此相关,并使用公共语言来表示每个位置。In the utterance, Move new employee Robert Williams from Sacramento and San Francisco, the origin city, and destination city values are related to each other and use common language to denote each location.

新员工 Billy Patterson 的名称还不是“员工”列表实体的一部分。The name of the new employee, Billy Patterson, is not part of the list entity Employee yet. 首先需提取新员工名称,才能将名称发送到外部系统以创建公司凭据。The new employee name is extracted first, in order to send the name to an external system to create the company credentials. 创建公司凭据后,会将员工凭据添加到“员工”列表实体。After the company credentials are created, the employee credentials are added to the list entity Employee.

需要将新员工和家人从现有城市调到虚构公司所在的城市。The new employee and family need to be moved from the current city to a city where the fictitious company is located. 由于新员工可以来自于任何城市,因此需要发现此位置。Because a new employee can come from any city, the locations need to be discovered. 诸如列表实体等的集合列表可能不起作用,因为只会提取列表中的城市。A set list such as a list entity would not work because only the cities in the list would be extracted.

与原城市和目的地城市相关联的角色名称需要在所有实体中是唯一的。The role names associated with the origin and destination cities need to be unique across all entities. 确保这些角色唯一的一种简单办法是通过命名策略将其绑定到包含实体。An easy way to make sure the roles are unique is to tie them to the containing entity through a naming strategy. NewEmployeeRelocation 实体是具有以下两个角色的简单实体:NewEmployeeReloOrigin 和 NewEmployeeReloDestination。The NewEmployeeRelocation entity is a simple entity with two roles: NewEmployeeReloOrigin and NewEmployeeReloDestination. Relo 是 relocation 的缩写。Relo is short for relocation.

由于示例话语 Move new employee Robert Williams from Sacramento and San Francisco 仅具有机器学习的实体,因此,向意向提供足够的示例话语至关重要,以便检测到这些实体。Because the example utterance Move new employee Robert Williams from Sacramento and San Francisco has only machine-learned entities, it is important to provide enough example utterances to the intent so the entities are detected.

尽管模式允许提供较少的示例话语,但如果未检测到实体,则该模式将不匹配。While patterns allow you to provide fewer example utterances, if the entities are not detected, the pattern does not match.

如果由于简单实体是城市等名称而难以检测到该实体,请考虑添加类似值的短语列表。If you have difficulty with simple entity detection because it is a name such as a city, consider adding a phrase list of similar values. 这通过为 LUIS 提供有关该类型的字或短语的其他信号,帮助检测城市名称。This helps the detection of the city name by giving LUIS an additional signal about that type of word or phrase. 短语列表只能通过帮助模式匹配所必需的实体检测来为模式提供帮助。Phrase lists only help the pattern by helping with entity detection, which is necessary for the pattern to match.

导入示例应用Import example app

继续使用上一个教程中创建的名为 HumanResources 的应用。Continue with the app created in the last tutorial, named HumanResources.

使用以下步骤:Use the following steps:

  1. 下载并保存应用 JSON 文件Download and save app JSON file.

  2. 将 JSON 导入到新应用中。Import the JSON into a new app.

  3. 在“管理”部分的“版本”选项卡上,克隆版本并将其命名为 rolesFrom the Manage section, on the Versions tab, clone the version, and name it roles. 克隆非常适合用于演练各种 LUIS 功能,且不会影响原始版本。Cloning is a great way to play with various LUIS features without affecting the original version. 由于版本名称用作 URL 路由的一部分,因此该名称不能包含任何在 URL 中无效的字符。Because the version name is used as part of the URL route, the name can't contain any characters that are not valid in a URL.

创建新实体Create new entities

  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 Entities from the left navigation.

  3. 选择“创建新实体”。Select Create new entity.

  4. 在弹出窗口中,输入 NewEmployee 作为简单实体。In the pop-up window, enter NewEmployee as a Simple entity.

  5. 选择“创建新实体”。Select Create new entity.

  6. 在弹出窗口中,输入 NewEmployeeRelocation 作为简单实体。In the pop-up window, enter NewEmployeeRelocation as a Simple entity.

  7. 从实体列表中选择“NewEmployeeRelocation”。Select NewEmployeeRelocation from the list of entities.

  8. 输入第一个角色作为 NewEmployeeReloOrigin 并选择 Enter。Enter the first role as NewEmployeeReloOrigin and select enter.

  9. 输入第二个角色作为 NewEmployeeReloDestination 并选择 Enter。Enter the second role as NewEmployeeReloDestination and select enter.

创建新意向Create new intent

如果在开始前删除了预生成的 keyPhrase 实体,接着在执行完此部分中的步骤后又将其添加了回来,则在这些步骤中为实体添加标签可能会更加轻松。Labeling the entities in these steps may be easier if the prebuilt keyPhrase entity is removed before beginning then added back after you are done with the steps in this section.

  1. 从左侧导航栏中选择“意向”。Select Intents from the left navigation.

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

  3. 在弹出对话框中,输入 NewEmployeeRelocationProcess 作为意向名称。Enter NewEmployeeRelocationProcess as the intent name in the pop-up dialog box.

  4. 输入以下示例话语,为新实体添加标签。Enter the following example utterances, labeling the new entities. 实体和角色值均以粗体显示。The entity and role values are in bold. 如果发现为文本添加标签很容易,请记得切换到“令牌视图”。Remember to switch to the Tokens View if you find it easier to label the text.

    在意向中添加标签时,不要指定实体的角色。You don't specify the role of the entity when labeling in the intent. 在稍后创建模式时才执行此操作。You do that later when creating the pattern.

    话语Utterance NewEmployeeNewEmployee NewEmployeeRelocationNewEmployeeRelocation
    Bob JonesSeattle 调到 Los ColinasMove Bob Jones from Seattle to Los Colinas Bob JonesBob Jones Seattle、Los ColinasSeattle, Los Colinas
    Dave C. CooperRedmond 调到 New York CityMove Dave C. Cooper from Redmond to New York City Dave C. CooperDave C. Cooper Redmond、New York CityRedmond, New York City
    Jim Paul SmithToronto 调到 West VancouverMove Jim Paul Smith from Toronto to West Vancouver Jim Paul SmithJim Paul Smith Toronto、West VancouverToronto, West Vancouver
    J. BensonBoston 调到 Staines-upon-ThamesMove J. Benson from Boston to Staines-upon-Thames J.J. BensonBenson Boston、Staines-upon-ThamesBoston, Staines-upon-Thames
    Travis "Trav" HintonCastelo Branco 调到 OrlandoMove Travis "Trav" Hinton from Castelo Branco to Orlando Travis "Trav" HintonTravis "Trav" Hinton Castelo Branco、OrlandoCastelo Branco, Orlando
    Trevor Nottington IIIAranda de Duero 调到 BoiseMove Trevor Nottington III from Aranda de Duero to Boise Trevor Nottington IIITrevor Nottington III Aranda de Duero、BoiseAranda de Duero, Boise
    Dr.Greg WilliamsOrlando 调到 Ellicott CityMove Dr. Greg Williams from Orlando to Ellicott City Dr.Greg WilliamsDr. Greg Williams Orlando、Ellicott CityOrlando, Ellicott City
    Robert "Bobby" GregsonKansas City 调到 San Juan CapistranoMove Robert "Bobby" Gregson from Kansas City to San Juan Capistrano Robert "Bobby" GregsonRobert "Bobby" Gregson Kansas City、San Juan CapistranoKansas City, San Juan Capistrano
    Patti OwensBellevue 调到 RockfordMove Patti Owens from Bellevue to Rockford Patti OwensPatti Owens Bellevue、RockfordBellevue, Rockford
    Janet BartletTuscan 调到 Santa FeMove Janet Bartlet from Tuscan to Santa Fe Janet BartletJanet Bartlet Tuscan、Santa FeTuscan, Santa Fe

    员工名称具有各种前缀、字数统计、语法和后缀。The employee name has a variety of prefix, word count, syntax, and suffix. 这对于 LUIS 理解新员工名称的变体至关重要。This is important for LUIS to understand the variations of a new employee name. 城市名称还具有各种字数统计和语法。The city names also have a variety of word count and syntax. 此多样性对于指导 LUIS 这些实体可能在用户的话语中出现的方式至关重要。This variety is important to teach LUIS how these entities may appear in a user's utterance.

    如果任一实体具有相同的字数统计并且没有任何其他变体,则指导 LUIS 此实体仅具有该字数统计并且没有任何其他变体。If either entity had been of the same word count and no other variations, you would teach LUIS that this entity only has that word count and no other variations. LUIS 可能无法准确预测更广泛的变体集合,因为并未向它显示任何变化。LUIS would not be able to correctly predict a broader set of variations because it was not shown any.

    如果删除了 keyPhrase 实体,请立即将其添加回应用。If you removed the keyPhrase entity, add it back to the app now.

定型Train

  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.

    已训练状态栏

发布Publish

若要在聊天机器人或其他客户端应用程序中接收 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 intent and entities from 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 的末尾,并输入 Move Wayne Berry from Miami to Mount VernonGo to the end of the URL in the address and enter Move Wayne Berry from Miami to Mount Vernon. 最后一个查询字符串参数为 q,表示陈述查询The last querystring parameter is q, the utterance query.

    {
      "query": "Move Wayne Berry from Newark to Columbus",
      "topScoringIntent": {
        "intent": "NewEmployeeRelocationProcess",
        "score": 0.514479756
      },
      "intents": [
        {
          "intent": "NewEmployeeRelocationProcess",
          "score": 0.514479756
        },
        {
          "intent": "Utilities.Confirm",
          "score": 0.017118983
        },
        {
          "intent": "MoveEmployee",
          "score": 0.009982505
        },
        {
          "intent": "GetJobInformation",
          "score": 0.008637771
        },
        {
          "intent": "ApplyForJob",
          "score": 0.007115978
        },
        {
          "intent": "Utilities.StartOver",
          "score": 0.006120186
        },
        {
          "intent": "Utilities.Cancel",
          "score": 0.00452428637
        },
        {
          "intent": "None",
          "score": 0.00400899537
        },
        {
          "intent": "OrgChart-Reports",
          "score": 0.00240071164
        },
        {
          "intent": "Utilities.Help",
          "score": 0.001770991
        },
        {
          "intent": "EmployeeFeedback",
          "score": 0.001697356
        },
        {
          "intent": "OrgChart-Manager",
          "score": 0.00168116146
        },
        {
          "intent": "Utilities.Stop",
          "score": 0.00163952739
        },
        {
          "intent": "FindForm",
          "score": 0.00112958835
        }
      ],
      "entities": [
        {
          "entity": "wayne berry",
          "type": "NewEmployee",
          "startIndex": 5,
          "endIndex": 15,
          "score": 0.629158735
        },
        {
          "entity": "newark",
          "type": "NewEmployeeRelocation",
          "startIndex": 22,
          "endIndex": 27,
          "score": 0.638941
        }
      ]
    }  
    

意向预测得分大约只有 50%。The intent prediction score is only about 50%. 如果客户端应用程序需要较大的数字,则需要修改此项目。If your client application requires a higher number, this needs to be fixed. 此外,也未预测到任何实体。The entities were not predicted either.

提取了其中的一个位置,但未提取另一个位置。One of the locations was extracted but the other location was not.

模式有助于预测得分,但是,必须在模式匹配话语之前正确预测实体。Patterns will help the prediction score, however, the entities must be correctly predicted before the pattern matches the utterance.

包含角色的模式Pattern with roles

  1. 选择顶部导航栏中的“生成”。Select Build in the top navigation.

  2. 选择左侧导航栏中的“模式”。Select Patterns in the left navigation.

  3. 从“选择意向”下拉列表中选择“NewEmployeeRelocationProcess”。Select NewEmployeeRelocationProcess from the Select an intent drop-down list.

  4. 输入以下模式:move {NewEmployee} from {NewEmployeeRelocation:NewEmployeeReloOrigin} to {NewEmployeeRelocation:NewEmployeeReloDestination}[.]Enter the following pattern: move {NewEmployee} from {NewEmployeeRelocation:NewEmployeeReloOrigin} to {NewEmployeeRelocation:NewEmployeeReloDestination}[.]

    如果定型、发布和查询终结点,则可能会因看到找不到实体而感到失望,因此,该模式不会进行匹配,因而预测也得不到改进。If you train, publish, and query the endpoint, you may be disappointed to see that the entities are not found, so the pattern didn't match, therefore the prediction didn't improve. 这是没有足够的含带标签的实体的示例话语的结果。This is a consequence of not enough example utterances with labeled entities. 要修复此问题,请添加短语列表,而不是添加更多示例。Instead of adding more examples, add a phrase list to fix this problem.

城市短语列表Cities phrase list

城市就像人名一样很棘手,因为它们可以是字和标点符号的任意组合。Cities, like people's names are tricky in that they can be any mix of words and punctuation. 区域和全球的城市都是已知的,因此,LUIS 需要城市的短语列表才能开始学习。The cities of the region and world are known, so LUIS needs a phrase list of cities to begin learning.

  1. 从左侧菜单的“提高应用性能”部分中,选择“短语列表”。Select Phrase list from the Improve app performance section of the left menu.

  2. 命名 Cities 列表,并为该列表添加以下 valuesName the list Cities and add the following values for the list:

    短语列表的值Values of phrase list
    西雅图Seattle
    San DiegoSan Diego
    New York CityNew York City
    洛杉矶Los Angeles
    PortlandPortland
    PhiladephiaPhiladephia
    迈阿密Miami
    达拉斯Dallas

    请勿添加世界上的每个城市,也不要添加区域中的每个城市。Do not add every city in the world or even every city in the region. LUIS 需要能够从列表中推断出城市的概况。LUIS needs to be able to generalize what a city is from the list. 确保“这些值可互换”处于选中状态。Make sure to keep These values are interchangeable selected. 此设置意味着列表上的字被视为同义词。This setting means the words on the list on treated as synonyms.

  3. 定型并发布应用。Train and publish the app.

从终结点获取意向和实体Get intent and entities from endpoint

  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. 将光标定位到地址中 URL 的末尾,并输入 Move wayne berry from miami to mount vernonGo to the end of the URL in the address and enter Move wayne berry from miami to mount vernon. 最后一个查询字符串参数为 q,表示陈述查询The last querystring parameter is q, the utterance query.

    {
      "query": "Move Wayne Berry from Miami to Mount Vernon",
      "topScoringIntent": {
        "intent": "NewEmployeeRelocationProcess",
        "score": 0.9999999
      },
      "intents": [
        {
          "intent": "NewEmployeeRelocationProcess",
          "score": 0.9999999
        },
        {
          "intent": "Utilities.Confirm",
          "score": 1.49678385E-06
        },
        {
          "intent": "MoveEmployee",
          "score": 8.240291E-07
        },
        {
          "intent": "GetJobInformation",
          "score": 6.3131273E-07
        },
        {
          "intent": "None",
          "score": 4.25E-09
        },
        {
          "intent": "OrgChart-Manager",
          "score": 2.8E-09
        },
        {
          "intent": "OrgChart-Reports",
          "score": 2.8E-09
        },
        {
          "intent": "EmployeeFeedback",
          "score": 1.64E-09
        },
        {
          "intent": "Utilities.StartOver",
          "score": 1.64E-09
        },
        {
          "intent": "Utilities.Help",
          "score": 1.48181822E-09
        },
        {
          "intent": "Utilities.Stop",
          "score": 1.48181822E-09
        },
        {
          "intent": "Utilities.Cancel",
          "score": 1.35E-09
        },
        {
          "intent": "FindForm",
          "score": 1.23846156E-09
        },
        {
          "intent": "ApplyForJob",
          "score": 5.692308E-10
        }
      ],
      "entities": [
        {
          "entity": "wayne berry",
          "type": "builtin.keyPhrase",
          "startIndex": 5,
          "endIndex": 15
        },
        {
          "entity": "miami",
          "type": "builtin.keyPhrase",
          "startIndex": 22,
          "endIndex": 26
        },
        {
          "entity": "wayne berry",
          "type": "NewEmployee",
          "startIndex": 5,
          "endIndex": 15,
          "score": 0.9410646,
          "role": ""
        },
        {
          "entity": "miami",
          "type": "NewEmployeeRelocation",
          "startIndex": 22,
          "endIndex": 26,
          "score": 0.9853915,
          "role": "NewEmployeeReloOrigin"
        },
        {
          "entity": "mount vernon",
          "type": "NewEmployeeRelocation",
          "startIndex": 31,
          "endIndex": 42,
          "score": 0.986044347,
          "role": "NewEmployeeReloDestination"
        }
      ],
      "sentimentAnalysis": {
        "label": "neutral",
        "score": 0.5
      }
    }
    

意向得分现在会高出许多,并且角色名称为实体响应的一部分。The intent score is now much higher and the role names are part of the entity response.

清理资源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 added an entity with roles and an intent with example utterances. 使用该实体的第一个终结点预测正确预测了意向,但可信度分数较低。The first endpoint prediction using the entity correctly predicted the intent but with a low confidence score. 仅检测到了两个实体中的一个。Only one of the two entities was detected. 接下来,本教程添加了一个模式,该模式使用了实体角色和短语列表来改进话语中的城市名称的值。Next, the tutorial added a pattern that used the entity roles, and a phrase list to boost the value of the city names in the utterances. 第二个终结点预测返回了较高的可信度分数,并且找到了全部两个实体角色。The second endpoint prediction returned a high-confidence score and found both entity roles.