본문 바로가기
파이썬

파이썬 문장 목록에서 단어 토큰 화 Python

by º기록 2020. 12. 25.
반응형

현재 다음과 같은 목록이 포함 된 파일이 있습니다.

example = ['Mary had a little lamb' , 
           'Jack went up the hill' , 
           'Jill followed suit' ,    
           'i woke up suddenly' ,
           'it was a really bad dream...']

"example"은 이러한 문장의 목록이며 출력이 다음과 같이 표시되기를 원합니다.

mod_example = [ " 'Mary' 'had' 'a' 'little' 'lamb'", 'Jack' 'went' 'up' 'the' 'hill'....] and so on. 토큰 화 된 각 단어로 문장을 분리하여 mod_example 문장의 각 단어 (for 루프 사용시)를 참조 문장과 비교할 수 있어야합니다.

나는 이것을 시도했다 :

for sentence in example:
    text3 = sentence.split()
    print text3 

출력으로 다음이 표시됩니다.

['it', 'was', 'a', 'really', 'bad', 'dream...']

모든 문장에 대해 어떻게 얻나요? it keeps overwriting . and yes , also mention whether my approach is right? 이것은 토큰 화 된 단어가 포함 된 문장 목록으로 남아 있어야합니다. 감사합니다.

 

해결 방법

 


>>> from nltk.tokenize import word_tokenize
>>> example = ['Mary had a little lamb' , 
...            'Jack went up the hill' , 
...            'Jill followed suit' ,    
...            'i woke up suddenly' ,
...            'it was a really bad dream...']
>>> tokenized_sents = [word_tokenize(i) for i in example]
>>> for i in tokenized_sents:
...     print i
... 
['Mary', 'had', 'a', 'little', 'lamb']
['Jack', 'went', 'up', 'the', 'hill']
['Jill', 'followed', 'suit']
['i', 'woke', 'up', 'suddenly']
['it', 'was', 'a', 'really', 'bad', 'dream', '...']

 

참조 페이지 https://stackoverflow.com/questions/21361073

 

 

반응형

댓글