1

i have a list of 100 messages. And i am able to find the most frequent words used in the list of messages. But i want to find the pair of words which occur most frequently. For example, key and board are being shown as most frequent words. But i need to find the number of occurrences where 'key board' are used as a pair in NLTK. Here abstracts are the list of sentences and abstract words are list of 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)

Here i am able to plot the individual top 10 words. But need to plot combination of words which are most frequent.

1 Answers1

2

N-grams, see n-grams in python, four, five, six grams?, e.g.

>>> 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})
alvas
  • 115,346
  • 109
  • 446
  • 738