Document Sample
NLP Powered By Docstoc
					Chapter 2: Finite-State Machines

          Heshaam Faili

       University of Tehran
   Regular Expressions
   FSAs
   Properties of
    Regular Languages

Regular Expressions
   A regular expression (RE) is a formula in a
    specialized language, used to characterize strings.
       A string is a sequence of characters
       REs allow us to search for patterns
   A finite-state machine is a device for
    recognizing/generating regular expressions
   We‟ll use a “Perlish” notation for writing regular
    expressions, based on regular expressions in the Perl
    programming language.
       The concepts are the important thing
       NB: Perlish isn‟t exactly the same as Perl
   We will write REs between slashes: /…/                  3
Regular expression inventory
   Character Literals and Classes
       Characters: /abcd/
       Set: /p[aeiou]p/
       Range: /ab[a-z]d/
   Operators (disjunction, negation)
       Disjunction:
            Set elements: /[Aa]ardvark/
            Sequences of characters: /ant(eater|farm)/
       Negation:
            Single item: /[^a]/ (any character but a)
            Range: [^a-z] (not a lowercase letter)
Regular expression inventory
   Counters
       ?: Optionality (0 or 1 occurrence): /colou?r/
       * (Kleene star): Any number of occurrences: /[0-
       +: At least one occurrence: /[0-9]+/
       {n}: n number of occurrences: /[0-9]{4}/
   Wildcard: matches any single character (.)
       /beg.n/
Regular expression inventory
   Parentheses: used to group items
       /ant(farm)?/: all of farm is optional
   Escaped characters: needed to specify
    characters that have a special meaning:
    *, +, ?, (, ), |, [, ], .:
       Use a backslash: /why\?/
       Period expressed as: _
Regular expression inventory
   Anchors: anchor expressions to various
    parts of the string
       ^ start of line
            do not confuse with [^..] used to express
             negation; anywhere else it‟s a start of line
       $ end of line
       \b non-word character
            word characters are digits, underscores, or
             letters, i.e., [0-9A-Za-z\_]
     Examples of Regular
   /fire/ a sequence of f followed immediately by i, then immediately
    by r, then immediately by e
   /fires?/ matches fire or fires
   /fires\?/ matches fires ?
   /[abcd]/ matches a, b, c, or d
   /[0-9]/ matches any character in the range 0 to 9 (inclusive)
   /[^0-9]/ matches any non-digit character, i.e., any character
    except those in the set 0 thru 9
   /[0-9]+/ matches 0, 1, 11, 12, 367, …
   /[0-9]*/ matches 0, 1, 11, 12, 367, … and matches no string
   /fir./ matches fire, fir9, firm, firp, …
   /fir.*/ matches fir, fire, fir987, firppery, …
   /[fFHhs]ire/ matches fire, Fire, Hire, hire, sire
   /f|Fire/ matches f and Fire

   /fire|ings?/ the sequence fire or the
    sequence ing (the latter optionally followed
    by s)
   Why?
   Because sequences have precedence over
   To override precedence, use parentheses
   /fir(e|ings)/ the sequence fire followed by
    either the sequence e or the sequence ings
Precedence Rules
1) Parentheses have the highest precedence.
2) Then come counters, *, +, ?, {}
3) Then come sequences and anchors
  • so, /good.*/ matches goodies, etc., and not
    (just) goodgood
  • /echo{3}/       the sequence ech followed by
  • /(echo){3}/     the sequence echoechoecho
4) Then comes disjunction
   Use aliases to designate particular recurrent sets of
   \d [0-9]: digit
   \D [^\d]: non-digit
   \w [a-zA-Z0-9\_]: alphanumeric
   \W [^\w]: non-alphanumeric
   \s [~\r\t\n\f]: whitespace character
        \r: space, \t: tab
        \n: newline, \f: formfeed
   \S [^\s]: non-whitespace
Example 1

Example 2

Times on a digital watch (hours and



recognizes watch times, but also other
  sequences. In other words, the pattern
  over generates, covering expressions
  which aren‟t in the target


undergenerates, i.e., does not cover all
  watch times.

Representing sentences
„handling‟ agreement:
/the (student solves|students solve) the problem/

an optional adjective:
/the clever?(student solves|students solve) the

generating an infinite number of sentences
  /the clever?(student solves|students solve) the
  problem (and (the clever?(student solves|students
  solve) the problem)*/

NOTE: here the symbols are words, not characters! Be
  sure to define the symbol type
   Regular Expressions
   FSAs
   Properties of
    Regular Languages

A Simple Finite State Analyzer
(or FSA)
   Example: FSA to recognize strings of the
    form: /[ab]+/
   i.e., L ={a, b, ab, ba, aab, bab, aba, bba, …}

   Transition Table
    initial =0; final = {1}
    0–>a-> 1
    1->b->1                                      18
How an FSA accepts or rejects
a string
   The behavior of an FSA is completely determined by its
    transition table. The assumption is that there is a tape, with the
    input symbols are read off consecutive cells of the tape.
   The machine starts in the start (initial) state, about to read the
    contents of the first cell on the input „tape‟.
   The FSA uses the transition table to decide where to go at each
   A string is rejected in exactly two cases:
        1. a transition on an input symbol takes you nowhere
        2. the state you‟re in after processing the entire input is not an
         accept (final) state
   Otherwise. the string is accepted.

FSA formally
   Finite state automaton defined by the
    following parameters:
       Q: finite set of (N) states: q0, q1, …, qN
       : finite input alphabet
       q0: designated start state
       F: set of final states (subset of Q)
       (q, i): transition function

More Examples of FSA‟s
   Let‟s design FSA‟s to recognize
       the set of zero or more a‟s
       the set of all lowercase alphabetic strings
        ending in a b.
       the set of all strings in [ab]* with exactly
        two a‟s.
       simple NPs, PPs, Ss
       etc.

The set of zero or more a‟s
   L ={, a, aa, aaa, aaaa, …}

   Transition Table
    initial =0; final = {0}
    0–>a-> 0

FSA for set of all lowercase
alphabetic strings ending in b
   /[a-z]*b/
   initial =0; final ={1}
   0->[a, c-z]->0
   0->b->1
   1->b->1
   1->[a, c-z]->0

The set of all strings in [ab]*
with exactly 2 a‟s
   Do this yourself
   It might help to first rewrite a more
    precise regular expression for this

        FSA for simple NPs, PPs, S, …
                                          Another FSA for NPs:
  initial=0; final ={2}                   initial=0; final ={2}
  0->D->1                                 0->N->2
  0->->1                                 0->D->1
• D is an alias for [the, a, an, all,…], N for [dog, cat, robin,…]
• What if we wanted to add adjectives? Or recognize PPs?
• What about one for simple sentences?
    • /(Prep D? A* N+)* (D? N) (Prep D? A* N+)* (V_tns|Aux
    V_ing) (Prep D? A* N+)*/
   • Note: FSA1 concat FSA2 recognizes L(FSA1) concat L(FSA2)

Deterministic and Non-
Deterministic FSA‟s
   An FSA is non-deterministic (NFSA) when, for some state and
    input, there is more than one state it can go to
   Occurs when transition table allows for a transition to two or
    more states from one state on a given input symbol.
        e.g., 1->a->2, 1->a->4
   Whenever epsilon-transitions occur, these can be taken without
    consuming input.
        So, whenever epsilon-transitions occur, the machine could either
         take the epsilon-transition, or consume an input symbol,
         introducing non-determinism.
   Any NFSA can be reduced to a DFSA (deterministic) (at the
    expense of possibly more states).

FAQ: Why Are These Machines
   Finite number of states
   Number of states bounded in advance -- determined by its
    transition table
        Therefore, the machine has a limit to the amount of memory it
    Its behavior at each stage is based on the transition table, and
    depends just on the state it‟s in, and the input. So, the current
    state reflects the history of the processing so far.
   Certain classes of formal languages (and linguistic phenomena)
    which are not regular require additional memory to keep track
    of previous information (beyond current state and input)
        e.g., center-embedding constructions (discussed later)

   Regular Expressions
   FSAs
   Properties of
    Regular Languages

Formal Languages Revisited
   We will view any formal language as a
    set of expressions
   The language will use a finite
    vocabulary  (called an alphabet), and
    a set of expression-combining
   Regular languages are the simplest
    class of formal languages
Formal Languages Revisited
   Note: Kleene closure of a set
    Let L = {a, b}.
    Then L* = the set of a‟s and b‟s
      concatenated zero or more times
       = {, a, b, ab, aab, aaab, aaaab, ba, baa, ….}.

Properties of Regular
   The class of regular languages over  is defined as
    1.  (the empty set) is a regular language.
    2.  a   U  , {a} is a regular language.
       ( = alphabet of symbols)
    3. If L1 and L2 are regular languages, so are:
        a. L1 U L2, the union (or disjunction) of L1 and L2
        b. L1.L2 = {xy | x L1, yL2}, concatenation of L1 and L2
        c. L1*, the Kleene closure of L1 (set formed by concatenating
           members of L1 zero or more times)
   So, if the language L is a regular language, any
    expression in L must be expressible by the three
    operations of concatenation, disjunction, and Kleene
    closure.                                             31
     General Closure Properties of
     Regular Languages
   Concatenation, Union, Kleene Closure
   Intersection: If L1 and L2 are regular
    languages, so are L1  L2.
   Set Difference: If L1 and L2 are regular
    languages, so are L1- L2.
   Reversal: If L1 is a regular language, so is
    L1R, the language formed by reversing all
    the strings in L1
What sorts of expressions
aren‟t regular
   In natural language, examples include
    center-embedding constructions.
    The cat loves Mozart.
    The cat the dog chased loves Mozart.
    The cat the dog the rat bit chased loves Mozart.
    The cat the dog the rat the elephant admired bit
      chased loves Mozart.
    (the noun)n (transitive-verb)n-1 loves Mozart
   These aren‟t regular
       though /A*B*loves Mozart/ is regular           33
Regular Expressions and FSAs
   Regular expressions are equivalent to
   So, any FSA can be constructed by just
    concatenation, union, and Kleene *
   Question: how would you (graphically)
    combine FSA‟s using:
       Concatenation
       Union
       Kleene *                             34
Practice #1
   e-Book: 2.1,2.4,2.8,2.10


Shared By: