RASA-实体提取组件Extractor

实体提取器主要是从用户消息中提取实体,例如人名或位置。如果开发者想使用多个实体提取器,我们建议每个提取器都针对一组独立的实体类型。例如,使用Duckling提取日期和时间, 使用DIETClassifier提取人名。否则,如果多个提取器针对相同的实体类型,很可能会多次提取实体。例如,如果开发者使用两个或多个通用提取器,如MitieEntityExtractor、 DIETClassifier或CRFEntityExtractor,则训练数据中的实体类型将由它们全部找到并提取。如果填充的槽类型都是text类型 ,那么管道中的只有最后一个提取器是起作用的。如果插槽的类型为list,则所有结果都将添加到列表中,包括重复项。

另一个不太明显的重复/重叠提取案例可能会发生,即使提取器专注于不同的实体类型。想象一个送餐机器人和一条用户消息,例如I would like to order the Monday special。假设地,如果你的时间提取器的性能不是很好,它可能会提取Monday这里作为订单的时间,而你的其他提取器可能会Monday special作为膳食提取。如果您难以处理此类重叠实体,添加额外的训练数据以改进您的提取器可能是有意义的。如果这还不够,您可以添加一个 自定义组件,根据您自己的逻辑解决实体提取中的冲突。


  1. MitieEntityExtractor

MitieEntityExtractor 主要是用MITIE 实体提取器提取信息中的实体。需要管道前配置MitieNLP。底层提取器主要是用稀疏线性核和自定义特征的多类线性 SVM。MITIE 不提供实体的置信度值。

{
    "entities": [{
        "value": "New York City",
        "start": 20,
        "end": 33,
        "confidence": null,
        "entity": "city",
        "extractor": "MitieEntityExtractor"
    }]
}

注意:MitieEntityExtractor不依赖于任何特征生成器,它可以自己生成特征。

配置:

pipeline:
    - name: "MitieEntityExtractor"

  1. SpacyEntityExtractor

spacyentityextractor 使用 spaCy 这个组件来提取消息中的实体。spaCy 主要是使用统计 BILOU 转换模型。到目前为止,该组件只能使用 spaCy 内置的实体提取模型,无法重新训练。此外,提取器不提供任何置信度分数。请注意,某些 spaCy 模型高度区分大小写。并且提取器SpacyEntityExtractor不提供confidence级别并将始终返回null。

{
    "entities": [{
        "value": "New York City",
        "start": 20,
        "end": 33,
        "confidence": null,
        "entity": "city",
        "extractor": "SpacyEntityExtractor"
    }]
}

注意:需要配置 spaCy 组件提取的实体类型列表。可以在spaCy 文档中找到可用维度的完整列表。如果不指定维度选项,默认将提取所有可用实体类型。

配置:

pipeline:
- name: "SpacyEntityExtractor"
  # dimensions to extract
  dimensions: ["PERSON", "LOC", "ORG", "PRODUCT"]

  1. CRFEntityExtractor

该提取器利用条件随机 (CRF) 来进行命名实体识别。CRF 可以被认为是一个无向马尔可夫链,其中时间步长是单词,状态是实体类。单词的特征(大写、词性标记等)给出了某些实体类的概率,相邻实体标签之间的转换也是如此:然后计算并返回最有可能的标签集。

{
    "entities": [{
        "value": "New York City",
        "start": 20,
        "end": 33,
        "entity": "city",
        "confidence": 0.874,
        "extractor": "CRFEntityExtractor"
    }]
}

    如果开发者想将自定义特征(例如预训练的词嵌入)传递给 CRFEntityExtractor,可以在 CRFEntityExtractor之前将任何稠密特征器添加到管道中,然后 CRFEntityExtractor 通过配置‘text_dense_feature’参数来使用密集特征。

如果开发者想将自定义特征(例如预训练的词嵌入)传递给 CRFEntityExtractor,可以在 CRFEntityExtractor之前将任何稠密特征器添加到管道中,然后 CRFEntityExtractor 通过配置‘text_dense_feature’参数来使用密集特征。

    CRFEntityExtractor 可以自动找到额外的密集特征并检查密集特征是否是len(tokens) 的可迭代,并且每条数据都是一个向量。如果检查失败,将提示警告。但是,CRFEntityExtractor 将在没有附加自定义功能的情况下继续训练。如果存在密集特征,CRFEntityExtractor 会将密集特征传递给 sklearn_crfsuite 并使用它们进行训练。

    CRFEntityExtractor 有默认功能列表。默认的功能列表如下:

===================  ==========================================================================================
Feature Name         Description
===================  ==========================================================================================
low                  word identity - use the lower-cased token as a feature.
upper                Checks if the token is upper case.
title                Checks if the token starts with an uppercase character and all remaining characters are
                     lowercased.
digit                Checks if the token contains just digits.
prefix5              Take the first five characters of the token.
prefix2              Take the first two characters of the token.
suffix5              Take the last five characters of the token.
suffix3              Take the last three characters of the token.
suffix2              Take the last two characters of the token.
suffix1              Take the last character of the token.
pos                  Take the Part-of-Speech tag of the token (``SpacyTokenizer`` required).
pos2                 Take the first two characters of the Part-of-Speech tag of the token
                     (``SpacyTokenizer`` required).
pattern              Take the patterns defined by ``RegexFeaturizer``.
bias                 Add an additional "bias" feature to the list of features.
text_dense_features  Adds additional features from a dense featurizer.
===================  ==========================================================================================

当特征化器使用滑动窗口在用户消息中的标记上移动时,您可以为滑动窗口中的先前标记、当前标记和下一个标记定义特征。您将特征定义为[before, token, after]数组。另外,您可以设置一个标志来确定是否使用 BILOU 标记模式。BILOU_flag确定是否使用 BILOU 标记。默认True。

配置:

pipeline:
- name: "CRFEntityExtractor"
  # BILOU_flag determines whether to use BILOU tagging or not.
  "BILOU_flag": True
  # features to extract in the sliding window
  "features": [
    ["low", "title", "upper"],
    [
      "bias",
      "low",
      "prefix5",
      "prefix2",
      "suffix5",
      "suffix3",
      "suffix2",
      "upper",
      "title",
      "digit",
      "pattern",
      "text_dense_features"
    ],
    ["low", "title", "upper"],
  ]
  # The maximum number of iterations for optimization algorithms.
  "max_iterations": 50
  # weight of the L1 regularization
  "L1_c": 0.1
  # weight of the L2 regularization
  "L2_c": 0.1
  # Name of dense featurizers to use.
  # If list is empty all available dense features are used.
  "featurizers": []
  # Indicated whether a list of extracted entities should be split into individual entities for a given entity type
  "split_entities_by_comma":
      address: False
      email: True

注意:

  1. 如果使用 POS 功能(pos或pos2),开发者就需要在pipline中配置SpacyTokenizer 。

  1. 如果使用功能pattern,开发者则需要在pipline中配置 RegexFeaturizer 。

  1. 如果使用特征text_dense_features,开发者需要在pipline中pipline中配置一个稠密的特征化器(例如 LanguageModelFeaturizer)。


  1. DucklingEntityExtractor

    Duckling 工具可让开发者以多种语言来提取常见实体,例如日期、金额、距离等。

    如果要使用此工具,需要先运行一个Duckling 服务。最简单的使用是用docker来部署。例如 docker run -p 8000:8000 rasa/duckling,或者可以直接在您的机器上安装 duckling并启动服务器。

    Duckling 允许识别日期、数字、距离和其他结构化实体并对其进行规范化。请注意,duckling工具不提供排名的情况下尝试提取尽可能多的实体类型。例如I will be there in 10 minutes,如果您为Duckling同时指定number和time作为维度,则该组件将提取两个实体:10作为数字和 in 10 minutes作为文本中的时间。在这种情况下,您的应用程序必须决定哪种实体类型是正确的。提取器将始终返回 1.0 作为置信度,因为它是一个基于规则的系统。可以在 Duckling GitHub 存储库中找到支持的语言列表。

{
    "entities": [{
        "end": 53,
        "entity": "time",
        "start": 48,
        "value": "2017-04-10T00:00:00.000+02:00",
        "confidence": 1.0,
        "extractor": "DucklingEntityExtractor"
    }]
}

    提取器配置方式,主要是在pipline中进行配置,需要配置 Duckling 组件提取的实体类型列表。可以在小鸭项目自述 中找到可用维度的完整列表。如果不指定维度选项,默认将提取所有可用实体类型。

配置:

pipeline:
- name: "DucklingEntityExtractor"
  # url of the running duckling server
  url: "http://localhost:8000"
  # dimensions to extract
  dimensions: ["time", "number", "amount-of-money", "distance"]
  # allows you to configure the locale, by default the language is
  # used
  locale: "de_DE"
  # if not set the default timezone of Duckling is going to be used
  # needed to calculate dates from relative expressions like "tomorrow"
  timezone: "Europe/Berlin"
  # Timeout for receiving response from http url of the running duckling server
  # if not set the default timeout of duckling http url is set to 3 seconds.
  timeout : 3

  1. RegexEntityExtractor

RegexEntityExtractor 实体提取器主要是用训练数据中定义的查找表和正则表达式提取实体。该组件检查用户消息是否包含其中一个查找表的条目或匹配其中一个正则表达式。如果找到匹配项,则将该值提取为实体。

    该组件只会是用那些出现在训练数据中的正则表达式特征。所以需要确保每个实体类型至少有一条训练数据。

    当开发者将此提取器与MitieEntityExtractor、 CRFEntityExtractor或DIETClassifier结合使用时,它可能会导致多次提取实体。

    如果开发者想同时使用 RegexEntityExtractor 和上述其他的实体提取器,我们建议考虑以下两个选择:

1: 为每一种实体类型都配置有独立的实体提取器。为确保提取器之间不会相互干扰,只为每个正则表达式/查找实体类型标注一个训练样例,但不要更多。

2: 只是将 RegexEntityExtractor 作为其他实体提取器的补充,但没有单独的实体类型。需要满足三个要求:

1️⃣在pipline中配置 RegexFeaturizer

2️⃣在训练数据中标注所有实体示例

3️⃣从pipline中删除 RegexEntityExtractor。这样,提取器将收到有关正则表达式匹配到的额外信号,并且能够统计确定何时依赖这些匹配,何时不依赖。

配置方式主要是在pipline中进行配置,通过添加选项使实体提取器区分大小写case_sensitive: True,默认为 case_sensitive: False。

要正确处理中文等不使用空格分隔单词的语言,用户需要添加该use_word_boundaries: False选项,默认为use_word_boundaries: True。

配置:

pipeline:
- name: RegexEntityExtractor
  # text will be processed with case insensitive as default
  case_sensitive: False
  # use lookup tables to extract entities
  use_lookup_tables: True
  # use regexes to extract entities
  use_regexes: True
  # use match word boundaries for lookup table
  use_word_boundaries: True

  1. EntitySynonymMapper

如果训练数据包含定义的同义词,此组件将确保检测到的实体值映射到相同的值。例如,如果您的训练数据包含以下示例:

[
  {
    "text": "I moved to New York City",
    "intent": "inform_relocation",
    "entities": [{
      "value": "nyc",
      "start": 11,
      "end": 24,
      "entity": "city",
    }]
  },
  {
    "text": "I got a new flat in NYC.",
    "intent": "inform_relocation",
    "entities": [{
      "value": "nyc",
      "start": 20,
      "end": 23,
      "entity": "city",
    }]
  }
]

该组件将允许您将实体New York City和NYC映射到nyc. 即使消息包含NYC,实体提取也会返回nyc。当该组件更改现有实体时,它会将自己附加到该实体的处理器列表中。

注意:使用EntitySynonymMapper时,在前面应该先添加实体提取组件。

配置:

pipeline:
- name: "EntitySynonymMapper"

猜你喜欢

转载自blog.csdn.net/fzz97_/article/details/128959114