overview
play

Overview A first introduction to Prolog Implementing finite state - PDF document

Overview A first introduction to Prolog Implementing finite state machines and learning Prolog along the way Encoding finite state machines in Prolog Recognition and generation with finite state machines in Prolog Completing the


  1. Overview • A first introduction to Prolog Implementing finite state machines and learning Prolog along the way • Encoding finite state machines in Prolog • Recognition and generation with finite state machines in Prolog • Completing the FSM recognition and generation algorithms to use Detmar Meurers: Intro to Computational Linguistics I • ǫ transitions OSU, LING 684.01 • abbreviations • Encoding finite state transducers in Prolog 2 The Prolog programming language (1) The Prolog programming language (2) PROgrammation LOGique was invented by Alain Colmerauer and colleagues at Marseille and Edinburgh in the early 70s. A Prolog A Prolog program consists of a set of Horn clauses: program is written in a subset of first order predicate logic. There are • unit clauses or facts • constants naming entities – syntax: predicate followed by a dot – syntax : starting with lower-case letter (or number or single quoted) – example: father(tom,mary). – examples: twelve, a, q 1, 14, ’John’ • variables over entities • non-unit clauses or rules – syntax : starting with upper-case letter (or an underscore) – syntax: rel 0 :- rel 1 , ..., rel n . – examples: A, This, – example: grandfather(Old,Young) :- twelve, father(Old,Middle), • predicate symbols naming relations among entities father(Middle,Young). – syntax: predicate name starting with a lower-case letter with parentheses around comma-separated arguments – examples: father(tom,mary) , age(X,15) 3 4 The Prolog programming language (3) A first Prolog program grandfather.pl father(adam,ben). • No global variables: Variables only have scope over a single clause. father(ben,claire). • No explicit typing of variables or of the arguments of predicates. father(ben,chris). • Negation by failure: For \ +(P) Prolog attempts to prove P , and if this grandfather(Old,Young) :- succeeds, it fails. father(Old,Middle), father(Middle,Young). Query: ?- grandfather(adam,X). X = claire ? ; X = chris ? ; no 5 6

  2. Recursive relations in Prolog Recursive relations in Prolog Compound terms as data structures Lists as special compound terms To define recursive relations, one needs a richer data structure than the • empty list: represented by the atom ” [] ” constants (atoms) introduced so far: compound terms . • non-empty list: compound term with ” . ” as binary functor A compound term comprises a functor and a sequence of one or more terms, the argument. 1 Compound terms are standardly written in prefix – first argument: first element of list (“ head ”) notation. 2 – second argument: rest of list (“ tail ”) Example: Example: .(a, .(b, .(c, .(d,[])))) – binary tree: bin tree( mother , l-dtr , r-dtr ) – example: bin tree(s, np, bin tree(vp,v,n)) 1 An atom can be thought of as a functor with arity 0. 2 Infix and postfix operators can also be defined, but need to be declared. 7 8 Abbreviating notations for lists An example for the four notations [a,b,c,d] = .(a, .(b, .(c, .(d,[])))) • bracket notation: [ element1 | restlist ] = [a | [b | [c | [d | []]]]] Example: [a | [b | [c | [d | []]]]] . • element separator: [ element1 , element2 ] = a = [ element1 | [ element2 | []]] . Example: [a, b, c, d] b . c . d [] 9 10 Recursive relations in Prolog Recursive relations in Prolog Example relations I: append Example relations IIa: (naive) reverse • Idea: a relation concatenating two lists • Idea: reverse a list • Example: ?- append([a,b,c],[d,e],X). ⇒ X=[a,b,c,d,e] • Example: ?- reverse([a,b,c],X). ⇒ X=[c,b,a] append([],L,L). naive_reverse([],[]). append([H|T],L,[H|R]) :- naive_reverse([H|T],Result) :- append(T,L,R). naive_reverse(T,Aux), append(Aux,[H],Result). 11 12

  3. Recursive relations in Prolog Some practical matters Example relations IIb: reverse • To start Prolog on the Linguistics Department Unix machines: reverse(A,B) :- • SWI-Prolog: pl (on Mac OSX: swipl ) reverse_aux(A,[],B). • SICStus: prolog or M-x run-prolog in XEmacs reverse_aux([],L,L). • At the Prolog prompt ( ?- ): reverse_aux([H|T],L,Result) :- reverse_aux(T,[H|L],Result). • Trace the next command: trace. • Exit Prolog: halt. • Consult a file in Prolog: [ filename ]. 3 • The manuals are accessible from the course web page. 3 The .pl suffix is added automatically, but use single quotes if name starts with a capital letter or contains special characters such as ”.” or ”–”. For example [’MyGrammar’]. or [’˜/file-1’] . 13 14 Encoding finite state automata in Prolog Prolog representation of a finite state automaton What needs to be represented? The FSA is represented by the following kind of Prolog facts: A finite state automaton is a quintuple ( Q, Σ , E, S, F ) with • initial nodes: initial( nodename ). • Q a finite set of states • final nodes: final( nodename ). • Σ a finite set of symbols, the alphabet • edges: arc( from-node , label , to-node ). • S ⊆ Q the set of start states • F ⊆ Q the set of final states • E a set of edges Q × (Σ ∪ { ǫ } ) × Q 15 16 A simple example An example with two final states FSTN representation of FSM: FSTN representation of FSM: r 1 c 1 d c o l o 0 6 5 4 2 u r 0 a b 3 3 2 Prolog encoding of FSM: Prolog encoding of FSM: initial(0). initial(0). final(1). final(1). final(2). arc(0,c,6). arc(6,o,5). arc(5,l,4). arc(4,o,2). arc(0,c,1). arc(1,d,1). arc(0,a,3). arc(3,b,2). arc(2,r,1). arc(2,u,3). arc(3,r,1). 17 18

  4. Recognition with FSMs in Prolog Generation with FSMs in Prolog fstn traversal basic.pl generate :- test(Words) :- test(X), initial(Node), write(X), recognize(Node,Words). nl, fail. recognize(Node,[]) :- final(Node). recognize(FromNode,String) :- arc(FromNode,Label,ToNode), traverse(Label,String,NewString), recognize(ToNode,NewString). traverse(First,[First|Rest],Rest). 19 20 Encoding finite state transducers in Prolog Prolog representation of a transducer What needs to be represented? The only change compared to automata, is an additional argument in the representation of the arcs: arc( from-node , label-in , to-node , label-out ). A finite state transducer is a 6-tuple ( Q, Σ 1 , Σ 2 , E, S, F ) with Example: • Q a finite set of states initial(1). • Σ 1 a finite set of symbols, the input alphabet final(5). • Σ 2 a finite set of symbols, the output alphabet arc(1,2,where,ou). arc(2,3,is,est). • S ⊆ Q the set of start states arc(3,4,the,la). arc(4,5,exit,sortie). • F ⊆ Q the set of final states arc(4,5,shop,boutique). arc(4,5,toilet,toilette). • E a set of edges Q × (Σ 1 ∪ { ǫ } ) × Q × (Σ 2 ∪ { ǫ } ) arc(3,6,the,le). arc(6,5,policeman,gendarme). 21 22 Processing with a finite state transducer FSMs with ǫ transitions and abbreviations Defining Prolog representations test(Input,Output) :- initial(Node), transduce(Node,Input,Output), 1. Decide on a symbol to use to mark ǫ transitions: ’#’ write(Output),nl. 2. Define abbreviations for labels: transduce(Node,[],[]) :- macro(Label,Word). final(Node). 3. Define a relation special/1 to recognize abbreviations and epsilon transduce(Node1,String1,String2) :- transitions: arc(Node1,Node2,Label1,Label2), traverse2(Label1,Label2,String1,NewString1, String2,NewString2), special(’#’). transduce(Node2,NewString1,NewString2). special(X) :- macro(X,_). traverse2(Word1,Word2,[Word1|RestString1],RestString1, [Word2|RestString2],RestString2). 23 24

  5. traverse(Label,[Label|RestString],RestString) :- FSMs with ǫ transitions and abbreviations \+ special(Label). Extending the recognition algorithm traverse(Abbrev,[Label|RestString],RestString) :- macro(Abbrev,Label). test(Words) :- traverse(’#’,String,String). initial(Node), recognize(Node,Words). special(’#’). special(X) :- recognize(Node,[]) :- macro(X,_). final(Node). recognize(FromNode,String) :- arc(FromNode,Label,ToNode), traverse(Label,String,NewString), recognize(ToNode,NewString). 25 26 A tiny English fragment as an example Reading assignment (fsa/ex simple engl.pl) arc(7,n,9). macro(n,man). initial(1). • Pages 1–26 of Fernando Pereira and Stuart Shieber (1987): Prolog final(9). arc(8,adj,9). macro(n,woman). and Natural-Language Analysis . Stanford: CSLI. arc(1,np,3). arc(8,mod,8). macro(pv,is). arc(9,cnj,4). macro(pv,was). arc(1,det,2). arc(2,n,3). arc(9,cnj,1). macro(cnj,and). macro(cnj,or). arc(3,pv,4). arc(4,adv,5). macro(np,kim). macro(adj,happy). macro(np,sandy). macro(adj,stupid). arc(4,’#’,5). arc(5,det,6). macro(np,lee). macro(mod,very). arc(5,det,7). macro(det,a). macro(adv,often). arc(5,’#’,8). macro(det,the). macro(adv,always). arc(6,adj,7). macro(det,her). macro(adv,sometimes). macro(n,consumer). arc(6,mod,6). 27 28

Recommend


More recommend