pythonspacysentencetext-segmentation

How to break up document by sentences with Spacy


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?


Solution

  • 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('sentencizer')
    doc = nlp(raw_text)
    sentences = [sent.text.strip() for sent in doc.sents]