

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

# 使用具名實體辨識擷取文字資訊
<a name="sms-named-entity-recg"></a>

若要從非結構化文字中擷取資訊並將其分類到預先定義的類別中，請使用 Amazon SageMaker Ground Truth 具名實體辨識 (NER) 標籤任務。傳統上，NER 包含篩選文字資料來尋找稱為*命名實體*的名詞片語，並使用標籤逐一進行分類，例如 “人員”、“組織” 或 “品牌”。您可以將此任務擴大為標籤較長的文字範圍，並使用您指定的預先定義標籤來分類那些文字序列。您可以使用 Amazon SageMaker AI 主控台的 Ground Truth 區段或 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) 作業來建立具名實體辨識標籤工作。

在獲派具名實體辨識標籤工作時，工作者會將您的標籤套用到較大文字區塊內的特定文字或片語。他們會選擇標籤，然後使用游標反白套用標籤的文字部分，以套用該標籤。Ground Truth 具名實體辨識工具支援重疊註釋、內容標籤選取，以及單一強調顯示多標籤選取。此外，工作者可以使用鍵盤快速選取標籤。

**重要**  
如果您手動建立輸入資訊清單檔案，請使用 `"source"` 來識別您要標籤的文字。如需詳細資訊，請參閱[輸入資料](sms-data-input.md)。

## 建立具名實體辨識標籤工作 (主控台)
<a name="sms-creating-ner-console"></a>

您可以依照指示 [建立標籤工作 (主控台)](sms-create-labeling-job-console.md)，了解如何在 SageMaker AI 主控台建立具名實體辨識標籤工作。在步驟 10 中，從**任務類別**下拉式清單中選擇**文字**，然後選擇**具名實體辨識**作為任務類型。

Ground Truth 提供類似下列標籤任務的工作者使用者介面。使用主控台建立標籤工作時，您可以指定指示以協助工作者完成工作，以及工作者可以選擇的標籤。

![\[顯示如何在 SageMaker AI 主控台建立具名實體辨識標籤工作的 GIF。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/sms/gifs/nertool.gif)


## 建立具名實體辨識標籤工作 (API)
<a name="sms-creating-ner-api"></a>

使用 SageMaker API 作業 `CreateLabelingJob` 建立具名實體辨識標籤工作。此 API 會定義 AWS SDKs此操作。若要查看這項作業支援的特定語言 SDK 清單，請參閱 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) 的**另請參閱**一節。

設定請求時，請遵循[建立標籤工作 (API)](sms-create-labeling-job-api.md)上的指示並執行下列動作：
+ 此任務類型的註釋前 Lambda 函式會以 `PRE-NamedEntityRecognition` 結尾。若要尋找您區域的註釋前 Lambda ARN，請參閱 [PreHumanTaskLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_HumanTaskConfig.html#SageMaker-Type-HumanTaskConfig-PreHumanTaskLambdaArn)。
+ 此任務類型的註釋合併 Lambda 函式會以 `ACS-NamedEntityRecognition` 結尾。若要尋找您區域的註釋合併 Lambda ARN，請參閱 [AnnotationConsolidationLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_AnnotationConsolidationConfig.html#SageMaker-Type-AnnotationConsolidationConfig-AnnotationConsolidationLambdaArn)。
+ 您必須為 `[HumanTaskUiArn](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UiConfig.html#sagemaker-Type-UiConfig-HumanTaskUiArn)` 提供下列項目：

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

  以您用來建立標籤工作的 AWS 區域取代 `aws-region`。例如，在美國西部 (北加州) 中建立標籤工作時使用 `us-west-1`。
+ 使用 `instructions` 參數在標籤類別組態檔案中提供工作者指示。您可以在 `shortInstruction` 和 `fullInstruction` 欄位中使用字串或 HTML 標記語言。如需詳細資訊，請參閱[在標籤類別組態檔案中提供工作者指示](#worker-instructions-ner)。

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

下列是用 [AWS Python SDK (Boto3) 請求](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_labeling_job)在美國東部 (維吉尼亞北部) 區域建立標籤工作的範例。所有以紅色標示的參數都應該用您的規格和資源加以取代。

```
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'
        },
    ]
)
```

### 在標籤類別組態檔案中提供工作者指示
<a name="worker-instructions-ner"></a>

您必須在使用 `CreateLabelingJob` 中的 `LabelCategoryConfigS3Uri` 參數識別的標籤類別組態檔案中提供工作者指示。您可以使用這些指示來提供您希望工作者執行的任務詳細資訊，並協助他們有效率地使用工具。

您可以分別使用 `instructions` 參數中的 `shortInstruction` 和 `fullInstruction` 提供簡短指示和詳細指示。若要深入了解這些指示類型，請參閱[建立指示頁面](sms-creating-instruction-pages.md)。

以下是標籤類別組態檔案的範例，其中包含可用於具名實體辨識標籤工作的指示。

```
{
  "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>"
  }
}
```

## 具名實體辨識輸出資料
<a name="sms-ner-output-data"></a>

建立具名實體辨識標籤工作後，您的輸出資料會位於使用 API 時在 `S3OutputPath` 參數中指定的 Amazon S3 儲存貯體中，或在主控台**工作概觀**區段的**輸出資料集位置**中。

若要進一步了解 Ground Truth 產生的輸出資訊清單檔案，以及 Ground Truth 用來儲存輸出資料的檔案結構，請參閱[標籤工作輸出資料](sms-data-output.md)。