问题
I'm writing a script for text prediction using NLTK's Conditional Frequency Distribution.
I want to store the distribution in SQL database for later usage using JSON. Is it even possible? If yes, how to dump the ConditionalFrequencyDistribution format using JSON?
Or maybe there is some other nifty way of storing it?
cfd = ConditionalFreqDist()
prev_words = None
cnt=0
for word in words:
if cnt > 1:
prev_words = words[cnt-2]+' '+words[cnt-1]
cfd[prev_words].inc(word)
cnt+=1
回答1:
you could use pickle to store the ConditionalFreqDist() object in a file
f = open('file.pkl', 'w')
pickle.dump(cfd, f)
f.close()
and to get back the object
#load the object
f = open('file.pkl', 'r')
cfd = pickle.load(f)
f.close()
来源:https://stackoverflow.com/questions/9150722/storing-conditional-frequency-distribution-using-nltk