How to break up a paragraph by sentences in Python

后端 未结 2 1001
独厮守ぢ
独厮守ぢ 2020-12-09 05:50

I need to parse sentences from a paragraph in Python. Is there an existing package to do this, or should I be trying to use regex here?

相关标签:
2条回答
  • 2020-12-09 06:42

    Here is how I am getting the first n sentences:

    def get_first_n_sentence(text, n):
        endsentence = ".?!"
        sentences = itertools.groupby(text, lambda x: any(x.endswith(punct) for punct in endsentence))
        for number,(truth, sentence) in enumerate(sentences):
            if truth:
                first_n_sentences = previous+''.join(sentence).replace('\n',' ')
            previous = ''.join(sentence)
            if number>=2*n: break #
    
        return first_n_sentences
    

    Reference: http://www.daniweb.com/software-development/python/threads/303844

    0 讨论(0)
  • 2020-12-09 06:46

    The nltk.tokenize module is designed for this and handles edge cases. For example:

    >>> from nltk import tokenize
    >>> p = "Good morning Dr. Adams. The patient is waiting for you in room number 3."
    >>> tokenize.sent_tokenize(p)
    ['Good morning Dr. Adams.', 'The patient is waiting for you in room number 3.']
    
    0 讨论(0)
提交回复
热议问题