我有以下标记化数据帧示例:
No category problem_definition_stopwords
175 2521 ['coffee', 'maker', 'brewing', 'properly', '2', '420', '420', '420']
211 1438 ['galley', 'work', 'table', 'stuck']
912 2698 ['cloth', 'stuck']
572 2521 ['stuck', 'coffee']
我成功地运行了下面的代码,以获得ngram短语。在
^{pr2}$pmi前10名的结果如下所示:
[('brewing', 'properly'), ('galley', 'work'), ('maker', 'brewing'), ('properly', '2'), ('work', 'table'), ('coffee', 'maker'), ('2', '420'), ('cloth', 'stuck'), ('table', 'stuck'), ('420', '420')]
我希望上面的结果出现在一个包含频率计数的数据帧中,显示这些二元组发生的频率。在
所需输出示例:
ngram frequency
'brewing', 'properly' 1
'galley', 'work' 1
'maker', 'brewing' 1
'properly', '2' 1
... ...
如何在Python中执行上述操作?在
这应该可以。。。在
首先,设置数据集(或类似的数据集):
使用
^{pr2}$nltk.ngrams
重新创建ngrams列表:使用
collections.Counter
计算每个ngram在整个语料库中出现的次数:构建一个看起来像您想要的数据帧:
然后,您可以筛选以仅查看由
finder.nbest
调用生成的ngram:相关问题 更多 >
编程相关推荐