Homework 4

Due Monday, October 9

The following homework problems relate to code that has already been written and can be found in /home/rws/HomeworkExercises/HW4 on the ling402 server.

  1. The python program "problem1.py" contains 5 errors. Find them, fix them, run the program, show me the result, and show me your fixed program. Some of the bugs are trivial typos or syntax errors, others more serious errors. All the bugs relate to things that you will find discussions of in the Lutz/Ascher reading so far: you do not need to understand the "sort" call, for example.

    For one of the errors it will help to remember that unlike awk, which allows you to increment a value of a key in an associative array, in python you have to first set that key to some value so that there will be an entry in the associative array. (You will know by now that associative arrays in python are called dictionaries.)

    Pay attention to the comment at the top of the program since it describes how the program is supposed to behave.

  2. "problem2.py" is a simple sentence generator. It makes use of functions and the random module, which we won't get to until later, but you do not need to understand these to do this problem.
    1. Modify the code to add five more nouns, five more adjectives and five more verbs of your choice.
    2. The program currently generates just one sentence. Use a loop to make it generate 10 sentences.
    3. Add a period at the end of the generated sentence.
    4. Pick some language other than English, presumably one you know reasonably well and develop a set of rules and a lexicon of about the complexity of what I have in English. (If the language you pick has complex morphology, just pick very easy sentence structures where you can guarantee that the morphological forms you pick will match the contexts in which you use them.)

      Watch out: if you want a tuple you must indicate it as, e.g., ("foo",) and not ("foo"). The latter will not be interpreted by python as a tuple.

  3. "problem3.py" reads a text file from standard input and prints out a sorted histogram of the words. Modify it (in two separate programs) to:
    1. Count the bigrams in the file.
    2. Count the trigrams in the file.
    In each case, show me your modified code and the the first twenty lines of the result of running it on /home/rws/data/alice.txt.

    (As with problem 1, you do not need to understand how the invocation to sort works to do this problem.)