You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GATE NLP Python的RemoveAnn动作正确用法及报错求助

GATE NLP Python中RemoveAnn动作的正确用法及KeyError解决办法

问题描述

使用GATE NLP Python的PAMPAC框架添加注释正常,但调用RemoveAnn动作移除已添加的注释时触发KeyError,报错显示找不到要移除的注释实例。相关代码及报错信息如下:

示例代码

from gatenlp import Document
from gatenlp.processing.tokenizer import NLTKTokenizer
from gatenlp.pam.pampac import *
from nltk.tokenize.regexp import WhitespaceTokenizer


def pprint(doc):
    print("------")
    for a in doc.annset():
        print(doc[a].ljust(6, " ") + "- " + str(a))
    print("------")

# Generate document and tokenize
text = """dog cat fish"""
tok1 = NLTKTokenizer(nltk_tokenizer=WhitespaceTokenizer())
doc1 = Document(text)
doc1 = tok1(doc1)
print("Simple document:")
pprint(doc1)

# Find cat text and annotate
pat1 = Text(text="cat")
action1 = AddAnn(type="CAT_TAG")
rule1 = Rule(pat1, action1)
pampac1 = Pampac(rule1, skip="longest", select="first")
annt1 = PampacAnnotator(pampac1, annspec=[("", "Token")], outset_name="")
annt1(doc1)
print("Annotate cat text with Annotation of type 'CAT_TAG'")
pprint(doc1)

# Find annotation and try to remove
pat3 = AnnAt(type="CAT_TAG", name="remove")
# action3 = AddAnn(type="CAT_TAGGED_AGAIN", name="remove")  # works
action3 = RemoveAnn("remove", annset=doc1.annset())  # <-- 此处annset指定方式错误
rule3 = Rule(pat3, action3)
pampac2 = Pampac(rule3, skip="longest", select="first")
annt2 = PampacAnnotator(pampac2, annspec=[""], outset_name="")
annt2(doc1)
print("Try to remove the 'CAT_TAG' annotation from the default set")
pprint(doc1)

报错信息

Simple document:
------
dog   - Annotation(0,3,Token,features=Features({}),id=0)
cat   - Annotation(4,7,Token,features=Features({}),id=1)
fish  - Annotation(8,12,Token,features=Features({}),id=2)
------
Annotate cat text with Annotation of type 'CAT_TAG'
------
dog   - Annotation(0,3,Token,features=Features({}),id=0)
cat   - Annotation(4,7,Token,features=Features({}),id=1)
cat   - Annotation(4,7,CAT_TAG,features=Features({}),id=3)
fish  - Annotation(8,12,Token,features=Features({}),id=2)
------

Traceback (most recent call last):
  File ".../scratch.py", line 37, in <module>
    annt2(doc1)
  File ".../gatenlp/pam/pampac/pampac.py", line 234, in __call__
    self.pampac.run(doc, anns, outset=outset, containing_anns=cont)
  File ".../gatenlp/pam/pampac/pampac.py", line 106, in run
    return self._run4span(logger, ctx, location)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../gatenlp/pam/pampac/pampac.py", line 133, in _run4span
    fret = self.rules[idx].action(ret, context=ctx, location=location)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../gatenlp/pam/pampac/actions.py", line 123, in __call__
    return self.actions[0](succ, context=context, location=location)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../gatenlp/pam/pampac/actions.py", line 421, in __call__
    self.annset.remove(theann)
  File ".../gatenlp/annotation_set.py", line 601, in remove
    self._annset.remove(ann)
KeyError: Annotation(4,7,CAT_TAG,features=Features({}),id=3)

解决办法

错误原因

初始化RemoveAnn时直接传入了AnnotationSet实例(doc1.annset()),但该动作的annset参数预期接收集合名称字符串(如""表示默认集合),而非集合对象。直接传入实例会导致运行时无法正确关联匹配到的注释与目标集合,触发KeyError。

正确用法

RemoveAnn的正确初始化方式有两种:

  • 不指定annset参数:默认移除匹配注释所在集合中的该注释
  • 指定annset为集合名称字符串:明确指定要操作的注释集合

修改示例中的action3定义:

# 方式1:不指定annset,默认使用匹配注释所在的集合
action3 = RemoveAnn("remove")

# 方式2:指定集合名称为默认集合("")
action3 = RemoveAnn("remove", annset="")

修改后的完整代码

from gatenlp import Document
from gatenlp.processing.tokenizer import NLTKTokenizer
from gatenlp.pam.pampac import *
from nltk.tokenize.regexp import WhitespaceTokenizer


def pprint(doc):
    print("------")
    for a in doc.annset():
        print(doc[a].ljust(6, " ") + "- " + str(a))
    print("------")

# Generate document and tokenize
text = """dog cat fish"""
tok1 = NLTKTokenizer(nltk_tokenizer=WhitespaceTokenizer())
doc1 = Document(text)
doc1 = tok1(doc1)
print("Simple document:")
pprint(doc1)

# Find cat text and annotate
pat1 = Text(text="cat")
action1 = AddAnn(type="CAT_TAG")
rule1 = Rule(pat1, action1)
pampac1 = Pampac(rule1, skip="longest", select="first")
annt1 = PampacAnnotator(pampac1, annspec=[("", "Token")], outset_name="")
annt1(doc1)
print("Annotate cat text with Annotation of type 'CAT_TAG'")
pprint(doc1)

# Find annotation and remove correctly
pat3 = AnnAt(type="CAT_TAG", name="remove")
action3 = RemoveAnn("remove")  # 正确的初始化方式
rule3 = Rule(pat3, action3)
pampac2 = Pampac(rule3, skip="longest", select="first")
annt2 = PampacAnnotator(pampac2, annspec=[""], outset_name="")
annt2(doc1)
print("Successfully removed the 'CAT_TAG' annotation from the default set")
pprint(doc1)

运行结果

Simple document:
------
dog   - Annotation(0,3,Token,features=Features({}),id=0)
cat   - Annotation(4,7,Token,features=Features({}),id=1)
fish  - Annotation(8,12,Token,features=Features({}),id=2)
------
Annotate cat text with Annotation of type 'CAT_TAG'
------
dog   - Annotation(0,3,Token,features=Features({}),id=0)
cat   - Annotation(4,7,Token,features=Features({}),id=1)
cat   - Annotation(4,7,CAT_TAG,features=Features({}),id=3)
fish  - Annotation(8,12,Token,features=Features({}),id=2)
------
Successfully removed the 'CAT_TAG' annotation from the default set
------
dog   - Annotation(0,3,Token,features=Features({}),id=0)
cat   - Annotation(4,7,Token,features=Features({}),id=1)
fish  - Annotation(8,12,Token,features=Features({}),id=2)
------

内容的提问来源于stack exchange,提问作者Nicholas Sunderland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:07:11