nltk sentence tokenizer, consider new lines as sentence boundary

前端 未结 1 1862
鱼传尺愫
鱼传尺愫 2021-02-12 15:30

I am using nltk\'s PunkSentenceTokenizer to tokenize a text to a set of sentences. However, the tokenizer doesn\'t seem to consider new paragraph or new lines as a

1条回答
  •  甜味超标
    2021-02-12 16:25

    Well, I had the same problem and what I have done was split the text in '\n'. Something like this:

    # in my case, when it had '\n', I called it a new paragraph, 
    # like a collection of sentences
    paragraphs = [p for p in text.split('\n') if p]
    # and here, sent_tokenize each one of the paragraphs
    for paragraph in paragraphs:
        sentences = tokenizer.tokenize(paragraph)
    

    This is a simplified version of what I had in production, but the general idea is the same. And, sorry about the comments and docstring in portuguese, this was done in 'educational purposes' for brazilian audience

    def paragraphs(self):
        if self._paragraphs is not None:
            for p in  self._paragraphs:
                yield p
        else:
            raw_paras = self.raw_text.split(self.paragraph_delimiter)
            gen = (Paragraph(self, p) for p in raw_paras if p)
            self._paragraphs = []
            for p in gen:
                self._paragraphs.append(p)
                yield p
    

    full code https://gitorious.org/restjor/restjor/source/4d684ea4f18f66b097be1e10cc8814736888dfb4:restjor/decomposition.py#Lundefined

    0 讨论(0)
提交回复
热议问题