如何用Spacy用句子来分解文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用Spacy用句子来分解文档相关的知识,希望对你有一定的参考价值。

如何将文档(例如段落,书籍等)分成句子

例如,"The dog ran. The cat jumped"与spacy一起进入["The dog ran", "The cat jumped"]

答案

来自spacy's github support page

from __future__ import unicode_literals, print_function
from spacy.en import English

raw_text = 'Hello, world. Here are two sentences.'
nlp = English()
doc = nlp(raw_text)
sentences = [sent.string.strip() for sent in doc.sents]
另一答案

最新的答案是这样的:

from __future__ import unicode_literals, print_function
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用句子来分解文档的主要内容,如果未能解决你的问题,请参考以下文章