Spacy 在简单的 .spans 赋值中返回“AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'spans'”为什么?

问题描述

我只是想按照 Spacy's documentation

将文档的子部分标记为跨度
interface TableColumnType<RowType,CellType> {
  name: string;
  dataExtractor: (row: RowType) => CellType;
  renderCell: (data: CellType) => React.ReactChild;
  sortable?: boolean;
  sortFunction: (row: RowType) => number;
}

interface TableSchemaType<T> {
  columns: TableColumnType<T>[];
}

这看起来很简单,但 Spacy 返回以下错误(并将其归因于第二行,即赋值):

import spacy
nlp = spacy.load('en_core_web_sm')
sentence = "The car with the white wheels was being confiscated by the police when the owner returns from robbing a bank"
doc = nlp(sentence)

doc.spans['remove_parts'] = [doc[2:6],doc[9:12]]
doc.spans['remove_parts']

我完全看不到发生了什么。这是一个 Spacy 错误吗?即使 AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'spans' 属性仍在文档中,它是否已被删除?如果不是,我错过了什么?

PD:我为此使用 Colab。 spans 显示

spacy.info

解决方法

此代码:

nlp = English()
text = "The car with the white wheels was being confiscated by the police when the owner returns from robbing a bank"
doc = nlp(text)
doc.spans['remove_parts'] = [doc[2:6],doc[9:12]]
doc.spans['remove_parts']

应该可以正常从 spaCy v3.0 开始。如果没有 - 您能否验证您实际上是从 colab 中的正确虚拟环境(而不是使用 spaCy v2 的不同环境)运行代码吗?我们之前曾看到 Colab 仍会访问系统上较旧的 spaCy 安装的问题,而不是从正确的 venv 获取代码。要仔细检查,您可以尝试直接在 Python 控制台中运行代码,而不是通过 Colab。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...