Question
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
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