반응형
현재 다음과 같은 목록이 포함 된 파일이 있습니다.
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
반응형
'파이썬' 카테고리의 다른 글
파이썬 문자열이 null이거나 비어 있습니다. (0) | 2020.12.25 |
---|---|
파이썬 python select specific elements from a list (0) | 2020.12.25 |
파이썬에서 어떻게 문자열을 분할하고 구분 기호를 유지합니까? (0) | 2020.12.25 |
파이썬 SQLAlchemy에서 오류 처리 (0) | 2020.12.25 |
파이썬 Python에서 변수를 사용하여 클래스의 인스턴스 만들기 (0) | 2020.12.25 |
댓글