STAY WITH US

[Hindi]NLP 26# Sentence Segmentation P.2 |NLP|Python 3|Natural Language Processing|2019

[Hindi]NLP 26# Sentence Segmentation P.2  |NLP|Python 3|Natural Language Processing|2019

Code:

# -*- coding: utf-8 -*-
"""NLP_Ex17.ipynb

Automatically generated by Colaboratory.


"""

import spacy

nlp = spacy.load('en_core_web_sm')


mystring = u"This is a sentence. This is another.\n\nThis is a \nthird sentence."

print(mystring)

doc = nlp(mystring)

for sentence in doc.sents:
  print(sentence)

from spacy.pipeline import SentenceSegmenter

def split_on_newlines(doc):
  start = 0
  seen_newline = False

  for word in doc:
    if seen_newline:
      yield doc[start:word.i]
      start = word.i
      seen_newline = False
    elif word.text.startswith('\n'):
      seen_newline = True
  yield doc[start:]

sbd = SentenceSegmenter(nlp.vocab, strategy=split_on_newlines)

nlp.add_pipe(sbd)

doc = nlp(mystring)

for sentence in doc.sents:
  print(sentence)

YouTube:


Recommended Book:

Post a Comment

0 Comments