【发布时间】:2016-11-19 21:22:26
【问题描述】:
我正在使用 NLTK 并尝试将特定文档的单词短语计数增加到一定长度以及每个短语的频率。我对字符串进行标记以获取数据列表。
from nltk.util import ngrams
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.collocations import *
data = ["this", "is", "not", "a", "test", "this", "is", "real", "not", "a", "test", "this", "is", "this", "is", "real", "not", "a", "test"]
bigrams = ngrams(data, 2)
bigrams_c = {}
for b in bigrams:
if b not in bigrams_c:
bigrams_c[b] = 1
else:
bigrams_c[b] += 1
上面的代码给出并输出如下:
(('is', 'this'), 1)
(('test', 'this'), 2)
(('a', 'test'), 3)
(('this', 'is'), 4)
(('is', 'not'), 1)
(('real', 'not'), 2)
(('is', 'real'), 2)
(('not', 'a'), 3)
这部分是我正在寻找的。p>
我的问题是,有没有更方便的方法可以说最多 4 或 5 个长度的短语,而无需复制此代码只是为了更改计数变量?
【问题讨论】:
标签: python nltk word-frequency