Storing conditional frequency distribution using NLTK

二次信任 提交于 2020-01-01 22:12:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!