Aelia L.

asked • 11/09/21

Python: Dictionary

Problem 1 – Analyze the Text

Write a function that takes in a filename, which is the name of the text file you will analyze. The file should be in the same folder/directory as your hw6 Python file. Read in the file line by line, creating a dictionary of word frequencies. The dictionary should keep track of the number of occurrence of each word in the file. Uppercase and lower case version of the word should be treated the same word. The function should return the newly created dictionary.

Problem 2 – Look up Frequency

Write a function that prompts the user to enter a word, for example

"Enter the word you are looking for: "

If the word is in the dictionary, return the frequency (value). If the word is not in the dictionary, return 0.

Problem 3 – Most Frequent Words

Write a function that returns a list of the most n most frequently occuring keys in the dictionary. The function should return a list of strings. You don't need to worry about ties. For instance, if there is a three-way tie for the 4th most frequent word and you want the top 5 most occurring words, you can select the first 5, ignoring the one of the values. You can sort the dictionary first using Python's built-in method for sorting. 

If you call the sorted() method, by default it will sort by keys. We can use a lambda function to sort by the values. Since this is something we have not covered yet, I have placed the code to do so below.

newdictionary = sorted(olddictionary.items(), key = lambda kv:kv[1])

Problem 4 – Histogramming the most commonly occurring words

In this problem, you will use a dictionary to create a histogram that stores the frequency of word frequencies. For example, you will determine how many words appear once, twice, three times, etc. Use the values in your input dictionary as keys in your new dictionary. After calculating, print the dictionary so that each key, value pair is on its own line.


1 Expert Answer

By:

Caleb L. answered • 02/12/22

Tutor
5 (1)

Experienced Physics and Math Tutor

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.