我正在尝试使用 NLTK 和 pandas 创建一个术语文档矩阵。我写了以下函数:
def fnDTM_Corpus(xCorpus):
import pandas as pd
'''to create a Term Document Matrix from a NLTK Corpus'''
fd_list = []
for x in range(0, len(xCorpus.fileids())):
fd_list.append(nltk.FreqDist(xCorpus.words(xCorpus.fileids()[x])))
DTM = pd.DataFrame(fd_list, index = xCorpus.fileids())
DTM.fillna(0,inplace = True)
return DTM.T
运行它
import nltk
from nltk.corpus import PlaintextCorpusReader
corpus_root = 'C:/Data/'
newcorpus = PlaintextCorpusReader(corpus_root, '.*')
x = fnDTM_Corpus(newcorpus)
它适用于语料库中的几个小文件,但是 当我尝试使用包含 4,000 个文件(每个大约 2 kb)的语料库运行它时,它给了我一个MemoryError 。
我错过了什么吗?
我正在使用 32 位 python。(我在 Windows 7、64 位操作系统、Core Quad CPU、8 GB RAM 上)。我真的需要为这种大小的语料库使用 64 位吗?