Shakespeare: Difference between revisions

From OpenHatch wiki
imported>Zanwenhuahao
imported>Zanwenhuahao
Line 62: Line 62:
   
 
====<font color="navy">File & Strings Exercise====
 
====<font color="navy">File & Strings Exercise====
=====Using the word "love"=====
+
=====<font color="navy">Using the word "love"</font>=====
 
* We will use the play <b> Romeo and Juliet </b>
 
* We will use the play <b> Romeo and Juliet </b>
 
1. Make a python program, call it <code> RomeoJuliet.py</code> save it under the same directory as where you've saved the Shakespearean texts.
 
1. Make a python program, call it <code> RomeoJuliet.py</code> save it under the same directory as where you've saved the Shakespearean texts.

Revision as of 13:06, 27 September 2014

Shakespeare.jpg

How many times does the word 'love' appear in Shakespeare's plays? Is it possible to find negative passages using a list of keywords? We'll use Python to practice our skills and answer questions like these.

Setup

We'll need additional setup here.

Goals

  • Have fun using Python to learn basic data science.
  • Practice searching for information in text documents
  • Practice manipulating strings
  • Practice using loops
  • Practice using lists
  • Practice using dictionaries.
  • Get experience with regular expressions.

Skills & Exercises

Strings

  • Checking if two strings are equal
>>> s = "mama"
>>> s == "mama"
True
>>> s == "papa"
False
  • Checking if a string contains another as a substring
>>> s = "mama"
>>> s in "I love mama"
True
>>> "day" in "Saturday"
True
>>> "Day" in "Saturday"
False
>>> Sat = "Saturday"
>>> "day" in Sat
True

File Operations

  • Open a file
>>> M_S=open("A Midsummer-Night's Dream.txt", "r")
>>> M_S
<open file "A Midsummer-Night's Dream.txt", mode 'r' at 0x102d04660>
  • Read a line
>>> M_S = open("A Midsummer-Night's Dream.txt", "r")
>>> line = M_S.readline()
>>> print line
< Shakespeare -- A MIDSUMMER-NIGHT'S DREAM >
  • Read a file line by line until the end of file (also known as eof )
>>> for eachline in M_S:
>>> # Do something here with each line read
...

File & Strings Exercise

Using the word "love"
  • We will use the play Romeo and Juliet

1. Make a python program, call it RomeoJuliet.py save it under the same directory as where you've saved the Shakespearean texts.

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. Check to see that the file was opened properly

4. Now create a variable that represents the string "love", for example:

>>> lv = "love"

5. Also create a variable that is a counter for the number of time that "love" appears, 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"?

Lists

  • Recall that lists in Python can contain arbitrary objects and dynamically expanded as new things "arrive" to fill up the list
  • We may declare our list as such
>>> lst=["William","Shakespeare"]
>>> lst
['William', 'Shakespeare']
  • We may append 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: [1]
  • One common way to process a file is to open it, and save each line as a string into a list