• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Untitled

a guest May 25th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def get_word_frequencies(word_list):
2.     """ Main method to create dictionary to contain unique words and their relative occurrences. """
3.     word_freqs = dict()             # Create empty word frequency histogram.
4.     for word in word_list:          # Loop over each word
5.         if word not in word_freqs:  # Add word as new histogram element if it doesn't already appear in histogram
6.             word_freqs[word] = 1
7.         else:                       # Increment word count if word already appears in histogram
8.             word_freqs[word] += 1
9.     return word_freqs               # Return frequency histogram
10.
11. def sort_words_by_frequency(word_freqs):
12.     """ Main method to convert dictionary into sorted list of unique-word-to-word-frequency associations based on word occurrence. """
13.     word_freq_pairs = list(word_freqs.items())                      # Convert histogram into list of word-frequency pairs
14.     word_freq_pairs.sort(key=lambda items: items[1], reverse=True)  # Sort the list based on decreasing frequency rates
15.     return word_freq_pairs                                          # Return sorted list of word-frequency pairs
16.
17. def grab_top_three_occurring_words(word_freq_pairs):
18.     """ Main method to return top three words by highest relative frequencies. """
19.     NUM_OF_WORDS = 3                                # Instantiate top number of words to pull from data
20.     for items in word_freq_pairs[:NUM_OF_WORDS]:    # Loop over top three word-frequency pairs in list
21.         print(items[0])                             # Return word in each item pair to user
22.
23. # Final Print Statement of Top Three Words
24. grab_top_three_occurring_words(sort_words_by_frequency(get_word_frequencies(word_list)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top