파이썬에서 문장으로 단락을 깨는 방법
-
13-11-2019 - |
문제
Python의 단락에서 문장을 파싱해야합니다.이 작업을 수행 할 기존 패키지가 있습니까? 아니면 여기에 정규식을 사용하려고 시도해야합니까?
해결책
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.']
. 다른 팁
여기에 첫 번째 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/파이썬 / 스레드 / 303844
제휴하지 않습니다 StackOverflow