我有50,000,000个文件 - 总共有1.62亿个单词。我想使用类似于本教程here的Gensim进行主题建模
因此,LDA需要一个人将文档标记为单词,然后创建一个单词频率字典。
所以,我把这些文件读入一个pandas数据框('content'列有文本)并执行以下操作来创建一个文本列表.image of dataframe attached here
texts = [[word for word in row[1]['content'].lower().split() if word not in stopwords]
for row in df.iterrows()]
但是,由于字数大,我一直遇到内存错误。
我也在Python中尝试过TokenVectorizer。我也有一个内存错误。
def simple_tokenizer(str_input):
words = re.sub(r"[^A-Za-z0-9\-]", " ", str_input).lower().split()
return words
vectorizer = TfidfVectorizer(
use_idf=True, tokenizer=simple_tokenizer, stop_words='english')
X = vectorizer.fit_transform(df['content'])
如何处理这些真正长文档的标记,以便为LDA分析处理它?
我有一个i7,16GB桌面,如果这很重要。
编辑
由于Python无法存储真正大的列表。我实际上重写了代码,读取每个文件(最初存储为HTML),将其转换为文本,创建文本向量,将其附加到列表,然后将其发送到LDA代码。有效!
因此,LDA需要一个人将文档标记为单词,然后创建一个单词频率字典。
如果您需要的唯一输出是带字数的字典,我会执行以下操作:
在循环中逐个处理文件。这样,您只能在内存中存储一个文件。处理它,然后转到下一个:
# for all files in your directory/directories:
with open(current_file, 'r') as f:
for line in f:
# your logic to update the dictionary with the word count
# here the file is closed and the loop moves to the next one
编辑:当涉及到在内存中保存一个非常大的字典的问题时,你必须记住Python保留了大量内存以保持dict
低密度 - 这是快速查找可能性的代价。因此,您必须搜索另一种存储键值对的方法,例如,元组列表,但查找成本会慢得多。 This question是关于那个,并有一些很好的替代品在那里描述。