Docsity

Programming Assignment 1 - Computational Linguistics | LING 581, Assignments of Linguistics

Toggle sidebar
bg1
CS/Ling 581: Intro to Computational Linguistics
Spring 2008
Assignment #1
Due February 15
Get fsa1.py from the class website and try it out:
>>> from fsa1 import *
>>> fsa1.recognize(’baa!’)
True
>>> fsa2.recognize(’baa!’)
True
>>> fsa2.recognize(’ba!’)
False
Now make a copy of fsa1.py (called, say, fsa2.py) which you will modify and extend in the rest
of the homework. On the due date, please turn in a hard copy of your new FSA module, along
with a brief write-up which explains what you did and shows examples of how your program
works.
Exercise 1.
The current implementation of recognize for NFSAs performs a depth-first traversal of the
search space. Modify this method so that it can perform either a depth-first or a breadth-first
search. Add a new keyword argument to NFSA.recognize to control the search order. The re-
sults should look something like:
>>> from fsa2 import *
>>> fsa2.recognize(’baa!’,depthFirst=True,trace=True)
Obs 0 at state 0
Obs 1 at state 1
Obs 2 at state 2
Obs 3 at state 3
Obs 4 at state 4
ACCEPT
True
>>> fsa2.recognize(’baa!’,depthFirst=False,trace=True)
Obs 0 at state 0
Obs 1 at state 1
Obs 2 at state 2
Obs 3 at state 2
Obs 3 at state 3
pf2

Partial preview of the text

Download Programming Assignment 1 - Computational Linguistics | LING 581 and more Assignments Linguistics in PDF only on Docsity!

CS/Ling 581: Intro to Computational Linguistics

Spring 2008 Assignment # Due February 15

Get fsa1.py from the class website and try it out:

from fsa1 import * fsa1.recognize(’baa!’) True fsa2.recognize(’baa!’) True fsa2.recognize(’ba!’) False

Now make a copy of fsa1.py (called, say, fsa2.py) which you will modify and extend in the rest of the homework. On the due date, please turn in a hard copy of your new FSA module, along with a brief write-up which explains what you did and shows examples of how your program works.

Exercise 1.

The current implementation of recognize for NFSAs performs a depth-first traversal of the search space. Modify this method so that it can perform either a depth-first or a breadth-first search. Add a new keyword argument to NFSA.recognize to control the search order. The re- sults should look something like:

from fsa2 import * fsa2.recognize(’baa!’,depthFirst=True,trace=True) Obs 0 at state 0 Obs 1 at state 1 Obs 2 at state 2 Obs 3 at state 3 Obs 4 at state 4 ACCEPT True fsa2.recognize(’baa!’,depthFirst=False,trace=True) Obs 0 at state 0 Obs 1 at state 1 Obs 2 at state 2 Obs 3 at state 2 Obs 3 at state 3

Obs 4 at state 4 ACCEPT True

Exercise 2.

Add a complement method to the DFSA class. If L is a regular language, then the complement of L is the set of all strings in Σ∗^ that are not in L: L = {s ∈ Σ∗|s 6 ∈ L}

The languages L and L have the same alphabet Σ, but no strings in common. You should get results like:

from fsa2 import * fsa1.recognize(’baaa!’) True fsa1.recognize(’baa’) False fsa1.recognize(’bxaa!’) False C = fsa1.complement() C.recognize(’baaa!’) False C.recognize(’baa’) True C.recognize(’bxaa!’) False

Here’s a hint: as a first step, create a complete DFSA by adding a sink state, and then take the complement of that.

Exercise 3.

Try the following NFSA:

x = NFSA([{’a’ : [0,1], ’’ : [2]}, {’b’ : [1,2], ’’ : [0]}, {’’ : [1]}], [2]) x.recognize(’0011’,trace=True)

What happens? Why? What property of this NFSA causes the problem? Modify the constructor method for NFSA so that it detects FSAs with this property and raises a ValueError exception. (This problem is challenging, but do the best you can!)