使用命名实体识别提取文本信息 - Amazon SageMaker

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用命名实体识别提取文本信息

要从非结构化文本中提取信息并将其归类为预定义类别,请使用 Amazon G SageMaker round Truth 命名实体识别 (NER) 标签任务。传统上,NER需要筛选文本数据以找到名词短语(称为命名实体),并使用诸如 “人”、“组织” 或 “品牌” 之类的标签对每个短语进行分类。您可以扩展此任务以标注更长的文本跨度,并使用您指定的预定义标签对这些序列进行分类。您可以使用亚马逊 SageMaker 控制台的 Ground Truth 部分或CreateLabelingJob操作创建命名实体识别标签作业。

当负责命名实体识别标注作业时,工作人员会将标签应用于较大文本块中的特定单词或短语。他们选择一个标签,然后通过使用光标突出显示标签所应用的文本部分来应用该标签。Ground Truth 命名实体识别工具支持重叠注释、上下文中的标签选择和单个突出显示的多标签选择。此外,工作人员可以使用键盘快速选择标签。

重要

如果您手动创建输入清单文件,请使用 "source" 来识别要标注的文本。有关更多信息,请参阅 输入数据

创建命名实体识别标注作业(控制台)

您可以按照说明学习创建标注作业(控制台)如何在 SageMaker 控制台中创建命名实体识别标签作业。在步骤 10 中,从任务类别下拉菜单中选择文本,然后选择命名实体识别作为任务类型。

Ground Truth 为标注任务提供类似于以下内容的工作人员 UI。使用控制台创建标注作业时,需要指定说明,以便于工作人员完成工作人员可以从中选择的作业和标签。

Gif 显示了如何在 SageMaker 控制台中创建命名实体识别标签作业。

创建命名实体识别标注 Job (API)

要创建命名实体识别标注作业,请使用 SageMaker API操作CreateLabelingJob。这为所有人API定义了此操作 AWS SDKs。要查看此操作SDKs支持的特定语言列表,请查看的 “另请参阅” 部分。CreateLabelingJob

请按照创建标注作业 (API)中的说明进行操作,并在配置请求时执行以下操作:

  • 此任务类型的注释前 Lambda 函数以 PRE-NamedEntityRecognition 结尾。要查找您所在地区的预注释 ARN Lambda,请参阅。PreHumanTaskLambdaArn

  • 此任务类型的注释合并 Lambda 函数以 ACS-NamedEntityRecognition 结尾。要查找您所在地区的注释合并 Lambd ARN a,请参阅。AnnotationConsolidationLambdaArn

  • 您必须为以下内容ARN提供以下信息HumanTaskUiArn

    arn:aws:sagemaker:aws-region:394669845002:human-task-ui/NamedEntityRecognition

    aws-region替换为您用于创建标注任务的AWS区域。例如,如果您在美国西部(北加利福尼亚)创建标注作业,请使用 us-west-1

  • 使用 instructions 参数在标签类别配置文件中提供工作人员说明。可以在shortInstructionfullInstruction字段中使用字符串或HTML标记语言。有关更多详细信息,请参阅在标签类别配置文件中提供工作人员说明

    "instructions": {"shortInstruction":"<h1>Add header</h1><p>Add Instructions</p>", "fullInstruction":"<p>Add additional instructions.</p>"}

以下是在美国东部 SDK(弗吉尼亚北部)地区创建标签任务的AWS Python (Boto3) 请求示例。所有红色参数都应替换为您的规范和资源。

response = client.create_labeling_job( LabelingJobName='example-ner-labeling-job', LabelAttributeName='label', InputConfig={ 'DataSource': { 'S3DataSource': { 'ManifestS3Uri': 's3://bucket/path/manifest-with-input-data.json' } }, 'DataAttributes': { 'ContentClassifiers': [ 'FreeOfPersonallyIdentifiableInformation'|'FreeOfAdultContent', ] } }, OutputConfig={ 'S3OutputPath': 's3://bucket/path/file-to-store-output-data', 'KmsKeyId': 'string' }, RoleArn='arn:aws:iam::*:role/*', LabelCategoryConfigS3Uri='s3://bucket/path/label-categories.json', StoppingConditions={ 'MaxHumanLabeledObjectCount': 123, 'MaxPercentageOfInputDatasetLabeled': 123 }, HumanTaskConfig={ 'WorkteamArn': 'arn:aws:sagemaker:region:*:workteam/private-crowd/*', 'UiConfig': { 'HumanTaskUiArn': 'arn:aws:sagemaker:us-east-1:394669845002:human-task-ui/NamedEntityRecognition' }, 'PreHumanTaskLambdaArn': 'arn:aws:lambda:us-east-1:432418664414:function:PRE-NamedEntityRecognition', 'TaskKeywords': [ 'Named entity Recognition', ], 'TaskTitle': 'Named entity Recognition task', 'TaskDescription': 'Apply the labels provided to specific words or phrases within the larger text block.', 'NumberOfHumanWorkersPerDataObject': 1, 'TaskTimeLimitInSeconds': 28800, 'TaskAvailabilityLifetimeInSeconds': 864000, 'MaxConcurrentTaskCount': 1000, 'AnnotationConsolidationConfig': { 'AnnotationConsolidationLambdaArn': 'arn:aws:lambda:us-east-1:432418664414:function:ACS-NamedEntityRecognition' }, Tags=[ { 'Key': 'string', 'Value': 'string' }, ] )

在标签类别配置文件中提供工作人员说明

必须在用 CreateLabelingJob 中的 LabelCategoryConfigS3Uri 参数标识的标签类别配置文件中提供工作人员说明。您可以使用这些说明提供有关您希望工作人员执行的任务的详细信息,便于他们高效地使用工具。

您可以分别使用 instructions 参数中的 shortInstructionfullInstruction 提供简短和详细的说明。要了解有关这些说明类型的更多信息,请参阅创建说明页面

下面是一个标签类别配置文件示例,其中包含可用于命名实体识别标注作业的说明。

{ "document-version": "2018-11-28", "labels": [ { "label": "label1", "shortDisplayName": "L1" }, { "label": "label2", "shortDisplayName": "L2" }, { "label": "label3", "shortDisplayName": "L3" }, { "label": "label4", "shortDisplayName": "L4" }, { "label": "label5", "shortDisplayName": "L5" } ], "instructions": { "shortInstruction": "<p>Enter description of the labels that workers have to choose from</p><br><p>Add examples to help workers understand the label</p>", "fullInstruction": "<ol> <li><strong>Read</strong> the text carefully.</li> <li><strong>Highlight</strong> words, phrases, or sections of the text.</li> <li><strong>Choose</strong> the label that best matches what you have highlighted.</li> <li>To <strong>change</strong> a label, choose highlighted text and select a new label.</li> <li>To <strong>remove</strong> a label from highlighted text, choose the X next to the abbreviated label name on the highlighted text.</li> <li>You can select all of a previously highlighted text, but not a portion of it.</li> </ol>" } }

命名实体识别输出数据

创建命名实体识别标签任务后,您的输出数据将位于使用时S3OutputPath参数中指定的 Amazon S3 存储桶中,API或者位于控制台任务概述部分的输出数据集位置字段中。

要了解有关 Ground Truth 生成的输出清单文件以及 Ground Truth 用来存储输出数据的文件结构的更多信息,请参阅标注任务输出数据