【问题标题】:Custom sentence boundary detection in SpaCySpaCy 中的自定义句子边界检测
【发布时间】:2020-11-13 20:11:27
【问题描述】:

我正在尝试在 spaCy 中编写一个自定义句子分段器,它将整个文档作为一个句子返回。

我使用here 中的代码编写了一个自定义管道组件。

但我无法让它工作,因为它不会更改句子边界以将整个文档视为一个句子,而是会引发两个不同的错误。

如果我创建一个空白语言实例并且只将我的自定义组件添加到管道中,我会收到以下错误:

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.

因此,如果它抛出一个需要依赖解析的错误,如果它不存在或者它在自定义句子边界检测之后执行,并且在首先执行依赖解析时出现不同的错误,那么合适的方法是什么?

谢谢!

【问题讨论】:

    标签: python nlp spacy


    【解决方案1】:

    来自 spaCy 的 Ines 回答了我的问题 here

    感谢您提出这个问题 - 抱歉,这有点令人困惑。 我很确定您描述的第一个问题已经解决 掌握。 spaCy 绝对应该尊重自定义句子的边界, 即使在没有依赖解析器的管道中。

    如果您想在没有解析器的情况下使用您的自定义 SBD 组件,非常 简单的解决方案是在您的自定义中设置 doc.is_parsed = True 零件。所以当 Doc.sents 检查依赖解析时,它看起来 在 is_parsed 并且不会抱怨。

    如果您想将组件与解析器一起使用,请确保添加它 在解析器之前。解析器应该始终尊重已经设置的 先前处理步骤的句子边界。

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 2012-07-24
      • 1970-01-01
      • 2011-06-29
      • 2017-12-20
      • 2012-07-08
      • 2019-02-11
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多