Skip to content

片段標註

高亮並標記文本片段,用於命名實體識別等任務。

片段標註允許標註者選擇並標記文本的一部分,常用於命名實體識別(NER)、詞性標註和文本高亮任務。

基本配置

yaml
annotation_schemes:
  - annotation_type: span
    name: entities
    description: "Highlight named entities in the text"
    labels:
      - PERSON
      - ORGANIZATION
      - LOCATION

配置選項

實體標籤

定義標註者可以建立的片段類型:

yaml
labels:
  - PERSON
  - ORGANIZATION
  - LOCATION
  - DATE
  - EVENT

標籤顏色

自定義顏色以進行視覺區分:

yaml
label_colors:
  PERSON: "#3b82f6"
  ORGANIZATION: "#10b981"
  LOCATION: "#f59e0b"
  DATE: "#8b5cf6"
  EVENT: "#ec4899"

顏色可以是十六進位制格式(#ff0000)或 RGB 格式(rgb(255, 0, 0))。

鍵盤快捷鍵

通過鍵盤繫結加速標註:

yaml
keyboard_shortcuts:
  PERSON: "1"
  ORGANIZATION: "2"
  LOCATION: "3"
  DATE: "4"

工具提示

為每個標籤提供指導:

yaml
tooltips:
  PERSON: "Names of people, characters, or personas"
  ORGANIZATION: "Companies, agencies, institutions"
  LOCATION: "Physical locations, addresses, geographic regions"

重疊片段

允許重疊

啟用可以重疊的片段:

yaml
- annotation_type: span
  name: entities
  labels:
    - PERSON
    - ROLE
  allow_discontinuous: true

當同一文本可以有多個標籤時(例如 "Dr. Smith" 既是 PERSON 又有 ROLE),這很有用。

禁止重疊(預設)

yaml
- annotation_type: span
  name: entities
  labels:
    - PERSON
    - ORGANIZATION
  allow_discontinuous: false  # Default behavior

片段選擇模式

詞級選擇

僅選擇完整的詞:

yaml
- annotation_type: span
  name: entities
  labels:
    - ENTITY

字元級選擇

允許選擇部分詞:

yaml
- annotation_type: span
  name: entities
  labels:
    - ENTITY

預標註片段

載入現有標註用於稽核或修正:

json
{
  "id": "doc1",
  "text": "John Smith works at Microsoft in Seattle.",
  "spans": [
    {"start": 0, "end": 10, "label": "PERSON"},
    {"start": 20, "end": 29, "label": "ORGANIZATION"},
    {"start": 33, "end": 40, "label": "LOCATION"}
  ]
}

配置載入預標註:

yaml
- annotation_type: span
  name: entities

常見 NER 配置

標準 NER(4 種類型)

yaml
- annotation_type: span
  name: ner
  description: "Label named entities"
  labels:
    - PER    # Person
    - ORG    # Organization
    - LOC    # Location
    - MISC   # Miscellaneous
  sequential_key_binding:
    PER: "1"
    ORG: "2"
    LOC: "3"
    MISC: "4"

擴充套件 NER(OntoNotes 風格)

yaml
- annotation_type: span
  name: ner_extended
  labels:
    - PERSON
    - NORP        # Nationalities, religious/political groups
    - FAC         # Facilities
    - ORG
    - GPE         # Geopolitical entities
    - LOC
    - PRODUCT
    - EVENT
    - WORK_OF_ART
    - LAW
    - LANGUAGE
    - DATE
    - TIME
    - PERCENT
    - MONEY
    - QUANTITY
    - ORDINAL
    - CARDINAL

生物醫學 NER

yaml
- annotation_type: span
  name: bio_ner
  labels:
    - GENE
    - PROTEIN
    - DISEASE
    - DRUG
    - SPECIES

社交媒體 NER

yaml
- annotation_type: span
  name: social_ner
  labels:
    - PERSON
    - ORGANIZATION
    - LOCATION
    - PRODUCT
    - CREATIVE_WORK
    - GROUP

帶屬性的片段

為片段新增屬性以進行更豐富的標註:

yaml
annotation_schemes:
  - annotation_type: span
    name: entities
    labels:
      - PERSON
      - ORGANIZATION
 
  - annotation_type: radio
    name: entity_type
    description: "What type of entity is this?"
    labels:
      - Named
      - Nominal
      - Pronominal

多片段方案

分別標註不同方面:

yaml
annotation_schemes:
  # Named entities
  - annotation_type: span
    name: entities
    description: "Label named entities"
    labels:
      - PERSON
      - ORGANIZATION
      - LOCATION
 
  # Sentiment expressions
  - annotation_type: span
    name: sentiment_spans
    description: "Highlight sentiment expressions"
    labels:
      - POSITIVE
      - NEGATIVE

多欄位片段標註

v2.1.0 新增

片段標註可以使用 target_field 選項定位多欄位資料中的特定文本欄位。當您的資料包含多個文本欄位並且您想在特定欄位中標註片段時,這非常有用。

配置

yaml
annotation_schemes:
  - annotation_type: span
    name: source_entities
    description: "Label entities in the source text"
    labels:
      - PERSON
      - ORGANIZATION
 
  - annotation_type: span
    name: summary_entities
    description: "Label entities in the summary"
    labels:
      - PERSON
      - ORGANIZATION

多欄位資料格式

您的資料應包含單獨的文本欄位:

json
{
  "id": "doc1",
  "source_text": "John Smith works at Microsoft in Seattle.",
  "summary": "Smith is employed by Microsoft."
}

輸出格式

使用 target_field 時,標註按欄位鍵組織:

json
{
  "id": "doc1",
  "source_entities": {
    "source_text": [
      {"start": 0, "end": 10, "text": "John Smith", "label": "PERSON"},
      {"start": 20, "end": 29, "text": "Microsoft", "label": "ORGANIZATION"}
    ]
  },
  "summary_entities": {
    "summary": [
      {"start": 0, "end": 5, "text": "Smith", "label": "PERSON"},
      {"start": 22, "end": 31, "text": "Microsoft", "label": "ORGANIZATION"}
    ]
  }
}

完整的工作示例請參見 Potato 倉庫中的 project-hub/simple_examples/simple-multi-span/

顯示選項

在片段中顯示標籤

在高亮片段內顯示標籤文本:

yaml
- annotation_type: span
  name: entities

下劃線樣式

使用下劃線代替背景高亮:

yaml
- annotation_type: span
  name: entities

輸出格式

片段標註以字元偏移量儲存:

json
{
  "id": "doc1",
  "entities": [
    {
      "start": 0,
      "end": 10,
      "text": "John Smith",
      "label": "PERSON"
    },
    {
      "start": 20,
      "end": 29,
      "text": "Microsoft",
      "label": "ORGANIZATION"
    }
  ]
}

完整示例:NER 任務

yaml
task_name: "Named Entity Recognition"
 
data_files:
  - path: data/documents.json
    text_field: text
 
annotation_schemes:
  - annotation_type: span
    name: entities
    description: "Highlight and label all named entities"
    labels:
      - PERSON
      - ORGANIZATION
      - LOCATION
      - DATE
      - MONEY
    sequential_key_binding:
      PERSON: "1"
      ORGANIZATION: "2"
      LOCATION: "3"
      DATE: "4"
      MONEY: "5"
    allow_discontinuous: false
 
  - annotation_type: radio
    name: difficulty
    description: "How difficult was this document to annotate?"
    labels:
      - Easy
      - Medium
      - Hard

不連續片段

v2.2.0 新增

使用 allow_discontinuous 參數啟用不連續文本片段。這允許標註者選擇多個不相鄰的文本段作為單個片段標註,適用於不連續實體或分割表達。

yaml
- annotation_type: span
  name: entities
  labels:
    - PERSON
    - ORGANIZATION
  allow_discontinuous: true

啟用後,標註者可以按住修飾鍵同時選擇額外的文本段以將其新增到當前片段。輸出包含每個段的多個起始/結束對。

實體連結整合

v2.2.0 新增

通過向片段方案新增 entity_linking 配置塊,可以將片段標註連結到外部知識庫(Wikidata、UMLS 或自定義 REST API):

yaml
- annotation_type: span
  name: entities
  labels:
    - PERSON
    - ORGANIZATION
    - LOCATION
  entity_linking:
    enabled: true
    knowledge_bases:
      - name: wikidata
        type: wikidata
        language: en

啟用實體連結後,每個片段的控制欄上會出現連結圖示。點選它會開啟搜尋模態框以查詢和連結匹配的知識庫實體。詳情請參閱實體連結文件。

最佳實踐

  1. 使用獨特的顏色以便於視覺區分
  2. 為每種實體類型提供清晰的工具提示和示例
  3. 啟用鍵盤快捷鍵以加快標註速度
  4. 使用詞級選擇除非需要字元精度
  5. 考慮預標註以加快修正工作流程
  6. 根據標註指南測試重疊設定

延伸閱讀

有關實現細節,請參閱源文件