Question
I've been trying to figure this out for hours, can someone please help? Thank you.. Specification Create a script that does the following Reads in
I've been trying to figure this out for hours, can someone please help? Thank you..
Specification
Create a script that does the following
Reads in a text file
Creates a dictionary of word frequencies
In this dictionary they keys will be words found in the file
The values would be the number of times the word appeared
Prints a sorted list of all words in the file and how many times they appeared in the file
In the frequency dictionary, the keys would be individual words and the values would be the number of times the word appeared in the file. This script must contains two functions.
word_frequencies_create
word_frequency_print
word_frequencies_create
This function must have the following header
def word_frequencies_create(filename):
This function should read in a text file and return a word frequency dictionary. A word frequency dictionary is one where the keys are words and the values are the number of times the word appears in the file. This function must ignore case, so "We" and "we" should count as the same word. The following pseudocode shows the algorithm
open the file for reading create an empty dictionary for each line in the file make the line lowercase turn the line into a list for each word in the list if the word is not in the list create an entry in the dictionary with a value of 1 else add 1 to the value of the word entry in the dictionary return the dictionary
word_frequency_print
This function has the following header
def word_frequency_print(frequencies):
This function must print the words in alphabetical order along with the number of times each word appears in the file.
Test Code
The script must contain the following test code at the bottom of the file
word_freq = word_frequencies_create('xxxxxxx') word_freq = word_frequencies_create('gettysburg.txt') word_frequency_print(word_freq)
Suggestions
Write this script in stages, testing your script at each step
Create a file with the hashbang line, the test code and each function header. The body of the function should be just the the Python statement pass. Pass does nothing but it will keep the code from giving syntax errors. Run the code and fix any errors.
Remove the pass statement in word_frequencies_create. Create a try/exceptstatement. Add a line that opens a file for reading using the filename parameter. Run the code and fix any errors.
Add an else clause to the try/except statement. Create an empty dictionary called word_frequency. Create a for loop that prints each line in the file. Run the code and fix any errors.
Before the print statement in the for loop, Use the lower string method to turn all letters in the line to lowercase. Run the code and fix any errors.
Remove the print statement. Use the split string method to create a list of all the words in the file and assign this list to a variable. Print this list. Run the code and fix any errors.
Remove the previous print statement. Add a for loop that prints each word in the list. Run the code and fix any errors.
Remove the previous print statement. Write an if statement that checks whether the word is NOT already in the dictionary. If the word is not already in the dictionary, create an entry in the dictionary with the word as the key and a value of 1. Add a line to print the dictionary. This line must be inside the else clause, but outside both for loops. Make sure you get the indentation right. Run the code and fix any errors.
Add an else clause to the if statement that adds 1 to the value associated with the word. Run the code and fix any errors.
Remove the print statement from the end of the function. Replace it with a statement that returns the word_frequencies_create dictionary. Remove the pass statement from word_frequency_print code block. Replace it with a statement that prints the parameter frequencies. Run the code and fix any errors.
Replace the print statement with a for loop that prints the word and word count value for each word in the dictionary. Run the code and fix any errors.
Change the for loop so it prints the words in alphabetical order.
Output
When you run the program the output should look something like this
Cannot open file xxxxxxx a 7 above 1 add 1 advanced 1 ago 1 all 1 altogether 1 and 6 any 1 ... war 2 we 10 what 2 whether 1 which 2 who 3 will 1 work 1 world 1 years 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started