CMSC 723 Intro to Computational Linguistics Lecture 2

  • Slides: 36
Download presentation
CMSC 723: Intro to Computational Linguistics Lecture 2: February 4, 2004 Regular Expressions and

CMSC 723: Intro to Computational Linguistics Lecture 2: February 4, 2004 Regular Expressions and Finite State Automata Professor Bonnie J. Dorr Dr. Nizar Habash TAs: Nitin Madnani and Nate Waisbrot

Regular Expressions and Finite State Automata • REs: Language for specifying text strings •

Regular Expressions and Finite State Automata • REs: Language for specifying text strings • Search for document containing a string – Searching for “woodchuck” • How much wood would a woodchuck if a woodchuck would chuck wood? – Searching for “woodchucks” with an optional final “s” • Finite-state automata (FSA) (singular: automaton)

Regular Expressions • Basic regular expression patterns • Perl-based syntax (slightly different from other

Regular Expressions • Basic regular expression patterns • Perl-based syntax (slightly different from other notations for regular expressions) • Disjunctions /[w. W]oodchuck/

Regular Expressions • Ranges [A-Z] • Negations [^Ss]

Regular Expressions • Ranges [A-Z] • Negations [^Ss]

Regular Expressions *+ • Optional characters ? , * and + – ? (0

Regular Expressions *+ • Optional characters ? , * and + – ? (0 or 1) • /colou? r/ color or colour – * (0 or more) • /oo*h!/ oh! or Ooooh! – + (1 or more) • /o+h!/ oh! or Ooooh! • Wild cards. Stephen Cole Kleene – /beg. n/ begin or began or begun

Regular Expressions • Anchors ^ and $ – /^[A-Z]/ “Ramallah, Palestine” – /^[^A-Z]/ “¿verdad?

Regular Expressions • Anchors ^ and $ – /^[A-Z]/ “Ramallah, Palestine” – /^[^A-Z]/ “¿verdad? ” “really? ” – /. $/ “It is over. ” – /. $/ ? • Boundaries b and B – /bonb/ “on my way” “Monday” – /Bonb/ “automaton” • Disjunction | – /yours|mine/ “it is either yours or mine”

Disjunction, Grouping, Precedence • Column 1 Column 2 Column 3 … How do we

Disjunction, Grouping, Precedence • Column 1 Column 2 Column 3 … How do we express this? /Column [0 -9]+ */ /(Column [0 -9]+ *)*/ • Precedence – – Parenthesis Counters Sequences and anchors Disjunction • REs are greedy! () * + ? {} the ^my end$ |

Perl Commands While ($line=<STDIN>){ if ($line =~ /the/){ print “MATCH: $line”; } }

Perl Commands While ($line=<STDIN>){ if ($line =~ /the/){ print “MATCH: $line”; } }

Writing correct expressions Exercise: write a Perl regular expression to match the English article

Writing correct expressions Exercise: write a Perl regular expression to match the English article “the”: /the/ /[t. T]he/ /b[t. T]heb/ /[^a-z. A-Z][t. T]he[^a-z. A-Z]/ /(^|[^a-z. A-Z])[t. T]he[^a-z. A-Z]/

A more complex example Exercise: Write a regular expression that will match “any PC

A more complex example Exercise: Write a regular expression that will match “any PC with more than 500 MHz and 32 Gb of disk space for less than $1000”: /$[0 -9]+/ /$[0 -9]+. [0 -9]/ /b$[0 -9]+(. [0 -9])? b/ /b$[0 -9]? (. [0 -9])? b/ /b[0 -9]+ *([MG]Hz|[Mm]egahertz| [Gg]igahertz)b/ /b[0 -9]+ *(Mb|[Mm]egabytes? )b/ /b[0 -9](. [0 -9]+) *(Gb|[Gg]igabytes? )b/

Advanced operators

Advanced operators

Substitutions and Memory • Substitutions Substitute as many times as possible! s/colour/color/g s/colour/color/i Case

Substitutions and Memory • Substitutions Substitute as many times as possible! s/colour/color/g s/colour/color/i Case insensitive matching • Memory (1, 2, etc. refer back to matches) s/([0 -9]+)/<1>/ /the (. *)er they were, the 1 er they will be/ /the (. *)er they (. *), the 1 er they 2/

Eliza [Weizenbaum, 1966] User: Men are all alike ELIZA: IN WHAT WAY User: They’re

Eliza [Weizenbaum, 1966] User: Men are all alike ELIZA: IN WHAT WAY User: They’re always bugging us about something or other ELIZA: CAN YOU THINK OF A SPECIFIC EXAMPLE? User: Well, my boyfriend made me come here ELIZA: YOUR BOYFRIEND MADE YOU COME HERE User: He says I’m depressed much of the time ELIZA: I AM SORRY TO HEAR THAT YOU ARE DEPRESSED

Eliza-style regular expressions Step 1: replace first person references with second person references s/b.

Eliza-style regular expressions Step 1: replace first person references with second person references s/b. I(’m| am)b/YOU ARE/g s/bmyb/YOUR/g S/bmineb/YOURS/g Step 2: use additional regular expressions to generate replies s/. * YOU ARE (depressed|sad). */I AM SORRY TO HEAR YOU ARE 1/ s/. * YOU ARE (depressed|sad). */WHY DO YOU THINK YOU ARE 1/ s/. * all. */IN WHAT WAY/ s/. * always. */CAN YOU THINK OF A SPECIFIC EXAMPLE/ Step 3: use scores to rank possible transformations

Finite-state Automata • Finite-state automata (FSA) • Regular languages • Regular expressions

Finite-state Automata • Finite-state automata (FSA) • Regular languages • Regular expressions

Finite-state Automata (Machines) baa! baaaa! baaaaa!. . . /baa+!/ a b q 0 a

Finite-state Automata (Machines) baa! baaaa! baaaaa!. . . /baa+!/ a b q 0 a q 1 a q 2 state ! q 3 transition q 4 final state

Input Tape q 0 a b b 0 a ! a 1 a a

Input Tape q 0 a b b 0 a ! a 1 a a 2 REJECT b 3 ! 4

Input Tape q 0 q 1 q 2 q 3 b a a a

Input Tape q 0 q 1 q 2 q 3 b a a a b 0 a 1 q 4 a a 2 ACCEPT ! 3 ! 4

Finite-state Automata • Q: a finite set of N states – Q = {q

Finite-state Automata • Q: a finite set of N states – Q = {q 0, q 1, q 2, q 3, q 4} • : a finite input alphabet of symbols – = {a, b, !} • q 0: the start state • F: the set of final states – F = {q 4} • (q, i): transition function – Given state q and input symbol i, return new state q' – (q 3, !) q 4

State-transition Tables State 0 1 2 3 4: b 1 0 0 Input a

State-transition Tables State 0 1 2 3 4: b 1 0 0 Input a 0 2 3 3 0 ! 0 0 0 4 0

D-RECOGNIZE function D-RECOGNIZE (tape, machine) returns accept or reject index Beginning of tape current-state

D-RECOGNIZE function D-RECOGNIZE (tape, machine) returns accept or reject index Beginning of tape current-state Initial state of machine loop if End of input has been reached then if current-state is an accept state then return accept else return reject elsif transition-table [current-state, tape[index]] is empty then return reject else current-state transition-table [current-state, tape[index]] index + 1 end

Adding a failing state a b q 0 a a q 1 q 2

Adding a failing state a b q 0 a a q 1 q 2 ! ! b q 3 ! q 4 ! b b b a ! q. F a

Adding an “all else” arc a b q 0 a q 1 q 2

Adding an “all else” arc a b q 0 a q 1 q 2 = = a ! q 3 = q. F = q 4

Languages and Automata • Can use FSA as a generator as well as a

Languages and Automata • Can use FSA as a generator as well as a recognizer • Formal language L: defined by machine M that both generates and recognizes all and only the strings of that language. – L(M) = {baa!, baaaa!, …} • Regular languages vs. non-regular languages

Languages and Automata • Deterministic vs. Non-deterministic FSAs • Epsilon ( ) transitions

Languages and Automata • Deterministic vs. Non-deterministic FSAs • Epsilon ( ) transitions

Using NFSAs to accept strings • Backup: add markers at choice points, then possibly

Using NFSAs to accept strings • Backup: add markers at choice points, then possibly revisit unexplored arcs at marked choice point. • Look-ahead: look ahead in input • Parallelism: look at alternatives in parallel

Using NFSAs Input State 0 1 2 3 4 b 1 0 0 a

Using NFSAs Input State 0 1 2 3 4 b 1 0 0 a 0 2 2, 3 0 0 ! 0 0 0 4 0 0 0 0

More about FSAs • Transducers • Equivalence of DFSAs and NFSAs • Recognition as

More about FSAs • Transducers • Equivalence of DFSAs and NFSAs • Recognition as search: depth-first, breadthsearch

Recognition using NFSAs

Recognition using NFSAs

NFSA Recognition of “baaa!”

NFSA Recognition of “baaa!”

Breadth-first Recognition of “baaa!”

Breadth-first Recognition of “baaa!”

Regular languages • Regular languages are characterized by FSAs • For every NFSA, there

Regular languages • Regular languages are characterized by FSAs • For every NFSA, there is an equivalent DFSA. • Regular languages are closed under concatenation, Kleene closure, union.

Concatenation

Concatenation

Kleene Closure

Kleene Closure

Union

Union

Readings for next time • J&M Chapter 3

Readings for next time • J&M Chapter 3