我正在尝试在 spaCy 中编写一个自定义句子分段器,它将整个文档作为单个句子返回。
我编写了一个自定义管道组件,它使用以下代码来执行此操作here https://github.com/explosion/spaCy/issues/1850.
但我无法让它工作,因为它不会更改句子边界以将整个文档作为单个句子,而是会引发两个不同的错误。
如果我创建一个空白语言实例并仅将自定义组件添加到管道中,我会收到此错误:
ValueError: Sentence boundary detection requires the dependency parse, which requires a statistical model to be installed and loaded.
如果我将解析器组件添加到管道中
nlp = spacy.blank('es')
parser = nlp.create_pipe('parser')
nlp.add_pipe(parser, last=True)
def custom_sbd(doc):
print("EXECUTING SBD!!!!!!!!!!!!!!!!!!!!")
doc[0].sent_start = True
for i in range(1, len(doc)):
doc[i].sent_start = False
return doc
nlp.begin_training()
nlp.add_pipe(custom_sbd, first=True)
我犯了同样的错误。
如果我更改它首先解析的顺序,然后更改句子边界,则错误将更改为
Refusing to write to token.sent_start if its document is parsed, because this may cause inconsistent state.
因此,如果它抛出一个错误,要求依赖项解析(如果它不存在)或者在自定义句子边界检测之后执行,并且在首先执行依赖项解析时抛出一个不同的错误,那么正确的方法是什么?
谢谢你!