Jump to content

Shakespeare: Difference between revisions

(47 intermediate revisions by 3 users not shown)
# Download https://csclub.uwaterloo.ca/~ehashman/pwfb/ShakespearePlay.zip
We'll need additional setup here.
# Extract the contents of the folder, as per [https://openhatch.org/wiki/Python_Workshops_for_Beginners/Friday_September_26th_setup_and_tutorial#Goal_.236:_Download_the_Saturday_projects Goal 6] yesterday
# Change directory to inside the folder, for instance:
<pre>cd Downloads/ShakespearePlay</pre>
# Open up the python shell and import some modules:
from RomeoJuliet import RomeoJuliet
from summerNight import SummerNight
from summerChar import SummerChar
* Practice using lists
* Practice using dictionaries.
* Get experience with regular expressions.
==Skills & Exercises==
====<font color="navy">File & Strings Exercise====
=====<font color="navy">Using the word "love"</font>=====
* We will use the play <b> Romeo and Juliet </b>
1. MakeCreate a pythonfile program, call itnamed <code> RomeoJulietRom_Juli.py</code> save it under the same directory as where you've saved the ShakespeareanShakespeare texts.
2. Import RomeoJuliet
2. In your program, open the file "Romeo and Juliet.txt" (The text file need to be saved in the same directory as your python program to be able to use local file name)
3. The variable RomeoJuliet is the play - don't output it, as it's too long. It's in a list.
3. Check to see that the file was opened properly
4. Now create a variable that represents the string "love", for example:
>>> lv_counter = 0;
6. Use a for loop (or while loop, if you like) to read through the lines of the file. While you are reading each line, count the number of lines that contains the word "love"
7. Does Shakespeare use a lot of love in his plays? How about other synonyms of "love"?
* Recall that lists in Python can contain <b>multiple kinds of items (numbers, strings, etc.)</b> and <b>dynamically expand</b> as new things are added.
* We may declare our list as such
>>> lst=["William","Shakespeare"]
>>> lst
['William', 'Shakespeare']
* We may <b>append</b> something to our list as such
>>> lst.append("Bard of Avon")
>>> lst
['William', 'Shakespeare', 'Bard of Avon']
* More on list operations can be fount at the following URL: https://docs.python.org/2/tutorial/datastructures.html
* One common way to process a file is to open it, and save each line as a string into a list
>>> lst = open("importList.txt").readlines()
# readlines.() reads each line in the file and puts each line as a string and saves them all to the list called lst.
>>> lst
['apple\n', 'pear\n', 'oranges\n', 'kiwi\n', 'banana\n', 'monkey']
# notice that there are these weird '\n' characters following each item, they are the newline symbols (when you hit enter to start a new line)
# we can remove them as such:
>>> lstFinal = [eachthing.strip("\n") for eachthing in lst]
>>> lstFinal
['apple', 'pear', 'oranges', 'kiwi', 'banana', 'monkey']
* Dictionaries are another data structure commonly used in programming. Dictionaries store what we call <b> key-value pairs</b> (kind of like dictionaries in real life). One common thing to do with dictionary is determine an entry's value given its key.
* Entries in the dictionary are stored <b> without order </b> (i.e. no way to arrange storage position according to value)
* <b>keys</b> for each entry in the dictionary must be unique, <b> values </b> do not have to be unique.
* For our simplicity, we will use a string as keys.
* Creating a dictionary:
>>> myDict = {"Python":9, "Workshop": 27, "Interesting":1}
>>> myDict
{'Python': 9, 'Interesting': 1, 'Workshop': 27}
# notice that printing them does not necessarily give the entries in the order you entered them.
* The following basic operations work with entries in a dictionary (remove an entry, add an entry, check if a key is in the dictionary, print the value corresponding to a given key):
>>> del myDict["Interesting"]
>>> myDict
{'Python': 9, 'Workshop': 27}
>>> myDict["Interesting"]=1
>>> myDict
{'Python': 9, 'Interesting': 1, 'Workshop': 27}
>>> "Interesting" in myDict
>>> myDict["Interesting"]
For more information on the dictionary data type: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
====<font color="navy">Lists and Dictionaries Exercises====
<b>List & Iteration Exercise 1:</b>
* Create a new python file.
* Import the list of characters from <b> A Midsummer Night's Dream </b> by importing <code>summerChar</code> and import the play <code>summerNight</code>
>>> summerChar.SummerChar
# prints all the characters (such as <HELENA>) that are in the play A Midsummer Night's Dream.
<b>List & Iteration Exercise 2:</b>
In the same file that you created in the previous exercise:
* Count how many times <b> <OBERON> </b>has spoken.
* Print to screen the name <b><OBERON></b> and the number of times he spoke.
<b>Demo: Which of Shakespeare's famous tragic heros talks the most?</b>
<b>List & Iteration Exercise 3*:</b>
* Now iterate through the list of character you saved from exercise 1, see how many times each of them speak.
* Print to screen each character's name and the number of times they spoke.
<b>List & Iteration Exercise 4**:</b>
* Do the same as exercise 3, except now instead of printing them directly, save the <b>names of character</b> and <b>number of times they spoke</b> as a <b>key:value</b> pair in to a dictionary.
* Print the dictionary to screen and check that you have the same result as part 3.
<b>Who speaks the most in Shakespeare's A Midsummer Night's Dream? Can you guess (or if you know) the pairings of the couples in the story?</b></font>
==Extended Exercises==
<b>Exercise 1:</b>
* In modern English, we owe many words to William Shakespeare, who alone invented over 2000 words in the English literature.
* The file "popularWords.txt" lists some popular words that we still use today that were invented by Shakespeare (bet you didn't know that!). Each word originates from at least one of his plays. Can you find the play(s) where each word originated?
* You can save the <b>popular word</b> and <b> the play it comes from </b> as a <b>key:value</b> pair in a dictionary. (remember that value does not have to be a number, it could be arbitrary data structure, i.e. it could be a string or a list of strings)
<b>Exercise 2:</b>
* Give yourself a word that you see in a play. Try to output the entire dialogue spoken by the person that first contained the word.
* i.e. if your word is "fantastical" and you are looking at the play "Twelfth Night", you should output:
<DUKE> <0%>
If music be the food of love, play on;
Give me excess of it, that, surfeiting,
The appetite may sicken, and so die.
That strain again! it had a dying fall:
O! it came o'er my ear like the sweet sound
That breathes upon a bank of violets,
Stealing and giving odour. Enough! no more:
'Tis not so sweet now as it was before.
O spirit of love! how quick and fresh art thou,
That, notwithstanding thy capacity
Receiveth as the sea, nought enters there,
Of what validity and pitch soe'er,
But falls into abatement and low price,
Even in a minute: so full of shapes is fancy,
That it alone is high fantastical.
* Note that each person's speech is opened with their name in angular brackets and ends with </...>, as similar to HTML tags.
<b>Exercise 3:</b>
* Animals are often used as condescending terms to address someone in Shakespeare's plays.
* Using the list of animals given in the file "listAnimal.txt" can you find (in a play or in many plays) which play and who spoke of something as what animal?
* Feel free to devise your own exercises that you would enjoy and find out things about Shakespeare's plays!
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.