IT 117: Intermediate Scripting
Homework 4


Due Sunday, February 24th at 11:59 PM

Deliverables

There is one deliverable for this assignment: 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 using the following Unix command
cp  /home/ghoffman/course_files/it117_files/gettysburg.txt  .

Specification

Create a script that does the following 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

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 dictionary
            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
  1. 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.
    You should see nothing.
    If you have errors, fix them.
  2. Remove the pass statement in word_frequencies_create.
    Create a try/except statement.
    Add a line that opens a file for reading using the filename parameter.
    Write code to print an error message if the file cannot be opened.
    Run the code. You should see
    Cannot open xxxxxxx
    If you see something else, fix it.
  3. Add an else clause to the try/except statement.
    Create an empty dictionary called word_frequencies.
    Create a for loop that prints each line in the file.
    Run the code and fix any errors.
  4. You need to convert all words into lowercase.
    To do this insert an assignment statement before the print statement that will turn all the uppercase characters in the line to lowercase using the lower string method.
    Run the code and fix any errors.
  5. 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.
  6. Remove the previous print statement.
    Add a new for loop inside the old for loop that prints each word in the list.
    Run the code and fix any errors.
  7. 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 outside both for loops but still inside the else clause of the try/except statement.
    Make sure you get the indentation right.
    Run the code.
    You should see many words, but all the values should be 1.
    Fix any errors you find.
  8. Add an else clause to the if statement inside the second for loop.
    Inside the else clause add 1 to the count for the current word in the dictionary.
    Run the code.
    You should see words with many different values.
    Fix any errors you find.
  9. Remove the print statement from the end of the function.
    Replace it with a statement that returns the word_frequencies dictionary.
    Remove the pass statement from word_frequency_print code block.
    Replace it with a statement that prints the parameter frequencies.
    Run the code.
    The output should be similar to what you saw at the last step.
    Fix any errors you find.
  10. 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.
  11. 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