Python:NLTK 单词列表概率



我一直在计算语料库中的单词列表,并查看单词列表的概率。

我一直在计算每个单词的频率,然后使用 EXCEL 求和,但这需要很长时间。我清单上的字数只有几千个。

我只想总结单词列表的总频率,然后看看单词的概率。

 genres = ['C:/A1.txt','C:/A2.txt','C:/A3.txt']
 modals = ['can', 'could', 'may', 'might', 'must', 'will']
 cfd = nltk.ConditionalFreqDist(
        (genre, word)
        for genre in genres
        for word in modals)
 cfd.tabulate(conditions=genres, samples=modals)

请帮助我。我已经花了两天时间来克服这个问题。

提前非常感谢。!

这是我用来获取频率计数的函数。 它使用 numpy 数组。 您可以修改代码以获取概率。

def freqCount(y_list):
        x =array(y_list)
        y = bincount(x)
        ii = nonzero(y)[0]
        freq = zip(ii,y[ii])
        f = open(fn_freq,'w')
        f.write('Distribution:n')
        print 
        print "Distribution:"
        freq_dict={}
        for ff in freq:
            temp = "%s %sn" % (ff[0],ff[1])
            f.write(temp)
            print ff[0],ff[1]
            freq_dict[ff[0]]=ff[1]
        f.close()
        return freq_dict

最新更新