Natural Language Processing, Lab assignment 2.

Due Feb 3, 6:15PM.

Working with Regular Expressions in Python

You can use Python on your own computer or in this lab.

Start the interactive Python interpreter and enter the following at the prompt:

 import re
 pat = re.compile('baaa*!!')
 while 1:
	line = raw_input('Are you a sheep? ("q to quit):')
	if line == 'q':
		break
	if pat.match(line):
		print 'Yep. You said " ' +line +'"'
	else:
		print 'Nope. You said " ' +line +'"'

You have defined a pattern using a regular expression and used it to match the input.

  1. Experiment with this code interactively to see what inputs it "accepts"
  2. Change from using match to using search
  3. Begin Working through the Python 201 tutorial on regular expressions by Dave Kuhlman at: http://www.rexx.com/~dkuhlman/python_201/python_201.html While doing so, figure out how to extract the matched text and use that in the "accept" print statement (instead of line).
  4. Capture the output of your session and turn it in at the beginning of the next class.

EXPLORING PYTHON

Once you've carried out the above, it is advisable to switch to a gentler introduction to regular expressions:

Homework:

Do Exercises 2.1, 2.3 (also draw the FSA) using Python. Exercise 2.6 on paper only.