reader = nltk.corpus.reader.PlaintextCorpusReader("path/to/corpus", r".*\.txt")
for sent in reader.sents():
if "shenanigans" in sent:
print(" ".join(sent))
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)
你试过用读卡器吗?
nltk
语料库读取器被设计成以增量方式传递文本,从后台的磁盘读取大的块,而不是整个文件。所以只要在你的整个语料库上打开一个PlaintextCorpusReader
,它就应该一句一句地传达你的整个语料库,而不是任何恶作剧。例如:流式处理文件,并在逐行读取文件时进行处理。在
如果存储令牌的内存是个问题,那么逐行或成批地写出进程令牌。在
逐行:
分批(共1000个):
^{pr2}$相关问题 更多 >
编程相关推荐