Skip to content

片段链接

在已标注的文本片段之间创建类型化关系。

片段链接标注

v2.1.0 新增

片段链接允许标注者在已标注的片段(文本段)之间创建类型化关系。这对关系抽取任务非常有用,您需要识别实体之间的关系,例如"PERSON works_for ORGANIZATION"或"PERSON collaborates_with PERSON"。

概述

片段链接功能提供:

  • 类型化关系:定义具有不同颜色和约束的多种链接类型
  • 有向和无向链接:支持方向性和对称关系
  • N 元链接:在多个片段之间创建链接(不仅限于成对)
  • 可视化弧形显示:以彩色弧线渲染在文本上方的关系
  • 标签约束:限制哪些片段标签可以作为源或目标

配置

要使用片段链接,您需要两个标注方案:

  1. 一个 span 方案用于标注实体
  2. 一个 span_link 方案用于标注关系
yaml
annotation_schemes:
  # First, define the span schema for named entities
  - annotation_type: span
    name: entities
    description: "Highlight named entities"
    labels:
      - name: "PERSON"
        color: "#3b82f6"
      - name: "ORGANIZATION"
        color: "#22c55e"
      - name: "LOCATION"
        color: "#f59e0b"
    sequential_key_binding: true
 
  # Then, define the span_link schema for relationships
  - annotation_type: span_link
    name: relations
    description: "Annotate relationships between entities"
    span_schema: entities  # References the span schema above
    link_types:
      - name: "WORKS_FOR"
        directed: true
        allowed_source_labels: ["PERSON"]
        allowed_target_labels: ["ORGANIZATION"]
        color: "#dc2626"
      - name: "COLLABORATES_WITH"
        directed: false
        allowed_source_labels: ["PERSON"]
        allowed_target_labels: ["PERSON"]
        color: "#06b6d4"

配置选项

必填字段

字段描述
annotation_type必须为 "span_link"
name方案的唯一标识符
description显示给标注者的描述
span_schema要链接的片段标注方案名称
link_types链接类型定义数组

链接类型选项

字段类型描述
namestring必填。 链接类型的名称/标签
directedboolean链接是否有方向。默认:false
colorstring链接的十六进制颜色代码
allowed_source_labelsarray可以作为源的片段标签
allowed_target_labelsarray可以作为目标的片段标签
max_spansinteger此链接类型中的最大片段数(用于 N 元链接)

视觉显示选项

yaml
visual_display:
  enabled: true        # Show arc visualization
  arc_position: "above"  # Position of arcs
  show_labels: true    # Show link type labels on arcs

使用方法

创建片段

  1. 首先,通过选择片段标签并高亮文本来标注文本
  2. 每个高亮的片段都可用于链接

创建链接

  1. 从可用选项中选择链接类型
  2. 点击片段将其添加到当前链接(选中的片段会高亮显示)
  3. 点击"创建链接"完成链接
  4. 对于有向链接,第一个选中的片段是源,最后一个是目标

查看链接

  • 已创建的链接出现在"已有链接"部分
  • 如果启用了视觉显示,弧线会在文本上方连接链接的片段
  • 弧线按链接类型进行颜色编码

删除链接

  • 点击"已有链接"部分中任何链接旁边的删除按钮(x)

数据格式

输入数据

带有 ID 和文本字段的标准文本数据:

json
[
  {
    "id": "item_1",
    "text": "John Smith works at Google as a senior engineer."
  }
]

输出格式

标注包括片段标注和链接标注:

json
{
  "id": "item_1",
  "text": "John Smith works at Google as a senior engineer.",
  "entities": [
    {"start": 0, "end": 10, "label": "PERSON", "text": "John Smith"},
    {"start": 20, "end": 26, "label": "ORGANIZATION", "text": "Google"}
  ],
  "relations": [
    {
      "link_type": "WORKS_FOR",
      "span_ids": ["span_abc123", "span_def456"],
      "direction": "directed"
    }
  ]
}

标签约束

限制哪些片段标签可以参与每种链接类型:

yaml
link_types:
  - name: "WORKS_FOR"
    directed: true
    allowed_source_labels: ["PERSON"]      # Only PERSON can be source
    allowed_target_labels: ["ORGANIZATION"]  # Only ORGANIZATION can be target

当违反约束时,UI 会显示错误消息并阻止创建链接。

N 元链接

默认情况下,链接连接恰好 2 个片段。对于涉及更多实体的关系:

yaml
link_types:
  - name: "MEETING"
    directed: false
    max_spans: 5  # Allow up to 5 participants
    allowed_source_labels: ["PERSON"]
    allowed_target_labels: ["PERSON"]

示例

完整的工作示例位于:

  • 配置:project-hub/simple_examples/simple-span-linking/config.yaml
  • 数据:project-hub/simple_examples/simple-span-linking/data.json

运行命令:

bash
cd project-hub/simple_examples
python ../../potato/flask_server.py start simple-span-linking/config.yaml -p 9001

提示

  1. 先标注片段:确保在尝试链接之前已创建片段标注
  2. 有效使用颜色:为不同的链接类型选择独特的颜色
  3. 考虑方向:当关系有明确方向时使用有向链接
  4. 键盘快捷键:片段标签支持键盘快捷键(1、2、3...)以加快标注

延伸阅读

有关实现细节,请参阅源文档