a guest May 25th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- def get_word_frequencies(word_list):
- """ Main method to create dictionary to contain unique words and their relative occurrences. """
- word_freqs = dict() # Create empty word frequency histogram.
- for word in word_list: # Loop over each word
- if word not in word_freqs: # Add word as new histogram element if it doesn't already appear in histogram
- word_freqs[word] = 1
- else: # Increment word count if word already appears in histogram
- word_freqs[word] += 1
- return word_freqs # Return frequency histogram
- def sort_words_by_frequency(word_freqs):
- """ Main method to convert dictionary into sorted list of unique-word-to-word-frequency associations based on word occurrence. """
- word_freq_pairs = list(word_freqs.items()) # Convert histogram into list of word-frequency pairs
- word_freq_pairs.sort(key=lambda items: items, reverse=True) # Sort the list based on decreasing frequency rates
- return word_freq_pairs # Return sorted list of word-frequency pairs
- def grab_top_three_occurring_words(word_freq_pairs):
- """ Main method to return top three words by highest relative frequencies. """
- NUM_OF_WORDS = 3 # Instantiate top number of words to pull from data
- for items in word_freq_pairs[:NUM_OF_WORDS]: # Loop over top three word-frequency pairs in list
- print(items) # Return word in each item pair to user
- # Final Print Statement of Top Three Words
RAW Paste Data