Spacy returns "AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'spans'" in simple .spans assignment. Why?

Spacy returns "AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'spans'" in simple .spans assignment. Why?

我只是想根据 Spacy's documentation

将文档的子部分标记为跨度
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 returns 出现以下错误(并将其归因于第二行,即赋值):

AttributeError: 'spacy.tokens.doc.Doc' object has no attribute 'spans'

我完全看不出发生了什么。这是一个 Spacy 错误吗? spans 属性 是否已被删除,即使它仍在文档中?如果不是,我错过了什么?

PD:我正在为此使用 Colab。 spacy.info 显示:

spaCy version    2.2.4                         
Location         /usr/local/lib/python3.7/dist-packages/spacy
Platform         Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic
Python version   3.7.10                        
Models           en    

此代码:

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。