24
CS 4705 Regular Expressions and Automata in Natural Language Analysis CS 4705 Julia Hirschberg

Regular Expressions and Automata in Natural Language Analysis

  • Upload
    afra

  • View
    25

  • Download
    0

Embed Size (px)

DESCRIPTION

Regular Expressions and Automata in Natural Language Analysis. CS 4705 Julia Hirschberg. Statistical vs. Symbolic (Knowledge Rich) Techniques. Some simple problems: How much is Google worth? How much is the Empire State Building worth? - PowerPoint PPT Presentation

Citation preview

Page 1: Regular Expressions and Automata in Natural Language Analysis

CS 4705

Regular Expressions and Automata in Natural Language Analysis

CS 4705

Julia Hirschberg

Page 2: Regular Expressions and Automata in Natural Language Analysis

Statistical vs. Symbolic (Knowledge Rich) Techniques

• Some simple problems: – How much is Google worth?

– How much is the Empire State Building worth?

• How much knowledge of language do our algorithms need to do useful NLP?– 80/20 Rule:

• Claim: 80% of NLP can be done with simple methods

• When should we worry about the other 20%?

Page 3: Regular Expressions and Automata in Natural Language Analysis

Today

• Review some simple representations of language and see how far they will take us– Regular Expressions

– Finite State Automata

• Think about the limits of these simple approaches– When do we need something more?

• How much is Absolute Bagels worth?

• How much is a Columbia education worth?

• How much is the Statue of Liberty worth?

• How much is your life worth?

Page 4: Regular Expressions and Automata in Natural Language Analysis

Regular Expression/Pattern Matching in NLP

• Simple but powerful tools for ‘shallow’ processing, e.g. of very large corpora– What word is most likely to begin a sentence?– What word is most likely to begin a question?– How often do people end sentences with prepositions?

• With other simple statistical tools, allow us to– Obtain word frequency and co-occurrence statistics

• What is this document ‘about’?• What words typically modify other words?

– Build simple interactive applications (e.g. Eliza)– Determine authorship: Who wrote Shakespeare’s plays? – Deception detection: Statement Analysis

Page 5: Regular Expressions and Automata in Natural Language Analysis

Review

A non-blank lineAny character/./

Not an NELower case word/ [^A-Z]/

Possible NECapitalized word/ [A-Z]/

Any l.c. letter/[a-z]/

Rhyme:/[bckmrs]ite/

Any char in set/[bckmsr]/

A ‘.’, a ‘?’/\./, /\?/

UsesMatchesRE

Rhyme: /[a-z]ite/

A statement, a question

Page 6: Regular Expressions and Automata in Natural Language Analysis

RE Description Uses?

/a*/ Zero or more a’s /(very[ ])*/

/a+/ One or more a’s /(very[ ])+/

/a?/ Optional single a /(very[ ])?/

/cat|dog/ ‘cat’ or ‘dog’ /[A-Z,a-z]* (cat|dog)/

/^[Nn]o$/ A line with only ‘No’ or ‘no’ in it

/\bun\B/ Prefixes Words prefixed by ‘un’ (nb. union)

Page 7: Regular Expressions and Automata in Natural Language Analysis

Patterns: happier and happier, fuzzier and fuzzier, classifier and classifier

/ (.+ier) and \1 /

Morphological variants of ‘kitty’/kitt(y|ies|en|ens)/

E.G.RE plus

Page 8: Regular Expressions and Automata in Natural Language Analysis

Eliza (Weizenbaum)Men are all alike.

IN WHAT WAY?

They're always bugging us about something or other.

CAN YOU THINK OF A SPECIFIC EXAMPLE?

Well, my boyfriend made me come here.

YOUR BOYFRIEND MADE YOU COME HERE

He says I'm depressed much of the time.

I AM SORRY TO HEAR YOU ARE DEPRESSED

It's true. I am unhappy

DO YOU THINK COMING HERE WILL HELP YOU NOT TO BE UNHAPPY

I need some help, that much seems certain.

WHAT WOULD IT MEAN TO YOU IF YOU GOT SOME HELP

Perhaps I could learn to get along with my mother.

TELL ME MORE ABOUT YOUR FAMILY

My mother takes care of me.

WHO ELSE IN YOUR FAMILY TAKES CARE OF YOU

My father.

YOUR FATHER

You are like my father in some ways.

Page 9: Regular Expressions and Automata in Natural Language Analysis

Using Regular Expressions to Build Eliza

• Step 1: Replace first person with second person references– s/\bI(’m| am)\b/YOU ARE/g

– s/\bmy\b/YOUR/g

– S/\bmine\b/YOURS/g

• Step 2: Use regexps to generate replies– s/.*I AM (depressed|sad) .*/I AM SORRY TO

HEAR YOU ARE \1/

– s/.* I AM (depressed|sad) .*/WHY DO YOU THINK YOU ARE \1/?

Page 10: Regular Expressions and Automata in Natural Language Analysis

– s/.* always .*/CAN YOU THINK OF A SPECIFIC EXAMPLE?/

– s/.*mother*/TELL ME MORE ABOUT YOUR FAMILY./

• Step 3: Can use scores to rank possible transformations

• How far can regular expressions+ let you go?

Page 11: Regular Expressions and Automata in Natural Language Analysis

Regular Languages: Equivalences

Regular Expressions

Finite State Automata Regular Grammars

Page 12: Regular Expressions and Automata in Natural Language Analysis
Page 13: Regular Expressions and Automata in Natural Language Analysis

Formally

• FSA is a 5-tuple consisting of– Q: set of states {q0,q1,q2,q3,q4} : an alphabet of symbols {a,b,!}

– q0: a start state in Q

– F: a set of final states in Q {q4} (q,i): a transition function mapping Q x to Q

q0 q4q1 q2 q3

b a

a

a !

Page 14: Regular Expressions and Automata in Natural Language Analysis

• FSA recognizes (accepts) strings of a regular language– baa!

– baaa!

– baaaa!

– …

• Tape metaphor: will this input be accepted?

b a a a

Page 15: Regular Expressions and Automata in Natural Language Analysis

Another View: A State Transition Table for SheepTalk

State

Input

b a !

0 1 - -

1 - 2 -

2 - 3 -

3 - 3 4

4 - - -

q0 q4q1 q2 q3

b a

a

a !

Page 16: Regular Expressions and Automata in Natural Language Analysis

FSA Recognition

• Goals– Determine whether a string should be accepted by a

machine

– Or… determine whether a string is in the language defined by the automaton

– Or… determine whether a regular expression matches a string

– Turing: process can be represented with a tape

Page 17: Regular Expressions and Automata in Natural Language Analysis
Page 18: Regular Expressions and Automata in Natural Language Analysis
Page 19: Regular Expressions and Automata in Natural Language Analysis

Determinism vs. Non-Determinism

• Deterministic FSA are FSA s.t. at each point in the FSA there is always one unique thing to do (no choices)

• Non-Deterministic FSA contain at least one state with multiple transitions

Page 20: Regular Expressions and Automata in Natural Language Analysis

Non-Deterministic FSAs for SheepTalk

q0 q4q1 q2 q3

b aa

a !

q0 q4q1 q2 q3

b a a !

Page 21: Regular Expressions and Automata in Natural Language Analysis

Problems of Non-Determinism

• At any choice point, we may follow the wrong arc• Potential solutions:

– Save backup states at each choice point

– Look-ahead in the input before making choice

– Pursue alternatives in parallel

– Determinize our NFSAs (and then minimize)

• FSAs can be useful tools for recognizing – and generating – subsets of natural language – But they cannot represent all NL phenomena (e.g.

center embedding: The mouse the cat chased died.)

Page 22: Regular Expressions and Automata in Natural Language Analysis

What can we do with FSAs? E.g. Recognize Proper Names

q2 q4 q5q0 q3q1 q6

the rev mr

dr

hon

pat l. robinson

ms

mrs

Page 23: Regular Expressions and Automata in Natural Language Analysis

Will this FSA Recognize all Proper Names?

• If we want to extract all the proper names in the news, will this work?– What will it miss?

– Will it accept something that is not a proper name?

– How would you change it to accept all proper names without false positives?

– Precision vs. recall….

Page 24: Regular Expressions and Automata in Natural Language Analysis

Summing Up

• Regular expressions and FSAs are good at representing subsets of natural language

• Both may be difficult for humans to understand for any real (large) subset of a language– Can be hard to scale up: e.g., when many choices at any

point (e.g. surnames)

– But quick, powerful and easy to use for small problems

– AT&T Finite State Toolkit does scale

• Next class: – Read Ch 3