12

Pythonの段落から文を解析する必要があります。これを行うための既存のパッケージはありますか、それともここで正規表現を使用しようとすべきですか?

4

2 に答える 2

42

nltk.tokenizeモジュールはこのために設計されており、エッジケースを処理します。例えば:

>>> 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.']
于 2012-02-28T00:34:26.907 に答える
0

これが私が最初のn文を取得する方法です:

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

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

于 2012-02-28T00:23:04.223 に答える