【问题标题】:Storing conditional frequency distribution using NLTK使用 NLTK 存储条件频率分布
【发布时间】:2012-02-05 16:13:09
【问题描述】:

我正在使用 NLTK 的条件频率分布编写文本预测脚本。

我想将分发存储在 SQL 数据库中,以供以后使用 JSON 使用。甚至可能吗?如果是,如何使用 JSON 转储 ConditionalFrequencyDistribution 格式?

或者也许还有其他一些不错的存储方式?

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

【问题讨论】:

    标签: python nltk


    【解决方案1】:

    您可以使用 pickle 将 ConditionalFreqDist() 对象存储在文件中

    f = open('file.pkl', 'w')
    pickle.dump(cfd, f)
    f.close()
    

    并取回对象

    #load the object
    f = open('file.pkl', 'r')
    cfd = pickle.load(f)
    f.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多