How to break up document by sentences with with Spacy

Ulad Kasach picture Ulad Kasach · Sep 19, 2017 · Viewed 9.8k times · Source

How can I break a document (e.g., paragraph, book, etc) into sentences.

For example, "The dog ran. The cat jumped" into ["The dog ran", "The cat jumped"] with spacy?

Answer

npit picture npit · Apr 23, 2019

The up-to-date answer is this:

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]