我一直在使用python脚本来标记和计算很多.txt
文件的TFIDF,我的脚本如下:
import nltk
import string
import os
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
import nltk
import string
from collections import Counter
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.externals import joblib
import re
import scipy.io
import glob
path = 'R'
token_dict = {}
stemmer = PorterStemmer()
def stem_tokens(tokens, stemmer):
stemmed = []
for item in tokens:
stemmed.append(stemmer.stem(item))
return stemmed
def tokenize(text):
tokens = nltk.word_tokenize(text)
stems = stem_tokens(tokens, stemmer)
return stems
for subdir, dirs, files in os.walk(path):
for file in files:
#if re.match("text\d+.txt",file):
#with open(os.path.join(path,file),'r') as f:
#for shakes in f:
remove_spl_char_regex = re.compile('[%s]' % re.escape(string.punctuation)) # regex to remove special characters
remove_num = re.compile('[\d]+')
file_path = subdir + os.path.sep + file
shakes = open(file_path, encoding="utf8")
text = shakes.read()
lowers = text.lower()
a1 = lowers.translate(string.punctuation)
a2 = remove_spl_char_regex.sub(" ",a1) # Remove special characters
a3 = remove_num.sub("", a2) #Remove numbers
token_dict[file] = a3
tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
tfs = tfidf.fit_transform(token_dict.values())
scipy.io.savemat('arrdata4.mat', mdict={'arr': tfs})
根据文件的大小,我在30分钟后遇到MemoryError 任何人都可以向我解释如何增加python可以访问的内存或我可以解决此问题的任何其他方式? 。
答案 0 :(得分:1)
Python没有超出操作系统强加的内存限制。
ulimit
或同等版本限制进程的内存使用量。top
并查看该进程是否使用了所有可用内存。