我有一个巨大的文本数据,我需要创建它的文字云。我正在使用名为Python
的word_cloud
库来创建可配置的词云。问题是我的文本数据非常庞大,因此高端计算机即使长时间也无法完成任务。
数据首先存储在MongoDB
中。由于Cursor
在将数据读入Python list
时出现问题,我已将整个数据导出为纯文本文件 - 只是txt
文件,即304 MB
。
所以我正在寻找答案的问题是如何处理这个巨大的文本数据? word_cloud
库需要一个String
参数,其中包含用' '
分隔的整个数据,以便创建Word Cloud
。
附: Python
版本:3.7.1
附: word_cloud
是Word Cloud
的开源Python
生成器,可在GitHub上找到:https://github.com/amueller/word_cloud
您不需要在内存中加载所有文件。
from wordcloud import WordCloud
from collections import Counter
wc = WordCloud()
counts_all = Counter()
with open('path/to/file.txt', 'r') as f:
for line in f: # Here you can also use the Cursor
counts_line = wc.process_text(line)
counts_all.update(counts_line)
wc.generate_from_frequencies(counts_all)
wc.to_file('/tmp/wc.png')