簡體   English   中英

從python中的消息列表中找到最常用的詞對

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM