큰 텍스트 파일에서 문장을 추출하려면 nltk.sent_tokenize()
을 사용해야합니다. 파일 크기는 1MB에서 400MB까지 다양하므로 메모리 제한 때문에 파일을 완전히로드 할 수 없으며 nltk.sent_tokenize()
을 사용하고 파일을 한 줄씩 읽을 수는 없다고 생각합니다.큰 텍스트 파일에서 nltk를 사용하는 문장 분할
이 작업을 수행 하시겠습니까?
큰 텍스트 파일에서 문장을 추출하려면 nltk.sent_tokenize()
을 사용해야합니다. 파일 크기는 1MB에서 400MB까지 다양하므로 메모리 제한 때문에 파일을 완전히로드 할 수 없으며 nltk.sent_tokenize()
을 사용하고 파일을 한 줄씩 읽을 수는 없다고 생각합니다.큰 텍스트 파일에서 nltk를 사용하는 문장 분할
이 작업을 수행 하시겠습니까?
은 그냥 판독기를 사용하려고 했습니까? nltk
코퍼스 리더는 텍스트를 점진적으로 전달하고 전체 파일이 아닌 배경에서 디스크의 큰 블록을 읽도록 설계되었습니다. 따라서 전체 코퍼스에 PlaintextCorpusReader
을 열면 모든 속임수를 문장없이 문장으로 전달해야합니다. 예를 들면 다음과 같습니다.
reader = nltk.corpus.reader.PlaintextCorpusReader("path/to/corpus", r".*\.txt")
for sent in reader.sents():
if "shenanigans" in sent:
print(" ".join(sent))
파일을 스트리밍하여 한 행씩 파일을 읽는 동안 처리하십시오.
토큰을 저장할 메모리가 문제가되면 프로세스 토큰을 한 줄씩 또는 일괄 적으로 작성하십시오. 라인별로
라인 (1000) 일괄
from __future__ import print_function
from nltk import word_tokenize
with open('input.txt', 'r') as fin, open('output.txt', 'w') as fout:
for line in fin:
tokenized_line = ' '.join(word_tokenize(line.strip()))
print(tokenized_line, end='\n', file=fout)
:
from __future__ import print_function
from nltk import word_tokenize
with open('input.txt', 'r') as fin, open('output.txt', 'w') as fout:
processed_lines = []
for i, line in enumerate(fin):
tokenized_line = ' '.join(word_tokenize(line.strip()))
processed_lines.append(tokenized_line)
if i % 1000 = 0:
print('\n'.join(processed_lines), end='\n', file=fout)
processed_lines = []
파일을 더 작은 청크로 나누어 그 입자에서 처리 할 수 있습니다. –