Laboratory Exercise 3

Computational Methods in Linguistics (Bender/Wassink)

Goals:

Due Friday 4/13, 5pm

Playing with the python interpreter

  1. Run python on your local machine (if you have it) or patas

    Open a terminal window (mac or linux) or cygwin window (Windows). Type "python". If you have python installed, it should give you the python prompt. If you don't, it will give an error saying "command not found".

    If you don't have python installed locally, you can look into installing it (post to the discussion board for assistance) or log into patas.

    The default version of python on patas is 2.5, but NLTK appears to need 2.6. So if you are running on patas, invoke python with python2.6 if you want to follow along with the nltk book examples.

  2. Play with the python interpreter. Python gives you >>> as a prompt. You can type expressions for python to evaluate at that prompt. Here are some things to try:

    Note that + functions as a concatenation operator when its arguments are both strings, and as addition when its arguments are both numbers.

  3. Here are some things to try that should give errors. In your write up, describe what is wrong with each of them (based on the error that python gives you):
    1. print 'hello world
    2. 'hello' + 1
    3. '1' + 1
    4. hello + world
  4. The next thing to try is assigning values to variables:

Writing simple python scripts

Now we'll try interacting with python differently: by writing a script in a text file and then sending it to python as an argument. In order to do this, you'll need a text editor that will allow you to create and edit script files. If you want to work direclty on patas, one such editor is emacs, which can invoke by typing emacs at the patas command line. Here is an emacs tutorial.

  1. Create a text file called hello.py
  2. In that file, add the one line:
    print 'hello world'
  3. From the command line, invoke python to interpret that file:
    python hello.py

    It should print the string 'hello world' and then give you back the command line prompt (NB: not the python prompt).

  4. The next script will be slightly more complicated. You should write a python script that has the following properties:

    In order to create that program, the following bits of python will be useful. You can play with them through the python interpreter to get a feel for what they do.

  5. lists in python are delimited with square brackets:
    greetings = ['Hi there!','Aloha!','Ciao!']
    
  6. You can refer to positions on the list with numeric indices (note that they start from 0):
    >>> greetings[0]
    'Hi there'
    >>>
    
  7. To prompt the user for input and store the input in a variable (here called "userinput"), use the following:
    userinput = raw_input("Type a number: ")
    
  8. That will record the user's input as a string, even if they type a number. To convert the string to an inter, use int():
    usernum = int(userinput)

    (Note: We're not worrying for now about making the program robust to unanticipated input, but if we were, we'd want to check that the user actually input a string of digits...)

  9. To find the length of your greetings list, use len:
    len(greetings)
  10. Finally, you'll need some control statements. One way to write this program uses only a while statement. Here's an example (that does something else, just to show you the functionality):
    i = 0
    while i < len(greetings):
       print greetings[i]
       i += 1
    

    Note that whitespace is meaningful in python, and in particular, it's the indentation of lines 3 and 4 that shows that they are part of the body of the while loop.

    Alternatively, you might want to make use of an if-else statement. Here's an example:

    if usernum > 3:
        print "That's too big."
    else:
        print "Okay."
    

    Note again the use of whitespace. Also, the else part is optional. If there is nothing for your program to do if the condition evaluates to false, then you don't need an else clause.

  11. After you have tried these various pieces out on the python prompt, create a script file called multilingualhw.py that has the functionality described in 4 above. Expect to go through several debugging cycles where you try running your script and then change it based on the errors that python reports.

To turn in (via Canvas)