arthuraal
arthuraal

Reputation: 75

How to solve the problem of importing when trying to import 'SentenceSegmenter' from 'spacy.pipeline' package?

ImportError: cannot import name 'SentenceSegmenter' from 'spacy.pipeline' 

Spacy version: 3.2.1

I know this class is for an earlier version of spacy, but would it have something similar for this version of spacy?

Upvotes: 3

Views: 1453

Answers (1)

There are several methods to perform sentence segmentation in spacy. You can read about these in the docs here: https://spacy.io/usage/linguistic-features#sbd.

This example is copied as-is from the docs, showing how to segment sentences based on an English language model.

import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("This is a sentence. This is another sentence.")
assert doc.has_annotation("SENT_START")
for sent in doc.sents:
    print(sent.text)

You can also use the rule-based one to perform punctuation-split based on language only, like so (also from the docs):

import spacy
from spacy.lang.en import English

nlp = English()  # just the language with no pipeline
nlp.add_pipe("sentencizer")
doc = nlp("This is a sentence. This is another sentence.")
for sent in doc.sents:
    print(sent.text)

This should work for spacy 3.0.5 and above.

Upvotes: 1

Related Questions