How to split text into sentences in spaCy

You can split text into sentences in spaCy by using the following code

import spacy

nlp = spacy.load('en_core_web_sm')
text = 'This is a sentence. This is a second sentence.'
doc = nlp(text)
for sent in doc.sents:
    print(sent)

You first need to download and install the en_core_web_sm model to run the above code.

This is the output:

This is a sentence.
This is a second sentence.

Alternatively, you can pass in the text directly into the nlp() call and get the same result.

import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('This is a sentence. This is a second sentence.')
for sent in doc.sents:
    print(sent)

Similar Posts

One Comment

Leave a Reply