前置知识: 自然语言处理

共指消解

00:00
5 min Intermediate

理解共指消解如何将代词链接到实体,为下游NLP任务提供完整上下文

共指消解

“她签署了协议。” 谁?共指消解回答这个问题。没有它,你的NER系统知道”她”是一个人但不知道是哪个人。

类型: 学习 语言: Python 前置条件: Phase 5 · 06(NER),Phase 5 · 07(词性标注与解析) 时间: ~60 分钟

问题

“Tim Cook became CEO of Apple in 2011. He previously worked at IBM.” NER标记”Tim Cook”和”He”为PERSON。但”He”指谁?没有共指消解,下游系统无法将两个提及链接到同一个实体。

共指消解将引用同一实体的所有提及分组为簇。输入:一个文档。输出:提及簇集合,每个簇引用一个唯一实体。

概念

提及检测。 找到所有可能引用实体的span:专有名词、代词、名词短语。通常与NER联合完成。

指代对分类。 给定两个提及,它们是否指同一实体?这是核心分类决策。特征包括:性别/数量一致性、句法角色、语义兼容性、距离。

提及聚类。 将正对链接为簇。传递闭包:如果A=B且B=C,则A=C。更高级方法使用图聚类。

架构。

  • 基于规则。 多通道系统(Hobbs, 1978; Lee et al., 2013)。确定性、可调试、高精确度低召回率。
  • SpanBERT。 对span对进行端到端分类。2018-2022年标准神经方法。
  • 基于Transformer。 长文档上使用Longformer/BigBird编码器。当前SOTA。

评估 MUC、B-cubed、CEAF-F指标。每个从不同角测量簇质量报告所有三个 + CoNLL平均(三者的均)。

构建它

步骤 1:基于规则的代词消解

def resolve_pronouns(doc_tokens, pos_tags, ner_tags):
    entities = []
    for i, (token, pos, ner) in enumerate(zip(doc_tokens, pos_tags, ner_tags)):
        if ner.startswith("B-PER"):
            entities.append({"mention": token, "type": "PER", "index": i, "gender": infer_gender(token)})

    clusters = {e["mention"]: [e["index"]] for e in entities}

    for i, (token, pos, ner) in enumerate(zip(doc_tokens, pos_tags, ner_tags)):
        if pos == "PRP" and token.lower() in {"he", "him", "his"}:
            for entity in reversed(entities):
                if entity["gender"] == "M" and entity["index"] < i:
                    clusters[entity["mention"]].append(i)
                    break
        elif pos == "PRP" and token.lower() in {"she", "her", "hers"}:
            for entity in reversed(entities):
                if entity["gender"] == "F" and entity["index"] < i:
                    clusters[entity["mention"]].append(i)
                    break
    return clusters

步骤 2:使用spaCy的神经共指

import spacy

nlp = spacy.load("en_coreference_web_trf")

doc = nlp("Tim Cook became CEO of Apple in 2011. He previously worked at IBM.")
for cluster in doc.spans:
    print(f"Cluster: {[str(m) for m in cluster]}")

步骤 3:评估指标

def muc_score(predicted_clusters, gold_clusters):
    """MUC: 基于链接的指标"""
    total_links = sum(len(c) - 1 for c in gold_clusters)
    found = 0
    for gold in gold_clusters:
        for pred in predicted_clusters:
            overlap = set(gold) & set(pred)
            if overlap:
                found += len(overlap) - 1
                break
    return found / total_links if total_links else 0

使用它

场景选择
快速、确定性基于规则通道)
生产质量spaCy共指模型
准确SpanBERT或Longformer
系统增量共指(逐更新)

共指消解在以下场景中承重:

  • 关系抽取。 “He founded Apple” — 需要知道”He” = “Tim Cook”。
  • 实体链接。 链接代词到KB实体。
  • 摘要 代词消解后摘要更连贯。
  • 问答。 “Who became CEO?” — 答案可能在代词指代的句子中。

交付它

结果保存为 outputs/skill-coref-picker.md

练习

  1. 简单 在10个手工编写的句子实现基于规则的代词消解。测量准确
  2. 中等。 在OntoNotes数据集子集上运行spaCy共指模型。与基于规则比较。
  3. 困难。 构建共指感知的关系抽取流水线。测量有和无共指的RE F1。

关键术语

术语通俗说法实际含义
共指指同一实体引用同一实体的不同提及。
提及实体引用引用实体的任何span名字、代词、名词短语)。
同一实体引用同一实体的提及集合
MUC评估指标基于链接的共指评估指标。

延伸阅读

知识检测

学习进度

-- 已学文档
--% 知识覆盖率

学习推荐

专注模式