Tags: %TAGME{ tpaction="" web="Main" tag="" }% view all tags

Shakespeare Word Lookup

Lab 7




We will be building a search tool to find all the sentences with a matching word in the complete works of William Shakespeare


Enter word to look up, return to end: blisters
This tyrant, whose sole name blisters our tongues,
Which oft the angry Mab with blisters plagues,

Enter word to look up, return to end: boat
Or (being wrecked) I am a worthless boat,
Come, down into the boat.
The sailors sought for safety by our boat,
To hazard all our lives in one small boat!
Fool.      Her boat hath a leak,
My boat sails freely, both with wind and stream.
Richmond in Dorsetshire sent out a boat
Like Perseus' horse. Where's then the saucy boat,
Hung on our driving boat, I saw your brother,
the boat with my sighs.

Enter word to look up, return to end: 

Let's consider how we might do this:

Consider how indexes work in books:


How can we do this is a program. What if we could make a dictionary like we did with the word couns for Emily Dickenson, BUT instead store for each word a list of line numbers for each word.

For example, consider the short poem:

A stranger came to the door at eve,
     And he spoke the bridegroom fair.
He bore a green-white stick in his hand,
     And, for all burden, care.
He asked with the eyes more than the lips
     For a shelter for the night,
And he turned and looked at the road afar 
     Without a window light.

Consider the following dictionary where the key is a word, and the value is a list of line numbers where that word appears.

{'a': [0, 2, 5, 7]
     'stranger': [0]
     'came': [0]
     'to': [0]
     'the': [0, 1, 4, 4, 5, 6]
     'door': [0]
     'at': [0, 6]
     'eve': [0]
     'and': [1, 3, 6, 6]
     'he': [1, 2, 4, 6]
     'spoke': [1]
     'bridegroom': [1]
     'fair': [1]
     'bore': [2]
     'greenwhite': [2]
     'stick': [2]
     'in': [2]
     'his': [2]
      hand': [2]
     'for': [3, 5, 5]
     'all': [3]
     'burden': [3]
     'care': [3]
     'asked': [4]
     'with': [4]
     'eyes': [4]
     'more': [4]
     'than': [4]
     'lips': [4]
     'shelter': [5]
     'night': [5]
     'turned': [6]
     'looked': [6]
     'road': [6]
     'afar': [6]
     'without': [7]
     'window': [7]
     'light': [7]}

If we could create such a dictionary, how would we print the matching lines? The answer is to build another list that is merely a list of the lines of the poem, i.e.:

poem=['A stranger came to the door at eve,',
 'And he spoke the bridegroom fair.', 
 'He bore a green-white stick in his hand,', 
 'And, for all burden, care.',
 'He asked with the eyes more than the lips',
 'For a shelter for the night,',
 'And he turned and looked at the road afar',
 'Without a window light.']

Now if we:


we get:

He asked with the eyes more than the lips

Step 1 - Load the poem into a list

To keep things simple for testing we will use the following file: Frost.txt

Create a program to read Frost.txt into a file:

Save this code for step 2

Step 2 - Create the index

Next you will create a function to take the loaded poem from step one, and build an index. The program should iterate through each line in the poem list, remove the special characters (function provided), and then iterate through each word in the line. For each word, check to see if it is in the dictionary. If it isn't add a new dictionary entry for this word with the value being a list with a single entry, the line number of the line we are on.

Then print out the dictionary so the repl.it can check your answer.

You will use this code in the next repl.it

Step 3 - Write a function to ask for a word, and print out the matching lines

Now you will as the write a function to ask for a word, and print out the matching lines. Use the code from the previous repl.it

Step 4 - Make the complete program, and make it work with Shakespeare.

Here is a repl.it with the Shakespeare file. Make it all work like the example above: https://repl.it/@JimSkon/ShakespeareStart


Meets all requirements 70
Code is broken up into functions, with one specific task per function 10
Each function is commented at top of function, after the function name 10
Program has meaningful comments, good variable names, and good formatting 10
Topic revision: r1 - 2020-01-23 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback