python assighment 12 not whole question
For this assignment, you will write a series of methods which are designed to help you analyze how often different words appear in text. You will be provided texts as lists of words, and you will use dictionaries to help with your analysis.
Methods to write
Method
Description
get_word_counts(word_list)
This method will take a list parameter and return a dictionary which has as its keys the words from the list, and has as its values the number of times that word appears in the list. So, for example, if you have the following list:
words = ["to", "be", "or", "not", "to", "be"]
then get_word_counts(words) should return this dictionary:
{'to': 2, 'be': 2, 'or': 1, 'not': 1}
number_of_appearances(word_counts, word)
This method takes a dictionary of word counts (like the one returned from your get_word_counts method) as a parameter and a single word. It returns the number of times that word appears in the text (i.e. the value if it exists in the dictionary, or 0 otherwise).
total_words(word_counts)
This method takes a dictionary of word counts (like the one returned from your get_word_counts method) as a parameter. It returns the total number of words from the text (i.e. the sum of all the values).
most_common(word_counts)
This method takes a dictionary of word counts (like the one returned from your get_word_counts method) as a parameter. It returns the word which appears the most frequently (i.e. the key with the highest value in the dictionary). Don't worry if there is more than one most common word; any correct answer should pass the grader, as it will be tested only on word count dictionaries with a single most common word.
single_words(word_counts)
This method takes a dictionary of word counts (like the one returned from your get_word_counts method) as a parameter. It returns a list containing every word which appears exactly once (i.e. every key which has a value of 1).
get_combined_counts(word_counts_a, word_counts_b)
This method takes two dictionaries of word counts (like the one returned from your get_word_counts method) as parameters. It returns a single dictionary which combines the totals from both.
The starter code already contains the following lines of code to test your methods on some words from two classic Beatles songs.
words = ['oh', 'i', 'need', 'your', 'love', 'babe', 'guess', 'you', 'know', 'its', 'true', 'hope', 'you', 'need', 'my', 'love', 'babe', 'just', 'like', 'i', 'need', 'you', 'hold', 'me', 'love', 'me', 'hold', 'me', 'love', 'me', 'i', 'aint', 'got', 'nothing', 'but', 'love', 'babe', 'eight', 'days', 'a', 'week', 'love', 'you', 'every', 'day', 'girl', 'always', 'on', 'my', 'mind', 'one', 'thing', 'i', 'can', 'say', 'girl', 'love', 'you', 'all', 'the', 'time', 'hold', 'me', 'love', 'me', 'hold', 'me', 'love', 'me', 'i', 'aint', 'got', 'nothing', 'but', 'love', 'girl', 'eight', 'days', 'a', 'week', 'eight', 'days', 'a', 'week', 'i', 'love', 'you', 'eight', 'days', 'a', 'week', 'is', 'not', 'enough', 'to', 'show', 'i', 'care']
counts = get_word_counts(words)
print("WORD COUNTS")
for word in sorted(counts):
print(word, counts[word])
print()
print("Appearances of 'need':", number_of_appearances(counts, "need"))
print("Appearances of 'want':", number_of_appearances(counts, "want"))
print()
print("Total words:", total_words(counts))
print("Most common word:", most_common(counts))
print()
print("SINGLE WORDS")
for word in sorted(single_words(counts)):
print(word)
print()
other_words = ['love', 'love', 'me', 'do', 'you', 'know', 'i', 'love', 'you', 'ill', 'always', 'be', 'true', 'so', 'please', 'love', 'me', 'do', 'whoa', 'love', 'me', 'do', 'love', 'love', 'me', 'do', 'you', 'know', 'i', 'love', 'you', 'ill', 'always', 'be', 'true', 'so', 'please', 'love', 'me', 'do', 'whoa', 'love', 'me', 'do', 'someone', 'to', 'love', 'somebody', 'new', 'someone', 'to', 'love', 'someone', 'like', 'you']
other_counts = get_word_counts(other_words)
print("OTHER WORD COUNTS")
for word in sorted(other_counts):
print(word, other_counts[word])
print()
combined_counts = get_combined_counts(counts, other_counts)
print("COMBINED WORD COUNTS")
for word in sorted(combined_counts):
print(word, combined_counts[word])
You should uncomment lines to test your methods as you go along. Leave these lines uncommented when you submit your code, as the grader will use them for one of the tests.
If you want to transform your own text to a list so you can analyze it using your methods, here is some simple code you can use to change a passage into a list. Be warned - it may not work if your text contains quotation marks or line breaks.