Ch3-Regular Expressions and Automata

Ch3-Regular Expressions and Automata - Search and Decoding...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Search and Decoding in Speech Recognition Regular Expressions and Automata Outline Introduction Regular Expressions Basic Regular Expression Patterns Disjunction, Grouping and Precedence Examples Advanced Operators Regular Expression Substitution, Memory and ELIZA Using an FSA to Recognize Sheeptalk Formal Languages Example NonDeterministic FSAs Using an NFSA to Accept Strings Recognition as Search Relating Deterministic and NonDeterministic Automata FiniteState Automata Regular Languages and FSAs Summary February 11, 2012 Veton Kpuska 2 Introduction Imagine that you have become a passionate fan of woodchucks. Desiring more information on this celebrated woodland creature, you turn to your favorite Web browser and type in woodchuck. Your browser returns a few sites. You have a flash of inspiration and type in woodchucks. Instead of having to do this search twice, you would have rather typed one search command specifying something like woodchuck with an optional final s. Or perhaps you might want to search for all the prices in some document; you might want to see all strings that look like $199 or $25 or $24.99. In this chapter we introduce the regular expression, the standard notation for characterizing text sequences. The regular expression is used for specifying: text strings in situations like this Websearch example, and in other information retrieval applications, but also plays an important role in wordprocessing, computation of frequencies from corpora, and other such tasks. February 11, 2012 Veton Kpuska 3 Intriduction Regular Expressions can be implemented via finitestate automaton. Finitestate automaton is one of the most significant tools of computational linguistics. Its variations: Finitestate transducers Hidden Markov Models, and Ngram grammars Important components of the Speech Recognition and Synthesis, spellchecking, and informationextraction applications that will be introduced in latter chapters. February 11, 2012 Veton Kpuska 4 Regular Expressions and Automata Regular Expressions Regular Expressions Formally, a regular expression is an algebraic notation for characterizing a set of strings. Thus they can be used to specify search strings as well as to define a language in a formal way. A pattern that we want to search for, and A corpus of text to search through. Regular Expression requires Thus when we give a search pattern, we will assume that the search engine returns the line of the document returned. This is what the UNIX grep command does. We will underline the exact part of the pattern that matches the regular expression. A search can be designed to return all matches to a regular expression or only the first match. We will show only the first match. February 11, 2012 Veton Kpuska 6 Basic Regular Expression Patterns The simplest kind of regular expression is a sequence of simple characters: /woodchuck/ /Buttercup/ /!/ RE /woodchucks/ /a/ /Claire says,/ /song/ /!/ February 11, 2012 Example Patterns Matched "interesting links to woodchucks and lemurs" "Mary Ann stopped by Mona's" "Dagmar, my gift please," Claire says," "all our pretty songs" "You've left the burglar behind again!" said Nori Veton Kpuska 7 Basic Regular Expression Patterns Regular Expressions are case sensitive /woodchucks/ will not match "Woodchucks" Disjunction: "[" and "]". RE /[wW]oodchuck/ /[abc]/ /[1234567890]/ February 11, 2012 /s/ /S/ Match Woodchuck or woodchuck `a', `b', or `c' Any digit Veton Kpuska Example Pattern "Woodchuck" "In uomini, in soldati" "plenty of 7 to 5" 8 Basic Regular Expression Patterns Specifying range in Regular Expressions: "" RE /[AZ]/ /[az]/ /[09]/ Match An uppercase letter A lower case letter A single digit Example Patterns Matched "we should call it `Drenched Blossoms'" "my beans were impatient to be hoed!" "Chapter 1: Down the Rabbit Hole" Veton Kpuska 9 February 11, 2012 Basic Regular Expression Patterns Negative Specification what pattern can not be: "^" If the first symbol after the open square brace "[" is "^" the resulting pattern is negated. Example /[^a]/ matches any single character (including special characters) except a. RE /[^AZ]/ /[^Ss]/ /[^\.]/ /[e^]/ /a^b/ Match (single characters) Not an uppercase letter Neither `S' nor `s' Not a period Either `e' or `^' Pattern `a^b' Veton Kpuska Example Patterns Matched "Oyfn pripetchik" "I have no exquisite reason for 't" "our resident Djinn" "look up ^ now" "look up a^b now" 10 February 11, 2012 Basic Regular Expression Patterns How do we specify both woodchuck and woodchucks? Optional character specification: /?/ /?/ means "the preceding character or nothing". Match woodchuck or woodchucks color or colour Veton Kpuska RE Example Patterns Matched /woodchucks?/ "woodchuck" Colou?r February 11, 2012 "colour" 11 Basic Regular Expression Patterns Questionmark "?" can be though of as "zero or one instances of the previous character". It is a way to specify how many of something that we want. Sometimes we need to specify regular expressions that allow repetitions of things. For example, consider the language of (certain) sheep, which consists of strings that look like the following: baa! baaa? baaaa? baaaaa? baaaaaa? ... February 11, 2012 Veton Kpuska 12 Basic Regular Expression Patterns Any number of repetitions is specified by "*" which means "any string of 0 or more". Examples: /aa*/ a followed by zero or more a's /[ab]*/ zero or more a's or b's. This will match aaaa or abababa or bbbb February 11, 2012 Veton Kpuska 13 Basic Regular Expression Patterns We know enough to specify part of our regular expression for prices: multiple digits. Regular expression for individual digit: Regular expression for an integer: Why is not just /[09]*/? /[09][09]*/ /[09]/ Because it is annoying to specify "at least once" RE since it involves repetition of the same pattern there is a special character that is used for "at least once": "+" Regular expression for an integer becomes then: Regular expression for sheep language: /baa*!/, or /ba+!/ /[09]+/ February 11, 2012 Veton Kpuska 14 Basic Regular Expression Patterns One very important special character is the period: /./, a wildcard expression that matches any single character (except carriage return). Example: Find any line in which a particular word (for example Veton) appears twice: /Veton.*Veton/ RE /beg.n/ Match Any character between beg and n Example Pattern begin beg'n, begun February 11, 2012 Veton Kpuska 15 Repetition Metacharacters RE Description Example /ac*e/ matches "ae", "ace", "acce", "accce" as in "The aerial acceleration alerted the ace pilot" /ac?e/ matches "ae" and "ace" as in "The aerial acceleration alerted the ace pilot" /ac+e/ matches "ace", "acce", "accce" as in "The aerial acceleration alerted the ace pilot" /ac{2}e/ matches "acce" as in "The aerial acceleration alerted the ace pilot" /ac{2,}e/ matches "acce", "accce" etc., as in "The aerial acceleration alerted the ace pilot" /ac{2,4}e/ matches "acce", "accce" and "acccce" , as in "The aerial acceleration alerted the ace pilot" * ? + {n} {n,} {n,m} . .* Matches any number of occurrences of the previous character zero or more Matches at most one occurrence of the previous characters zero or one. Matches one or more occurrences of the previous characters Matches exactly n occurrences of the previous characters. Matches n or more occurrences of the previous characters Matches from n to m occurrences of the previous characters. Matches one occurrence of any /a.e/ matches aae, aAe, abe, aBe, a1e, etc., as in characters of the alphabet except the new ""The aerial acceleration alerted the ace pilot" line character Matches any string of characters and until it encounters a new line character Veton Kpuska 16 February 11, 2012 Anchors Anchors are special characters that anchor regular expressions to particular places in a string. The most common anchors are: "^" matches the start of a line "$" matches the end of the line /^The/ matches the word "The" only at the start of the line. Three uses of "^": 1. 2. 3. /^xyz/ Matches the start of the line [^xyz] Negation /^/ Just to mean a caret Examples: /$/ "" Stands for space "character"; matches a space at the end of line. /^The dog\.$/ matches a line that contains only the phrase "The dog". Veton Kpuska 17 February 11, 2012 Anchors /\b/ matches a word boundary /\B/ matches a nonboundary /\bthe\b/ matches the word "the" but not the word "other". Word is defined as a any sequence of digits, underscores or letters. /\b99/ will match the string 99 in "There are 99 bottles of beer on the wall" but NOT "There are 299 bottles of beer on the wall" and it will match the string "$99" since 99 follows a "$" which is not a digit, underscore, or a letter. February 11, 2012 Veton Kpuska 18 Disjunction, Grouping and Precedence. Suppose we need to search for texts about pets; specifically we may be interested in cats and dogs. If we want to search for either "cat" or the string "dog" we can not use any of the constructs we have introduced so far (why not ""?). New operator that defines disjunction, also called the pipe symbol is "|". /cat|dog/ matches either cat or the string dog. February 11, 2012 Veton Kpuska 19 Grouping In many instances it is necessary to be able to group the sequence of characters to be treated as one set. Example: Search for guppy and guppies. Useful in conjunction to "*" operator. /gupp(y|ies)/ /*/ applies to single character and not to a whole sequence. Example: Match "Column 1 Column 2 Column 3 ..." /Column[09]+*/ will match "Column # ..." /(Column[09]+*)*/ will match "Column 1 Column 2 Column 3 ..." Veton Kpuska February 11, 2012 20 Operator Precedence Hierarchy Operator Class Parenthesis Counters Sequences and anchors Disjunction February 11, 2012 Precedence from Highest to Lowest () * + ? {} ^ $ | Veton Kpuska 21 Simple Example 1. 2. 3. Problem Statement: Want to write RE to find cases of the English article "the". /the/ It will miss "The" /[tT]he/ It will match "amalthea", "Bethesda", "theology", etc. /\b[tT]he\b/ Is the correct RE Problem Statement: If we want to find "the" where it might also have undelines or numbers nearby ("The" , "the_" or "the25") one needs to specify that we want instances in which there are no alphabetic letters on either side of "the": /[^azAZ][tT]he/[^azAZ]/ it will not find "the" if it begins the line. /(^|[^azAZ])[tT]he/[^azAZ]/ 1. 2. February 11, 2012 Veton Kpuska 22 A More Complex Example Problem Statement: Build an application to help a user by a computer on the Web. The user might want "any PC with more than 1000 MHz and 80 Gb of disk space for less than $1000 To solve the problem must be able to match the expressions like 1000 MHz, 1 GHz and 80 Gb as well as $999.99 etc. February 11, 2012 Veton Kpuska 23 Solution Dollar Amounts Complete regular expression for prices of full dollar amounts: Adding fractions of dollars: Problem since this RE only will match "$199.99" and not "$199". To solve this issue must make cents optional and make sure the $ amount is a word: /\b$[09]+(\.[09][09])?\b/ /$[09]+\.[09][09]/ or /$[09]+\.[09] {2}/ /$[09]+/ February 11, 2012 Veton Kpuska 24 Solution: Processor Speech Processor speech in megahertz = MHz or gigahertz = GHz) /\b[09]+*(MHz|[Mm]egahertz|GHz|[Gg]igahertz)\b/ * is used to denote "zero or more spaces". February 11, 2012 Veton Kpuska 25 Solution: Disk Space Dealing with disk space: Gb = gigabytes Memory size: Mb = megabytes or Gb = gigabytes Must allow optional fractions: /\b[09]+*(M[Bb]|[Mm]egabytes?)\b/ /\b[09]+(\.[09]+)?*(G[Bb]|[Gg]igabytes?)\b/ February 11, 2012 Veton Kpuska 26 Solution: Operating Systems and Vendors /\b((Windows)+*(XP|Vista)?)\b/ /\b((Mac|Macintosh|Apple)\b/ February 11, 2012 Veton Kpuska 27 Advanced Operators RE Expansion Match Any digit Any nondigit Any alphanumeric or space A non alphanumeric Whitespace (space, tab) Non whitespace Example Patterns "Party of 5" "Blue moon" Daiyu !!!! " " "in Concord" \d \D \w \W \s \S [09] [^09] [azAZ09] [^\w] [ \r\t\n\f] [^\s] Aliases for common sets of characters February 11, 2012 Veton Kpuska 28 Literal Matching of Special Characters & "\" Characters RE Match Example Patterns "K*A*P*L*A*N" "Dr. Kpuska, I presume" "Would you like to light my candle?" \* \. \? \n \t \r An asterisk "*" A period "." A question mark "?" A newline A tab A carriage return character Some characters that need to be backslashed "\" February 11, 2012 Veton Kpuska 29 Regular Expression Substitution, Memory, and ELIZA Substitutions are an important use of regular expressions. s/regexp1/regexp2/ allows a string characterized by one regular expression (regexp1) to be replaced by a string characterized by a second regular expressions (regexp2). s/colour/color/ It is also important to refer to a particular subpart of the string matching the first pattern. Example: replace s/([09]+)/<\1>/ "\1" refers to the first pattern matched by the first regular expression. "the 35 boxes", to "the <35> boxes" February 11, 2012 Veton Kpuska 30 Regular Expression Substitution, Memory, and ELIZA The parenthesis and number operators can also be used to specify that a certain string or expression must occur twice in the text. Example: "the Xer they were, the Xer they will be" We want to constrain the two X's to be the same string: /[Tt]he (.*)er they were, the \1er they will be/ This RE will match: "The bigger they were, the faster they will be" The number operator can be used with other numbers: if you match two different sets of parenthesis, \2 means whatever matched the second set. Example: /[Tt]he (.*)er they (.*), the \1er they \2/ This Re will match: "The bigger they were, the bigger they were", but not "The bugger the were, the bigger they will be", February 11, 2012 Veton Kpuska 31 Registers Numbered memories are called registers: \1 register 1 \2 register 2 \3 register 3 February 11, 2012 Veton Kpuska 32 ELIZA Substitutions using memory are very useful in implementing simple naturallanguage understanding programs like ELIZA. Here is example of dialog with ELIZA: User1: Men are all alike. ELIZA1: IN WHAT WAY User2: They're always bugging us about something or other. ELIZA2: CAN YOU THINK OF A SPECIFIC EXAMPLE User3: Well, my boyfriend made me come here. ELIZA3: YOUR BOYFRIEND MADE YOU COME HERE User4: He says I'm depressed much of the time. ELIZA4: I AM SORRY TO HEAR YOU ARE DEPRESSED. February 11, 2012 Veton Kpuska 33 ELIZA Eliza worked by having a cascade of regular expression substitutions that each matched some part of the input lines and changed them. The first substitutions changed all instances of: "my" "YOUR" "I'm" "YOU ARE" Next set of substitutions looked for relevant patterns in the input and created an appropriate output; 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/ February 11, 2012 Veton Kpuska 34 ELIZA Since multiple substitutions could apply to a given input, substitutions were assigned a rank and were applied in order. Creation of such patterns is addressed in Exercise 2.2. February 11, 2012 Veton Kpuska 35 Finate State Automata Finate State Automata The regular expression is more than just a convenient metalangue for text searching. 1. A regular expression is one way of describing a finite stateautomaton (FSA). FSA are the theoretical foundation of significant number of computational work described in the class. Any regular expression can be implemented as FSA (except regular expressions that use the memory feature). 1. Regular expression is one way of characterizing a particular kind of formal language called a regular language. Both FSA and RE can be used to describe regular languages. Veton Kpuska 37 February 11, 2012 FSA, RE and Regular Languages Regular expressions Regular Languages Regular languages Finite automata February 11, 2012 Veton Kpuska 38 Finitestate automaton for Regular Expressions Using FSA to Recognize Sheeptalk with RE: /baa+!/ a q0 Start State b q1 a q2 Transitions a q3 ! q4 Final State February 11, 2012 Veton Kpuska 39 FSA Use The FSA can be used for recognizing (we also say accepting) strings in the following way. First, think of the input as being written on a long tape broken up into cells, with one symbol written in each cell of the tape, as figure below: q0 a b a ! b February 11, 2012 Veton Kpuska 40 Recognition Process 1. a. The machine starts in the start state (q0), and iterates the following process: Check the next letter of the input. i. ii. iii. If it matches the symbol on an arc leaving the current state, then cross that arc move to the next state, also advance one symbol in the input b. 2. a. b. c. If the machine never gets to the final state, If we are in the accepting state (q4) when we run out of input, the machine has successfully recognized an instance of sheeptalk. either because it runs out of input, or it gets some input that doesn't match an arc (as in Fig in previous slide), or if it just happens to get stuck in some nonfinal state, we say the machine rejects or fails REJECTS to accept an input. February 11, 2012 Veton Kpuska 41 State Transition Table Input State 0 1 2 3 4: b 1 a 2 3 3 ! 4 We've marked state 4 with a colon to indicate that it's a final state (you can have as many final states as you want), and the indicates an illegal or missing transition. We can read the first row as "if we're in state 0 and we see the input b we must go to state 1. If we're in state 0 and we see the input a or !, we fail". February 11, 2012 Veton Kpuska 42 Formal Definition of Automaton Q={q0,q1,...,qN} A finite set of N states a finite input alphabet of symbols the start state the set of final states, F Q the transition function or transition matrix (q, i) between states. Given a state q Q and an input symbol i , (q, i) returns a new state q Q. is thus a relation from QS to Q; q0 F February 11, 2012 Veton Kpuska 43 FSA Example Q = {q0,q1,q2,q3,q4}, = {a,b, !}, F = {q4}, and (q, i) February 11, 2012 Veton Kpuska 44 Deterministic Algorithm for Recognizing a String function DRECOGNIZE(tape,machine) returns accept or reject indexBeginning of tape currentstateInitial state of machine loop if End of input has been reached then if currentstate is an accept state then return accept else return reject elsif transitiontable[currentstate,tape[index]] is empty then return reject else currentstatetransitiontable[currentstate,tape[index]] indexindex + 1 end February 11, 2012 Veton Kpuska 45 Tracing Execution for Some Sheep Talk q0 q1 q2 q3 q3 q4 b a a a ! Before examining the beginning of the tape, the machine is in state q0. Finding a b on input tape, it changes to state q1 as indicated by the contents of transitiontable[q0,b] in Fig. It then finds an a and switches to state q2, another a puts it in state q3, a third a leaves it in state q3, where it reads the "!", and switches to state q4. Since there is no more input, the End of input condition at the beginning of the loop is satisfied for the first time and the machine halts in q4. State q4 is an accepting state, and so the machine has accepted the string baaa! as a sentence in the sheep language. February 11, 2012 Veton Kpuska 46 Fail State The algorithm will fail whenever there is no legal transition for a given combination of state and input. The input abc will fail to be recognized since there is no legal transition out of state q0 on the input a, (i.e., this entry of the transition table has a ). Even if the automaton had allowed an initial a it would have certainly failed on c, since c isn't even in the sheeptalk alphabet! We can think of these "empty" elements in the table as if they all pointed at one "empty" state, which we might call the fail state or sink state. In a sense then, we could FAIL STATE view any machine with empty transitions as if we had augmented it with a fail state, and drawn in all the extra arcs, so we always had somewhere to go from any state on any possible input. Just for completeness, next Fig. shows the FSA from previous Figure with the fail state qF filled in. February 11, 2012 Veton Kpuska 47 Adding a Fail State to FSA a b a ! a ! ! q0 Start State q1 b q2 b qF q3 b ! q4 Final State a ! b a February 11, 2012 Veton Kpuska 48 Formal Languages Key Concept #1. Formal Language: A model which can both generate and recognize all an only the strings of a formal language acts as a definition of the formal language. A formal language is a set of strings, each string composed of symbols from a finite symbolset called an alphabet (the same alphabet used above for defining an automaton!). The alphabet for a "sheep" language is the set = {a,b, !}. Given a model m (such as FSA) we can use L(m) to mean "the formal language characterized by m". L(m)={baa!,baaa!, baaaa!, baaaaa!,....} February 11, 2012 Veton Kpuska 49 Example 2 Alphabet consisting of words. Must build an FSA that models the subpart of English language that deals with amounts of money. Such a formal language would model the subset of English that consists of phrases like ten cents, three dollars, one dollar thirtyfive cents, etc. 1. Solve the problem of building FSA for numbers 199 with which we will model cents. 2. Model dollar amounts by adding cents to it. February 11, 2012 Veton Kpuska 50 FSA for the words for English numbers 199 One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q0 Start State Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q1 One Two Three Four Five Six Seven Eight Nine q2 Final State February 11, 2012 Veton Kpuska 51 FSA for the simple Dollars and Cents One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q3 cents One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q7 cents q0 Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q1 One Two Three Four Five Six Seven Eight Nine q2 dollars q4 Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety q5 One Two Three Four Five Six Seven Eight Nine q6 February 11, 2012 Veton Kpuska 52 Homework #1 Problem 1. Complete the FSA for English money expressions in Fig. 2.16 (of the pdf: http://www.cs.colorado.edu/~martin/SLP/Updates/2.pdf) as suggested in the text following the figure. You should handle amounts up to $100,000, and make sure that "cent" and "dollar" have the proper plural endings when appropriate. February 11, 2012 Veton Kpuska 53 NonDeterministic FSAs a q0 Start State b q1 a q2 a q3 ! q4 Final State Deterministic FSA a q0 Start State b q1 a q2 a q3 ! q4 Final State NonDeterministic FSA Veton Kpuska February 11, 2012 54 Deterministic vs Nondeterministic FSA Deterministic FSA is one whose behavior during recognition is fully determined by the state it is in and the symbol it is looking at. The FSA in the previous slide when FSA is at the state q2 and the input symbol is a we do not know whether to remain in state 2 (selfloop transition) or state 3. Clearly the decision dependents on the next input symbols. February 11, 2012 Veton Kpuska 55 Another NFSA for "sheep" language a q0 Start State b q1 a q2 q3 ! q4 Final State transition defines the arc that cases transition without an input symbol. Thus when in state q3 transition to state q2 is allowed without looking at the input symbol or advancing input pointer. This example is another kind of nondeterministic behavior we might not know whether to follow the transition or the ! arc. February 11, 2012 Veton Kpuska 56 Using NFSA to Accept Strings There is a problem of (wrong) choice in nondeterministic FSA. There are three standard solutions to the problem of non determinism: Backup: Whenever we come to a choice point, we could put a marker to mark where we were in the input, and what state the automaton was in. Then if it turns out that we took the wrong choice, we could back up and try another path. Lookahead: We could look ahead in the input to help us decide which path to take. Parallelism: Whenever we come to a choice point, we could look at every alternative path in parallel. We will focus here on the backup approach and defer discussion of the lookahead and parallelism approaches to later chapters. February 11, 2012 Veton Kpuska 57 Backup Approach for NFSA Recognizer The backup approach suggests that we should make choices that might lead to deadends, knowing that we can always return to unexplored alternative choices. There are two keys to this approach: 1. Must know ALL alternatives for each choice point. 2. Store sufficient information about each alternative so that we can return to it when necessary. February 11, 2012 Veton Kpuska 58 Backup Approach for NFSA Recognizer When a backup algorithm reaches a point in its processing where no progress can be made: Runs out of input, or Has no legal transitions, It returns to a previous choice point and selects one of the unexplored alternatives and continues from there. To apply this notion to current definition of FSA we need only to store two things for each choice point: The State (or node) Corresponding position on the tape. February 11, 2012 Veton Kpuska 59 Search State Combination of the node and the position specifies the search state of the recognition algorithm. To avoid confusion, the state of automaton is called a node or machinestate. To changes are necessary in transition table: 1. To represent nodes that have transitions we need to add column, 2. Accommodate multiple transitions to different nodes from the same input symbol. Each cell entry consists of the list of destination nodes rather then a single node. February 11, 2012 Veton Kpuska 60 The Transition table from NFSA Input State 0 1 2 3 4: b 1 a 2 2,3 ! 4 2 a q0 Start State February 11, 2012 b q1 a q2 q3 ! q4 Final State 61 Veton Kpuska function NDRECOGNIZE(tape,machine) returns accept or reject agenda{(Initial state of machine, beginning of tape)} currentsearchstateNEXT(agenda) loop if ACCEPTSTATE?(currentsearchstate) returns true then return accept else agenda GENERATENEWSTATES(currentsearchstate) if agenda is empty then return reject else currentsearchstateNEXT(agenda) end February 11, 2012 Veton Kpuska 62 function GENERATENEWSTATES(currentstate) returns a set of searchstates currentnodethe node the current searchstate is in indexthe point on the tape the current searchstate is looking at return a list of search states from transition table as follows: (transitiontable[currentnode, ], index) (transitiontable[currentnode, tape[index]], index + 1) function ACCEPTSTATE?(searchstate) returns true or false currentnodethe node searchstate is in indexthe point on the tape searchstate is looking at if index is at the end of the tape and currentnode is an accept state of machine then return true else return false February 11, 2012 Veton Kpuska 63 Possible execution of NDRECOGNIZE q0 1 b a a a ! q0 q1 2 b a a a ! q1 q2 3 q2 q3 4 b a a a ! X b a a a ! q3 b a a a ! q2 b a a a ! q3 b a a a ! q3 b a a a ! February 11, 2012 Veton Kpuska 6 5 7 8 64 Recognition as Search NDRECOGNIZE accomplishes the task of recognizing strings in a regular language by providing a way to systematically explore all the psossible paths through a machine. This kind of solutions are known as statespace search algorithms. The key to the effectiveness of such programs is often the order which the states in the space are considered. A poor ordering of states may lead to the examination of a large number of unfruitful states before a successful solution is discovered. Unfortunately typically it is not possible to tell a good choice from a bad one, and often the best we can do is to insure that each possible solution is eventually considered. Node that the ordering of states is left unspecified in NDRECONGIZE (NEXT function). Thus critical to the performance of the algorithm is the implementation of NEXT function. February 11, 2012 Veton Kpuska 65 DepthFirstSearch DepthFirstSearch or LastInFirstOUT (LIFO). Next return the state at the front of the agenda. Pitfall: Under certain circumstances they can enter an infinite loop. February 11, 2012 Veton Kpuska 66 BreadthFirst Search BreadthFirst Search or First In First Out (FIFO) strategy. Pitfalls: As with depthfirst if the statespace is infinite, the search may never terminate. More importantly due to growth in the size of the agenda if the state if the statespace is even moderately large, the search may require an impractically large amount of memory. All possible choices explored at once. For larger problems, more complex search techniques such as dynamic programming or A* must be used. February 11, 2012 Veton Kpuska 67 Regular Languages and FSA The class of languages that definable by Regular Expressions is exactly the same as the class of languages that are characterizable by finitestate automata: Those languages are called Regular Languages. February 11, 2012 Veton Kpuska 68 Formal Definition of Regular Languages alphabet = set of symbols in a language. empty string empty set. The of regular languages (or regular sets) over is then formally defined as follows: 1. 2. 3. is a regular language a , {a} is a regular language If L1 and L2 are regular languages, then so are: a) b) c) L1L2 = {xy|x L1, y L2}, the concatenation of L1 and L2 L1 L2, the union or disjunction of L1 and L2 L1*, the * closure of L1. All and only the sets of languages which meet the above properties are regular languages. Veton Kpuska 69 February 11, 2012 Regular Languages and FSAs All regular languages can be implemented by the three operations which define regular languages: Concatenation Disjunction|Union (also called "|"), * closure. Example: (*,+,{n,m}) are just a special case of repetition plus * closure. All the anchors can be thought of as individual special symbols. The square braces are a kind of disjunction: [ab] means "a or b", or The disjunction of a and b. February 11, 2012 Veton Kpuska 70 Regular Languages and FSAs Regular languages are also closed under the following operations: Intersection: if L1 and L2 are regular languages, then so is L1 L2, the language consisting of the set of strings that are in both L1 and L2. Difference: if L1 and L2 are regular languages, then so is L1 L2, the language consisting of the set of strings that are in L1 but not L2. Complementation: if L1 and L2 are regular languages, then so is *L1, the set of all possible strings that are not in L1. Reversal: if L1 is regular language, then so is L1R, the language consisting of the set of reversals of the strings that are in L1. Veton Kpuska 71 February 11, 2012 Regular Expressions and FSA The regular expressions are equivalent to finitestate automaton (Proof: Hopcroft and Ullman 1979). Proof is inductive. Each primitive operations of a regular expression (concatenation, union, closure) is shown as part of inductive step of the proof: q0 (a) r= qf q0 (a) r= qf q0 a (a) r=a qf Automata for the base case (no operators) for the induction showing that any regular expression can be turned into an equivalent automaton February 11, 2012 Veton Kpuska 72 Concatenation FSAs next to each other by connecting all the final states of FSA1 to the initial state of FSA2 by an transition q0 qf q0 qf FSA1 FSA2 February 11, 2012 Veton Kpuska 73 Closure Repetition: All final states of the FSA back to the initial states by Zero occurrences case: Direct link from the initial state to final state transition q0 q0 qf qf Closure of an FSA February 11, 2012 Veton Kpuska 74 Union Add a single new initial state q0, and add new transitions from it to the former initial states of the two machines to be joined q0 qf FSA1 q0 q0 qf qf FSA2 The union (|) of two FSAs February 11, 2012 Veton Kpuska 75 Summary This chapter introduced the most important fundamental concept in language processing, the finite automaton, and the practical tool based on automaton, the regular expression. Here's a summary of the main points we covered about these ideas: The regular expression language is a powerful tool for pattern matching. Basic operations in regular expressions include concatenation of symbols, disjunction of symbols (, |, and .), counters (*, +, and {n,m}), anchors (^,$) and precedence operators ((,)). Any regular expression can be realized as a finite state automaton (FSA). February 11, 2012 Veton Kpuska 76 Summary Memory (\1 together with ()) is an advanced operation that is often considered part of regular expressions, but which cannot be realized as a finite automaton. An automaton implicitly defines a formal language as the set of strings the automaton accepts. An automaton can use any set of symbols for its vocabulary, including letters, words, or even graphic images. February 11, 2012 Veton Kpuska 77 Summary The behavior of a deterministic automaton (DFSA) is fully determined by the state it is in. A nondeterministic automaton (NFSA) sometimes has to make a choice between multiple paths to take given the same current state and next input. Any NFSA can be converted to a DFSA. The order in which a NFSA chooses the next state to explore on the agenda defines its search strategy. The depthfirst search or LIFO strategy corresponds to the agendaasstack; The breadthfirst search or FIFO strategy corresponds to the agendaasqueue. Any regular expression can be automatically compiled into a NFSA and hence into FSA February 11, 2012 Veton Kpuska 78 ...
View Full Document

Ask a homework question - tutors are online