【问题标题】:How to break up document by sentences with Spacy如何使用 Spacy 按句子分解文档
【发布时间】:2018-02-27 15:23:24
【问题描述】:

如何将文档(例如,段落、书籍等)分解为句子。

例如,"The dog ran. The cat jumped" 变为 ["The dog ran", "The cat jumped"] 并带有空格?

【问题讨论】:

  • 用基本的python:my_string.split(".")
  • @Julien 查看更新后的问题。我的意思不是字面意思“狗跑了。猫跳了”。想想“巴克斯特先生吃了一个泡菜。”

标签: python spacy sentence text-segmentation


【解决方案1】:

最新的答案是这样的:

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]

【讨论】:

  • 如果一个句子中有一个 etc. 它会失败。
  • 这里使用基于规则的方法,而不是统计模型来拆分句子。对于我的用例,使用en_core_web_sm 效果更好,而en_core_web_lg 效果更好,而且速度足够快以满足我的需求。见KB_'s answer
  • 实际上在 spacy 3.0 中,语法现在是 nlp.add_pipe('sentencizer') @user8189050 注释。
  • spacy 3.0.6:将 sent.string.strip() 更改为 sent.text.strip()
【解决方案2】:

回答

import spacy
nlp = spacy.load('en_core_web_sm')

text = 'My first birthday was great. My 2. was even better.'
sentences = [i for i in nlp(text).sents]

其他信息
这假设您已经在系统上安装了模型“en_core_web_sm”。如果没有,您可以通过在终端中运行以下命令轻松安装它:

$ python -m spacy download en_core_web_sm

(有关所有可用型号的概述,请参阅here。)

根据您的数据,这可能比仅使用spacy.lang.en.English 获得更好的结果。一个(非常简单的)比较示例:

import spacy
from spacy.lang.en import English

nlp_simple = English()
nlp_simple.add_pipe(nlp_simple.create_pipe('sentencizer'))

nlp_better = spacy.load('en_core_web_sm')


text = 'My first birthday was great. My 2. was even better.'

for nlp in [nlp_simple, nlp_better]:
    for i in nlp(text).sents:
        print(i)
    print('-' * 20)

输出:

>>> My first birthday was great.
>>> My 2.
>>> was even better.
>>> --------------------
>>> My first birthday was great.
>>> My 2. was even better.
>>> --------------------

【讨论】:

    【解决方案3】:

    来自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]
    

    【讨论】:

    • 这个答案在 2019 年已经过时(SpaCy 2.1.8)。 npit@ 答案更好。
    【解决方案4】:

    在 spacy 3.0.1 中,他们改变了管道。

    from spacy.lang.en import English 
    
    nlp = English()
    nlp.add_pipe('sentencizer')
    
    
    def split_in_sentences(text):
        doc = nlp(text)
        return [str(sent).strip() for sent in doc.sents]
    

    【讨论】:

    • 从 spacy 3.0 开始,这应该是公认的答案
    【解决方案5】:

    更新以反映第一个答案中的 cmets

    from spacy.lang.en import English
    
    raw_text = 'Hello, world. Here are two sentences.'
    nlp = English()
    nlp.add_pipe('sentencizer')
    doc = nlp(raw_text)
    sentences = [sent.text.strip() for sent in doc.sents]
    

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2019-10-18
      • 2021-09-06
      相关资源
      最近更新 更多