IT 117: Intermediate Scripting
Homework 4


Due Sunday, October 1st 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.

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 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
  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 and fix any errors.
  2. 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.
  3. 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.
  4. 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.
  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 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 inside the else clause, but outside both for loops.
    Make sure you get the indentation right.
    Run the code and fix any errors.
  8. 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.
  9. 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.
  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