在python nltk中计算n-gram频率

发布于 2021-01-29 15:06:45

我有以下代码。我知道我可以使用apply_freq_filter函数过滤掉少于频率计数的搭配。但是,在决定设置过滤频率之前,我不知道如何获取文档中所有n-
gram元组(在我的情况下为bi-gram)的频率。如您所见,我正在使用nltk搭配类。

import nltk
from nltk.collocations import *
line = ""
open_file = open('a_text_file','r')
for val in open_file:
    line += val
tokens = line.split()

bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = BigramCollocationFinder.from_words(tokens)
finder.apply_freq_filter(3)
print finder.nbest(bigram_measures.pmi, 100)
关注者
0
被浏览
77
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    finder.ngram_fd.viewitems()功能有效



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看