![](/img/trans.png)
[英]Given a list of lists of strings, find most frequent pair of strings, second most frequent pair, ....., then most frequent triplet of strings, etc
[英]Find the most frequent word pair from a list of messages in python
我有一个包含 100 条消息的列表。 而且我能够找到消息列表中最常用的词。 但我想找到出现频率最高的词对。 例如,key 和 board 被显示为最常用的词。 但是我需要找到“键盘”在 NLTK 中成对使用的次数。 这里abstracts是句子列表,abstract words是单词列表。
abstracts = [preprocessing(document) for document in abstracts]
abstract_words = " ".join(abstracts)
abstract_words = abstract_words.split()
def plot_word_frequency(words, top_n=10):
word_freq = FreqDist(words)
labels = [element[0] for element in word_freq.most_common(top_n)]
counts = [element[1] for element in word_freq.most_common(top_n)]
plot = sns.barplot(labels, counts)
return plot
plot_word_frequency(abstract_words, 10)
在这里,我可以绘制个人的前 10 个单词。 但需要绘制最常见的单词组合。
N-grams,见python中的n-grams,四、五、六克? ,例如
>>> from collections import Counter
>>> from nltk import ngrams
>>> tokens = "this is a sentence with some of this words this is meh ".split()
>>> Counter(list(ngrams(tokens, 2)))
Counter({('this', 'is'): 2, ('is', 'a'): 1, ('a', 'sentence'): 1, ('sentence', 'with'): 1, ('with', 'some'): 1, ('some', 'of'): 1, ('of', 'this'): 1, ('this', 'words'): 1, ('words', 'this'): 1, ('is', 'meh'): 1})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.