【发布时间】:2020-06-14 19:32:18
【问题描述】:
我有一个语料库,我想获得所有 2-gram 的频率。这是我正在使用的代码:
vec = CountVectorizer(ngram_range=(2,2).fit(corpus)
bag_of_words = vec.transform(corpus)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]
“words_freq”变量包含在语料库中找到的每个 gram 的频率,例如:
print(words_freq)
[('green apple', 10), ('yellow apple',2), ('apple green',5)]
但是,我想知道如何在不考虑 gram 中单词顺序的情况下获得每个 gram 的频率。
例如,“green apple”和“apple green”应该被认为是相同的克并给出结果('green apple',15)。
感谢您的帮助。
【问题讨论】:
标签: python nlp countvectorizer