python spacy句子分割器

标签 python spacy sentence

我想使用 spacy 从文本中获取句子。

nlp = English()  # just the language with no model
sentencizer = nlp.create_pipe("sentencizer")
nlp.add_pipe(sentencizer)
doc = nlp("This is a sentence. This is another sentence.")
for sent in doc.sents:
    print(sent.text)

是否可以提高句子分割器绕过规则的可靠性,例如从不在“no”等首字母缩略词之后分割句子。

想象一下,我当然有一堆非常技术性和特殊的缩写词。
您将如何进行?

最佳答案

您可以编写一个自定义函数,通过使用基于规则的句子拆分方法来更改默认行为。例如:

import spacy

text = "The formula is no. 45. This num. represents the chemical properties."

nlp = spacy.load("en_core_web_sm")
doc = nlp(text)
print("Before:", [sent.text for sent in doc.sents])

def set_custom_boundaries(doc):
    pattern_a = ['no', 'num']
    for token in doc[:-1]:
        if token.text in pattern_a and doc[token.i + 1].text == '.':
            doc[token.i + 2].is_sent_start = False
    return doc

nlp.add_pipe(set_custom_boundaries, before="parser")
doc = nlp(text)
print("After:", [sent.text for sent in doc.sents])

这将为您提供所需的句子拆分。

Before: ['The formula is no.', '45.', 'This num.', 'represents the chemical properties.']
After: ['The formula is no. 45.', 'This num. represents the chemical properties.']

关于python spacy句子分割器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64029623/

相关文章:

sql - 仅当列不为 NULL 时才连接列

ruby-on-rails - "transform"一个数组怎么写一句话?

python - 使用 scipy 集成功能?

python-3.x - 100 个训练示例是否足以使用 spacy 训练自定义 NER?

if-statement - 为什么不评估此 Cobol 代码中的第二个 if 语句 (OpenCOBOL)?

python-3.x - 使用表情符号肤色修饰符标记句子或推文

nlp - Spacy 手动下载 en_core_web_lg

python - 在 Mac 10.6 上为 GAE/Django-nonrel 安装 Python 2.5(我是一个新的 Mac 用户)

Python 对象初始化错误。还是我误解了对象的工作原理?

python - 复制的OpenCV图像与原始图像不同