nltk句子标记器,将新行视为句子边界 [英] nltk sentence tokenizer, consider new lines as sentence boundary

查看:137
本文介绍了nltk句子标记器,将新行视为句子边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nltk的PunkSentenceTokenizer将文本标记为一组句子.但是,分词器似乎并未将新段落或新行视为新句子.

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 new sentence.

>>> from nltk.tokenize.punkt import PunktSentenceTokenizer
>>> tokenizer = PunktSentenceTokenizer()
>>> tokenizer.tokenize('Sentence 1 \n Sentence 2. Sentence 3.')
['Sentence 1 \n Sentence 2.', 'Sentence 3.']
>>> tokenizer.span_tokenize('Sentence 1 \n Sentence 2. Sentence 3.')
[(0, 24), (25, 36)]

我也希望将换行视为句子的边界.无论如何要这样做(我也需要保存偏移量)?

I would like it to to consider new lines as a boundary of sentences as well. Anyway to do this (I need to save the offsets too)?

推荐答案

好,我遇到了同样的问题,我所做的就是将文本拆分为'\ n'.像这样:

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

完整代码 https://gitorious.org/restjor/restjor/source/4d684ea4f18f66b097be1e10cc8814736888 :restjor/decomposition.py#Lundefined

这篇关于nltk句子标记器,将新行视为句子边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆