【发布时间】:2021-09-23 16:59:01
【问题描述】:
我想使用 spacy(或其他东西)预处理文本数据。 我下面的代码有效,但速度很慢。我只有一个 20 MB 的压缩文本文件作为演示,处理我的代码需要 10 多分钟。 问题是:我需要处理大约 20 GB 压缩文本文件的文本文件,并且之前想加快我的算法。
另外,我将如何处理 20 GB 的压缩文本文件?如果我运行下面的代码,它会破坏我 16GB 的主内存。我可以逐行阅读它并且仍然获得良好的速度吗?
任何帮助将不胜感激。
import zipfile
nlp = spacy.load("en_core_web_sm" , n_process=4)
with zipfile.ZipFile(filename, 'r') as thezip:
text=thezip.open(thezip.filelist[0],mode='r').read()
text=text.decode('utf-8').splitlines()
for doc in nlp.pipe(text, disable=["tok2vec", "parser", "attribute_ruler"], batch_size=2000):
# Do something with the doc here
# First remove punctuation
tokens=[t for t in doc if t.text not in string.punctuation]
# then remove stop words, weird unicode characters, words with digits in them
# and empty characters.
tokens = [ t for t in tokens if not t.is_stop and t.is_ascii and not t.is_digit and len(t) > 1 and not any(char.isdigit() for char in t.text)]
# remove empty lines, make it lower case and put them in sentence form
if len(tokens):
sentence= " ".join(token.text.lower() for token in tokens)
# do something useful with sentence here
【问题讨论】: