【问题标题】:Add a SpaCy Tokenizer Exception: Do not split '>>'添加 SpaCy 标记器异常:不要拆分“>>”
【发布时间】:2018-09-08 15:36:18
【问题描述】:

我正在尝试添加一个例外来识别“>>”和“>>”作为开始新句子的指示符。例如,

import spacy

nlp = spacy.load('en_core_web_sm')
doc = nlp(u'>> We should. >>No.')

for sent in doc.sents:
    print (sent)

打印出来:

>> We should.
>
>
No.

但是,我想把它打印出来:

>> We should.
>> No. 

提前感谢您的宝贵时间!

【问题讨论】:

  • 这个答案可能是您正在寻找的:stackoverflow.com/a/51776803/2198727。您只需要将字符序列调整为要标记的字符序列(即使用“>>”作为特殊情况模式)。

标签: nlp tokenize spacy


【解决方案1】:

您需要创建一个custom component。代码示例提供自定义句子分割example。从文档中,该示例执行以下操作:

添加管道组件以禁止句子边界的示例 在某些标记之前。

代码(根据您的需要调整示例):

import spacy


def prevent_sentence_boundaries(doc):
    for token in doc:
        if not can_be_sentence_start(token):
            token.is_sent_start = False
    return doc


def can_be_sentence_start(token):
    if token.i > 0 and token.nbor(-1).text == '>':
        return False
    return True

nlp = spacy.load('en_core_web_sm')
nlp.add_pipe(prevent_sentence_boundaries, before='parser')

raw_text = u'>> We should. >> No.'
doc = nlp(raw_text)
sentences = [sent.string.strip() for sent in doc.sents]
for sentence in sentences:
    print(sentence)

输出

>> We should.
>> No.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    相关资源
    最近更新 更多