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:
pat = re.compile('baaa*!!')
line = raw_input('Are you a sheep? ("q to quit):')
if line == 'q':
print 'Yep. You said " ' +line +'"'
print 'Nope. You said " ' +line +'"'
You have defined a pattern using a regular expression and used it to match the input.
- Experiment with this code interactively to see what inputs it "accepts"
- Change from using match to using search
- Begin Working through the Python 201 tutorial on regular expressions by Dave Kuhlman at:
While doing so, figure out how to extract the matched text and use that in the "accept" print statement
(instead of line).
- Capture the output of your session and turn it in at the beginning of
the next class.
Once you've carried out the above, it is advisable to switch to a gentler introduction to regular expressions:
Do Exercises 2.1, 2.3 (also draw the FSA) using Python. Exercise 2.6 on paper only.