Question
Here is the gettysburg.txt file Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and
Here is the gettysburg.txt file
Four score and seven years ago our fathers brought forth on this
continent a new nation conceived in Liberty and dedicated to the
proposition that all men are created equal
Now we are engaged in a great civil war testing whether that nation
or any nation so conceived and so dedicated can long endure We are
met on a great battle field of that war We have come to dedicate a
portion of that field as a final resting place for those who here
gave their lives that that nation might live It is altogether
fitting and proper that we should do this
But in a larger sense we can not dedicate we can not consecrate
we can not hallow this ground The brave men living and dead
who struggled here have consecrated it far above our poor power to
add or detract The world will little note nor long remember what
we say here but it can never forget what they did here It is for
us the living rather to be dedicated here to the unfinished work
which they who fought here have thus far so nobly advanced It is
rather for us to be here dedicated to the great task remaining
before us that from these honored dead we take increased devotion
to that cause for which they gave the last full measure of devotion
that we here highly resolve that these dead shall not have died
in vain that this nation under God shall have a new birth of
freedom and that government of the people by the people for the
people shall not perish from the earth
Note : Use Pyhon please.
There is one deliverable for this assignment:
hw4.py
It must be in an hw4 directory, which you must create inside a hw directory inside you it117 directory. Make sure the script obeys all the rules in the Script Requirements page. To test this script you must copy into your hw4 directory the file gettysburg.txt from /home/ghoffman/course_files/it117_files.
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.
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. The body of the function should be the Python statement pass which does nothing but it stops syntax errors.
Remove the pass statement in word_frequencies_create and replace it with code that opens a file for reading. If the file cannot be opened for reading, the code should print an error message and not do any further processing of the file.
Create an empty dictionary called word_frequency. After this statement write a for loop that prints each line in the file.
Use the lower string method to change all capital letters in the string to lower case.
Use the split string method to create a list of all the words in the file. Print this list.
Remove the previous print statement. In its place write a for loop that prints each word in the list.
Write an if statement that checks whether the word is already in the dictionary. If not add this word to the dictionary with the value 1. Outside both for loops, print the dictionary.
Add an else clause to the if statement that adds 1 to the value associated with the word.
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 and replace it with a statement that prints the parameter frequencies.
Replace the print statement with a for loop that prints the word and word count value for each word in the dictionary.
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