繁体   English   中英

将文本分为句子NLTK vs spaCy

[英]separate texts into sentences NLTK vs spaCy

我想将文本分成句子。

在堆栈溢出中寻找,我发现:

与NLTK

from nltk.tokenize import sent_tokenize
text="""Hello Mr. Smith, how are you doing today? The weathe is great, and city is awesome. The sky is pinkish-blue. You shouldn't eat cardboard"""
tokenized_text=sent_tokenize(text)
print(tokenized_text)

宽敞

from spacy.lang.en import English # updated

raw_text = 'Hello, world. Here are two sentences.'
nlp = English()
nlp.add_pipe(nlp.create_pipe('sentencizer')) # updated
doc = nlp(raw_text)
sentences = [sent.string.strip() for sent in doc.sents]

问题是,在后台执行spacy必须使用所谓的create_pipe进行不同的处理。 句子对于训练自己的NLP单词嵌入非常重要。 应该有一个原因,为什么spaCy不直接在框外包括句子标记器。

谢谢。

注意:请注意,简单的.split(。)无效,文本中有多个十进制数字,其他类型的标记中也包含'。'。

默认情况下,spaCy使用其依赖性解析器进行句子分段,这需要加载统计模型。 sentencizer是基于规则的句子分段器,可用于定义自己的句子分段规则,而无需加载模型。

如果您不介意启用解析器,则可以使用以下代码:

import spacy
nlp = spacy.load('en_core_web_sm') # or whatever model you have installed
raw_text = 'Hello, world. Here are two sentences.'
doc = nlp(raw_text)
sentences = [sent.string.strip() for sent in doc.sents]

spaCy的处理管道具有模块化设置,此处提供了更多信息: https : spaCy 您可以通过定义管道来定义所需的零件。 在某些用例中,您可能不需要句子,例如,当您只想用词袋表示时。 因此,我想这可能就是为什么总不会自动包含sentencizer原因-但如果需要的话,它就在那里。

请注意, English()是一个非常通用的模型-您可以在此处找到一些更有用的预训练统计模型: https : //spacy.io/models/en

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM