【问题标题】:Find the most frequent word pair from a list of messages in python从python中的消息列表中查找最常见的词对
【发布时间】:2020-02-11 00:58:14
【问题描述】:

我有一个包含 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 个单词。但是需要绘制最常见的单词组合。

【问题讨论】:

  • 当然。你试过什么?到目前为止你的代码是什么?
  • 请在您的问题中编辑它。无法在这里阅读。

标签: python nltk


【解决方案1】:

N-gram,参见n-grams in python, four, five, six 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})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多