CS107 Handout 39
Spring 2008 June 2, 2008
Section Handout: Python
Problem 1: Jane Austen’s Favorite Word
Project Gutenberg is an open-source effort intended to legally distribute electronic copies
of thousands of books no longer under copyright. So, if you’re tired of coding in spite of
the strong Internet connection, you might download yourself a little Franz Kafka by
visiting:
http://www.gutenberg.org/files/5200/5200.txt
and e-flip through Metamorphosis to pass the time.
A quick scan of www.gutenberg.org reveals that virtually all of Jane Austen’s
novels—Emma, Lady Susan, Love and Friendship, Northanger Abbey, Persuasion, Price
and Prejudice, and Sense and Sensibility—are available in plain text format. Naturally,
this prompts you to wonder what everyone else is wondering: What’s Jane Austen’s
favorite word?
Given an array of URLs, write a Python function getFavoriteWord that reads in all of
the plain text documents addressed by the URLs, compiles a huge frequency map of all
words she uses anywhere in any of the texts, and returns the most commonly occurring
word. Only count a word if it’s all lowercase letters, and assume the favorite word is the
one with the highest score, where the score of a word is equal to freqlen, where freq is
the word’s frequency and len is the word’s length. For simplicity, assume all
punctuation marks are separated from real words by whitespace.
See the last page of the section handout for a list of all dictionary methods. (You’ll want
to focus on the get and operator[] methods.)
#
# Pulls all of the contents from each of the URLs in the 'urls' list,
# and returns what appears to be the favorite word, where the
# metric for what favorite means is outlined above
#
def getFavoriteWord(urls):