September 18, 2009 · homework

This homework involves some more practice with regular expressions, as well as using lists with python, and making some word frequency measurements with the NLTK. It covers material up to Sep. 17th.

Regular expressions

  1. Use grep to print out all words from the celex.txt file which have a
    frequency of 100 or more, and either start with a-l, or end with m-z. Note that
    the orthography column comes right before the frequency column, so this is
    possible to do using a single regular expression. You should use one grouping
    and several character classes. Hint 1: use negative character classes to avoid
    getting stuff that is in more than one column. Hint 2: Consider what is
    different between the numbers 87 and 99, vs. the numbers 100 and 154.

    Here are some example words which should be included

    • at (starts with a-l)
    • yellow (ends with m-z)

    Here are some example words which should be excluded

    • omega (does not start with a-l, does not end with m-z)
    • abacus (starts with a-l, but has a frequency less than 100

    (10 points)


Create a python script called, and put all your answers in this file.
You can use comments in the file if you like. Before each answer, print out the
question number, and a brief description. For example:

print('#2 - first 10 words in the holy grail')

  1. Print the first 10 words from monty python and the holy grail (text6). (3 points)
  2. Print the last 20 words from Moby Dick (text1). (4 points)
  3. Create a frequency distribution of the holy grail. Store it in the variable
    called moby_dist. (4 points)

  4. Print the number of times the word “Grail” occurs in this text (4 points)
  5. Print the most frequent word in the Holy Grail. (Hint: note that punctuation is counted as words by the NLTK, so it is possible that the most frequent word might actually be a punctuation mark). (4 points)
  6. Create a list which contains the word lengths of each word in the Holy
    Grail(text6). Store it in a variable called holy_lengths. Do the same for Moby
    Dick (text1), and store it in a variable called moby_lengths. (6 points)

  7. Create a frequency distribution of word lengths for Moby Dick and The Holy Grail. Store the distributions in variables called moby_len_dist and holy_len_dist respectively. (6 points)
  8. Print the most commonly occuring word length for Moby Dick and for The Holy Grail. (Use one command for each) (5 points)
  9. Calculate the mean word length for Moby Dick and The Holy Grail. You can use the sum() function to calculate the total number of characters in the text. For example, sum([22, 24, 3]) returns 49. Store the results in variables holy_mean_len and moby_mean_len respectively. (6 points)
  10. Create a list of words from Moby Dick which have more than 3 letters, and
    less than 7 letters. Store it in a variable called four_to_six. (8 points)

Written by Robert Felty

2 Comments to “Homework 4 – More regular expressions, Python lists and word frequency”

  1. robfelty says:

    Sam pointed out a few vague parts of this homework assignment (and one error). I have corrected those. Thanks Sam!

  2. kelleya says:

    Hi Rob,

    For questions 2 and 3 do you want the first 10 words or tokens?


Leave a Reply

You must be logged in to post a comment.

Subscribe without commenting