LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

25
LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    1

Transcript of LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

Page 1: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

LING 388: Language and Computers

Sandiway Fong

Lecture 10: 9/26

Page 2: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

2

Adminstrivia

• Reminders– No class Thursday– Homework 3 due Thursday

• Correction– Homework Exercise 2– Question 4 (1pt)

• Why is the answer different from a simple spokes* search?

– Reword as: • Why could the answer different from a simple spokes*

search?

Page 3: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

3

Last Time

• ... into detail with Microsoft Word’s regular expression facility– Find with wildcard option enabled – somewhat limited form of regular

expression search

Page 4: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

4

Today’s Topic

• Finite State Automata (FSA)

• equivalent to the regular expressions we’ve been studying

Page 5: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

5

Regular Expressions: Example

.... from lecture 8

• example (sheeptalk) – ba!– baa!– baaa! …

• regular expression– baa*!– ba+!

Page 6: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

6

Regular Expressions: Example

.... from lecture 6

• example (sheeptalk) – ba!– baa!– baaa! …

• regular expression– baa*!– ba+!

s x

z

b

!

ya

a

>

Page 7: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

7

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

s>

Page 8: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

8

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

– b

– from s, – see a ‘b’, – move to x

s xb>

Page 9: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

9

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

– ba

– from x, – see an ‘a’, – move to y

s xb ya>

Page 10: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

10

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

– baa*– ba– baa– baaa– baaaa...– from y,– see an ‘a’, – move to ?

s xb ya>

y’

y”

a

a

a...

but machine musthave a finite numberof states!

Page 11: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

11

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

– baa*– ba– baa– baaa– baaaa...– from y,– see an ‘a’, – “loop” or move back to y

s xb ya

a

>

Page 12: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

12

Regular Expressions: Example

• step-by-step• regular expression

– baa*!

– baa*!

– from y,– see an ‘!’, – move to final state z

(indicated in red)

s x

z

b

!

ya

a

>

Note: machine cannot finish (i.e. reach the end of the input string) in states s, x or y

Page 13: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

13

Finite State Automata (FSA)

• construction– the step-by-step FSA construction method we just

used – works for any regular expression

• conclusion– anything we can encode with a regular expression,

we can build a FSA for it

– an important step in showing that FSA and REs are equivalent

Page 14: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

14

Microsoft Word Wildcards

• basic wildcards– ? and *

• ? any single character

• e.g. p?t put, pit, pat, pet

• * zero or more characters

x yd

abc

e

z etc.

...

...

y

a etc.

one loopfor eachcharacter

Page 15: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

15

Microsoft Word Wildcards

• basic wildcards– @

• one or more of the preceding character

• e.g. a@

– [ ]

• range of characters• e.g. [aeiou]

x ya

a

x yo

aei

u

Page 16: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

16

Microsoft Word Wildcards

• basic wildcards– < >

• < • beginning of a word

• can think of there being a special symbol/invisible character marking the beginning of each word

• > • end of a word

• suppose there is an invisible character marking the end of each word

x y<

see anything but ‘<‘

x y>

see anything but ‘>‘

Page 17: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

17

Microsoft Word Wildcards

• basic wildcards– < >

• > • end of a word

– Note• the see-anything-but loop

is implicit• m>• “word that ends in m”• example:

– mom is...

x y>

see anything but ‘>‘

x ym

see anything but ‘m‘

z>

Page 18: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

18

Finite State Automata (FSA)

• more formally– (Q,s,f,Σ,)1. set of states (Q): {s,x,y,z} 4 states must be a finite set2. start state (s): s3. end state(s) (f): z

4. alphabet (Σ): {a, b, !}5. transition function :

signature: character × state → state1. (b,s)=x2. (a,x)=y3. (a,y)=y4. (!,y)=z

s x

z

b

!

ya

a

>

Page 19: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

19

Finite State Automata (FSA)

• in Prolog– define one predicate for each state

• taking one argument (the input string L)• consume input character• call next state with remaining input string

– query•fsa(L) :- s(L).

i.e. call start state s

Page 20: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

20

Finite State Automata (FSA)

• state s: (start state)– s([b|L]) :- x(L).match input string beginning with b and

call state x with remainder of input

• state x:– x([a|L]) :- y(L).

• state y:– y([a|L]) :- y(L).– y(['!'|L]) :- z(L).

• state z: (end state)– z([]).

s x

z

b

!

ya

a

>

Page 21: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

21

Finite State Automata (FSA)

• query– ?- s([b,a,a,’!’]).

s x

z

b

!

ya

a

>

Databases([b|L]) :- x(L).

x([a|L]) :- y(L).

y([a|L]) :- y(L).

y(['!'|L]) :- z(L).

z([]).• query

–?- s([b,a,b,a,’!’]).–in which state does this query fail?

[b,a,a,’!’] [a,a,’!’]

[a,’!’]

[’!’]

[]

[b,a,b,a,’!’] [a,b,a,’!’]

[b,a,’!’]

Page 22: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

22

FSA

• Finite State Automata (FSA) have a limited amount of expressive power

• Let’s look at a modification to FSA and its effect on its power

Page 23: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

23

String Transitions

– so far...

• all machines have had just a single character label on the arc

• so if we allow strings to label arcs– do they endow the FSA with any

more power?

b

• Answer: No– because we can always convert a

machine with string-transitions into one without

abb

a b b

Page 24: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

24

Finite State Automata (FSA)

• equivalent

s

z

ba

!

y

a

>s x

z

b

!

ya

a

>

4 state machine

Page 25: LING 388: Language and Computers Sandiway Fong Lecture 10: 9/26.

25

Finite State Automata (FSA)

• equivalent

s

z

ba

!

y

a

>s x

z

b

!

ya

a

>

Databases([b|L]) :- x(L).

x([a|L]) :- y(L).

y([a|L]) :- y(L).

y(['!'|L]) :- z(L).

z([]).

Databases([b,a|L]) :- y(L).

y([a|L]) :- y(L).

y(['!'|L]) :- z(L).

z([]).