For this assignment, you will first figure out your answers in the Python shell. When you feel like you've arrived at the right answers, type them up in a single Python file and share it with the handin email address. For some guidance on how to do this, see the Python homework instructions.
#
.
printConcordance()
is relatively simple and makes our concordance useful. Given a word, a concordance dictionary returned by buildConcordance()
, and the corresponding text, you want to print all occurrences of that word in the text with context. Here are the steps:
584
, then text[564:604]
ought to give you reasonable context surrounding that occurrence of the word. How many characters you want to print before and after the word is your choice (anywhere between 10 and 50 is probably reasonable). Hint: You will have access to the position of each word using a variable (probably your iterator variable in a for-loop), so the endpoints of the range "564:604" in the above example would be computed by adding and subtracting some fixed amount from your variable.Now, we'll test to make sure our print function for the concordance is working properly. The starter code has a defined variable, test_text
, that contains some of Moby Dick. You can use this to test your function.
testPrintConcordance()
. Include the input/output portion of the header string. Provide at least five test cases, and try tricky cases. Since printConcordance()
doesn't actually return anything, there's no way to programmatically test whether each call does the right thing or not. Instead, just make each call and, after each one, print what you would expect to see printed. Remember to try tricky cases; that's the point of a test function.printConcordance('ishmael', conc, test_text)
printConcordance()
. Hint: You have to do some extra work when calculating the start and end indices of your strings. Make sure that no index is smaller than 0 or larger than the size of your text.Run your program again, but this time, read the Moby Dick text file and print some examples from the concordance of this entire text. You have written code that reads in text files like Moby Dick already, so incorporate that. You should add this code at the bottom of your HW2-8.py
file, and assign your Moby Dick to concordance to a variable.
printConcordance()
with the proper arguments (choosing words from Moby Dick, your variable holding the concordance, and the Moby Dick text filename).print
expressions to the bottom of your HW2-8.py
that show good examples of your printConcordance()
function working with Moby Dick.\n
) with white spaces. You can choose to write a for-loop, or call a function that you yourself defined, or call the str.replace()
function after looking up how it works.Rename your program FirstLast_HW2-8.py
and share it with cs0931handinfall2015@gmail.com
.
Note: Before you turn in your Python files, make sure they run properly (save your Python file, then select Run > Run Module
or hit F5
on your keyboard)! If nothing appears in the Shell, don't worry, as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!