Question
Here is gettysburg.txt text: Four score and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated
Here is gettysburg.txt text:
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.
Here is gettysburg_hay.txt:
Four score and seven years ago our fathers brought forth upon 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 here on a great battlefield of that war We have come to
dedicate a portion of it 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 can never forget what they did here It is for us the
living rather to be dedicated here to the unfinished work which
they have thus far so nobly carried on 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
here gave the last full measure of devotion that we here highly
resolve that these dead shall not have died in vain that this
nation shall have a new birth of freedom and that this government
of the people by the people for the people shall not perish from
the earth.
Assignment:
To test this script you must copy into your hw5 directory the files gettysburg.txt from gettysburg_hay.txt /home/ghoffman/course_files/it117_files.
Specification
The purpose of this exercise is to create a script that compares two versions of the Gettysburg address. Create a script that does the following
Reads in text files and creates a set of words in each file
Creates sets of the words found in one file but not the other
Prints the difference sets found
This script must contain three functions.
word_set_create
set_difference
word_set_print
word_set_create
This function must have the following header
def word_set_create(filename)
This function reads in a text file and returns a set of all words found in the file. This function must ignore case, so "We" and "we" should count as the same word.
set_difference
This function has the following header
def set_difference(set_1, set_2):
This function will create a list of all words found in the first set that were not found in the second.
word_set_print
This function has the following header
def word_set_print(word_set):
This function prints the words in a set alphabetical order.
Test Code
The script must contain the following test code at the bottom of the file
word_set = word_set_create('xxxxxxx') word_set_1 = word_set_create('gettysburg.txt') word_set_2 = word_set_create('gettysburg_hay.txt') set_1_set_2_difference = set_difference(word_set_1, word_set_2) set_2_set_1_difference = set_difference(word_set_2, word_set_1) print() print('Words in the first text not found in the second') word_set_print(set_1_set_2_difference) print() print('Words in the second text not found in the first') word_set_print(set_2_set_1_difference)
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_set_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 set called word_set. 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.
Replace the print statement with a statement that adds the word to word_set. After the for loop, print word_set
Remove the print statement from the end of the function. Replace it with a statement that returns word_set.
Remove the pass statement from set_difference and replace it with a statement that return the difference between set_1 and set_2
Replace the the pass statement in word_set_print with a statement that prints the parameter word_set.
Replace the print statement in word_set_print with a for loop that prints each element of the set.
Change the for loop so it prints the words in alphabetical order.
Output
Your output should look something like this:
Cannot open file xxxxxxx Words in the first text not found in the second advanced battle field fought god under Words in the second text not found in the first battlefield carried upon
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