October 11, 2009 · homework

In this homework you will expand upon some of the code you wrote homework 6, using the functions you wrote to calculate mean word and sentence length. However, now you will accept command line arguments and options to use these functions, and print the output in a nice-looking format. Make sure to read all questions before starting the assignment. It is due Oct. 16th and covers material up to Oct. 8th.

  1. From BASH, use svn to copy your hmwk6.py file to hmwk7.py. This will preserve all of the history from hmwk6, so you can see how you have improved your code from homework 6 to homework 7. (3 points)
  2. Create a function called usage, which prints out information about how the script should be used, including what arguments should be specified, and what options are possible. It should take one argument, which is the name of the script file. (7 points)
  3. Write your script to process the following options. Look at opts.py under resources/py for an example. If both -s and -w are specified, it should print out both options. (14 points)
    -w --word print only mean word length
    -s --sent print only mean sentence length
    -h --help print this help information and exit
  4. Instead of specifying which texts to process in your code, change your code so
    that it accepts filenames from the command line. Look at the args.py file
    under resources/py for an example of how to do this. Your code should print out
    the name of each file (you can use the os.path.basename function to print out only the name of the file) specified on the command line, and the mean word length
    and sentence length, with a width of 13 and a precision of 2. Note that it
    should only print word length or sentence length if that option has been
    specified. If no files are specified, it should print the usage information
    and exit. Also note that after reading in a text you will have to first convert
    it to a list of words or sentences using the tokenize functions in the nltk,
    before calculating the mean word length and sentence length with the functions
    you defined in homework 6. See chapter 13 in the notes for examples on how to
    tokenize text to homework 5 for how to do this. The first line of output should
    be a line of headers describing the columns (28 points) Here is some example

    filename        mean_word_len mean_sent_len
    fooey                    3.45         13.47
    bar                      3.15          9.29
  5. Use your script to print out mean word length and sentence length for huckFinn, tomSawyeer, Candide, and devilsDictionary (in resources/texts). Save the output to a file called hmwk7_stats.txt in your personal directory, and commit it to the svn repository. Show the command you use in BASH. Make your paths relative to the root of your working copy of the repository. Do the same command, but also try the -s and -w option, and print to the screen. (8 points)
Written by Robert Felty

Leave a Reply

You must be logged in to post a comment.

Subscribe without commenting