Prolog programming....Dr.Yasser Nada. Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Simply Logical – Chapter 7© Peter Flach, 2000 Context-free grammar sentence--> noun_phrase,verb_phrase. noun_phrase--> proper_noun. noun_phrase--> article,adjective,noun.
Verbals and Verb Phrases
Augmented Transition Networks
November 2008NLP1 Natural Language Processing Definite Clause Grammars.
CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Cs7120 (Prasad)L21-DCG1 Definite Clause Grammars
CSA4050: Advanced Topics in NLP Semantics IV Partial Execution Proper Noun Adjective.
1 Grammars and Parsing Allen ’ s Chapters 3, Jurafski & Martin ’ s Chapters 8-9.
Statistical NLP: Lecture 3
DEFINITE CLAUSE GRAMMARS Ivan Bratko University of Ljubljana Faculty of Computer and Information Sc.
1 CS 385 Fall 2006 Chapter 14 Understanding Natural Language Problems.
Analysing Syntax 1 Lesson 8B.
Fall 2008Programming Development Techniques 1 Topic 9 Symbol Manipulation Generating English Sentences Section This is an additional example to symbolic.
Grammars.
GRAMMAR & PARSING (Syntactic Analysis) NLP- WEEK 4.
Natural Language Processing DCG and Syntax NLP DCG A “translation” example: special case A DCG recogniser.
For Monday Read Chapter 23, sections 3-4 Homework –Chapter 23, exercises 1, 6, 14, 19 –Do them in order. Do NOT read ahead.
Amirkabir University of Technology Computer Engineering Faculty AILAB Efficient Parsing Ahmad Abdollahzadeh Barfouroush Aban 1381 Natural Language Processing.
LING 364: Introduction to Formal Semantics Lecture 4 January 24th.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 7: Definite Clause Grammars Theory –Introduce context free grammars and some related concepts.
1 Introduction to Computational Linguistics Eleni Miltsakaki AUTH Fall 2005-Lecture 2.
1/17 Probabilistic Parsing … and some other approaches.
Natural Language Processing AI - Weeks 19 Natural Language Processing PART 2 Lee McCluskey, room 2/07
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
Lecture 19 Methods of simulation of understanding of NL
Grammars.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
For Friday Finish chapter 23 Homework: –Chapter 22, exercise 9.
LING 388: Language and Computers Sandiway Fong Lecture 7.
IV. SYNTAX. 1.1 What is syntax? Syntax is the study of how sentences are structured, or in other words, it tries to state what words can be combined with.
An Intelligent Analyzer and Understander of English Yorick Wilks 1975, ACM.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Transition Network Grammars for Natural Language Analysis - W. A. Woods In-Su Yoon Pusan National University School of Electrical and Computer Engineering.
Notes on Pinker ch.7 Grammar, parsing, meaning. What is a grammar? A grammar is a code or function that is a database specifying what kind of sounds correspond.
Parsing. Language A set of strings from an alphabet which may be empty, finite or infinite. A language is defined by a grammar and we may also say that.
Syntax Why is the structure of language (syntax) important? How do we represent syntax? What does an example grammar for English look like? What strategies.
Linguistic Essentials
Semantic Construction lecture 2. Semantic Construction Is there a systematic way of constructing semantic representation from a sentence of English? This.
1 Definite Clause Grammars for Language Analysis – A Survey of the Formalism and a Comparison with Augmented Transition Networks 인공지능 연구실 Hee keun Heo.
Rules, Movement, Ambiguity
Artificial Intelligence: Natural Language
The man bites the dog man bites the dog bites the dog the dog dog Parse Tree NP A N the man bites the dog V N NP S VP A 1. Sentence  noun-phrase verb-phrase.
CSA3050: Natural Language Algorithms Finite State Devices.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
1 Introduction to Computational Linguistics Eleni Miltsakaki AUTH Spring 2006-Lecture 2.
1 Syntax 1. 2 In your free time Look at the diagram again, and try to understand it. Phonetics Phonology Sounds of language Linguistics Grammar MorphologySyntax.
◦ Process of describing the structure of phrases and sentences Chapter 8 - Phrases and sentences: grammar1.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
NATURAL LANGUAGE PROCESSING
The final chapter.  Constituents ◦ Natural groupings of a sentence  Morphemes ◦ Smallest meaningful units of a word  How to test whether a group of.
LING/C SC/PSYC 438/538 Lecture 19 Sandiway Fong 1.
King Faisal University جامعة الملك فيصل Deanship of E-Learning and Distance Education عمادة التعلم الإلكتروني والتعليم عن بعد [ ] 1 King Faisal University.
Statistical NLP: Lecture 3
Basic Parsing with Context Free Grammars Chapter 13
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
BBI 3212 ENGLISH SYNTAX AND MORPHOLOGY
Introduction to Linguistics
Lecture 7: Introduction to Parsing (Syntax Analysis)
Parts of Speech Mr. White English I.
Linguistic Essentials
Programming Techniques
CSA2050 Introduction to Computational Linguistics
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
Lecture 7: Definite Clause Grammars
Presentation transcript:

prolog programming....Dr.Yasser Nada

Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada

what do we want the parser to do? We would like to know that a sentence is correct according to the (recognised) laws of english grammar.

prolog programming....Dr.Yasser Nada Simple English Syntax The components of this simple syntax will be such categories as sentences, nouns, verbs etc.

prolog programming....Dr.Yasser Nada Here is a description Unit: sentence Constructed from: noun phrase followed by a verb phrase Unit: noun phrase Constructed from: proper noun or determiner followed by a noun Unit: verb phrase Constructed from: verb or verb followed by noun phrase Unit: determiner Examples: a, the Unit: noun Examples: man, cake Unit verb: Examples: ate

prolog programming....Dr.Yasser Nada The Parse Tree Example : shows the parse tree for the sentence: the man ate the cake

prolog programming....Dr.Yasser Nada sentence (s) Nounphrase (np) Verbphrase (vp) Determiner(det)noun The man verbNounphrase (np) Determiner (det)noun ate the cake many sentences are ambiguous because they result in different parse trees

prolog programming....Dr.Yasser Nada First Attempt at Parsing We assume that we will parse sentences converted to list format. That is, the sentence the man ate the cake will be represented by the list [the,man,ate,the,cake]. We use append/3 to glue two lists together. The idea is that append/3 returns the result of gluing takes input as lists in the first and second argument positions and returns the result in the third position.

prolog programming....Dr.Yasser Nada sentence(S):- append(NP,VP,S), noun phrase(NP), verb phrase(VP). noun phrase(NP):- append(Det,Noun,NP), determiner(Det), noun(Noun). verb phrase(VP):- append(Verb,NP,VP), verb(Verb), noun phrase(NP). determiner([a]). determiner([the]). noun([man]). noun([cake]). verb([ate]).

prolog programming....Dr.Yasser Nada Here is what happens to the query: ?- sentence([the,man,ate,the cake]). append/3 succeeds with NP=[], VP=[the,man,ate,the,cake] noun phrase/1 fails append/3 succeeds with NP=[the], VP=[man,ate,the,cake] noun phrase/1 fails append/3 succeeds with NP=[the,man], VP=[ate,the,cake] noun phrase/1 succeeds... verb phrase/1 succeeds This is all very well but the process of parsing with this method is heavily non deterministic.

prolog programming....Dr.Yasser Nada Also, it suffers from not being a very flexible way of expressing some situations. For example, the problem of adjectives: the quick fox is also a noun phrase. We might try to parse this kind of noun phrase with the extra clause: noun phrase(NP):- append(Det,Bit,NP), determiner(Det), append(Adj,Noun,Bit), adjective(Adj), noun(Noun).

prolog programming....Dr.Yasser Nada A Second Approach We now try an approach which is less non-deterministic. We will start by looking at: sentence(In,Out) The idea is that sentence/2 takes in a list of words as input, finds a legal sentence and returns a result consisting of the input list minus all the words that formed the legal sentence. We can define it: sentence(S,S0):- noun_phrase(S,S1), verb _phrase(S1,S0). This declarative reading should help to bridge the gap between what we want to be a sentence and the procedure for finding a sentence.

prolog programming....Dr.Yasser Nada Here is the rest of the parser: noun phrase(NP,NP0):- determiner(NP,NP1), noun(NP1,NP0). verb phrase(VP,VP0):- verb(VP,VP1), noun phrase(VP1,VP0). determiner([a|Rest],Rest). determiner([the|Rest],Rest). noun([man|Rest],Rest). noun([cake|Rest],Rest). verb([ate|Rest],Rest).

prolog programming....Dr.Yasser Nada Prolog Grammar Rules Prolog, as a convenience, will do most of the tedious work for you. What follows, is the way you can take advantage of Prolog. This is how we can define the simple grammar which is accepted as is by Prolog. sentence --> noun phrase, verb phrase. noun phrase --> determiner, noun. verb phrase --> verb, noun phrase. determiner --> [a]. determiner --> [the]. noun --> [man]. noun --> [cake]. verb --> [ate].

prolog programming....Dr.Yasser Nada It is very easy to extend if we want to include adjectives. noun phrase --> determiner, adjectives, noun. adjectives --> adjective. adjectives --> adjective, adjectives. adjective --> [young]. This formulation is sometimes known as a Definite Clause Grammar (DCG).

prolog programming....Dr.Yasser Nada Essentially, the Prolog Grammar Rule formulation is syntactic sugaring.This means that Prolog enables you to write in: sentence --> noun phrase, verb phrase. and Prolog turns this into: sentence(S,S0):- noun phrase(S,S1), verb phrase(S1,S0). and adjective --> [young]. into adjective(A,A0):- C(A,young,A0). where C/3 is a built in Prolog Predicate which is defined as if: C([H|T],H,T).

prolog programming....Dr.Yasser Nada To Use the Grammar Rules Set a goal of the form sentence([the,man,ate,a,cake],[]) and not as sentence. or sentence([the,man,ate,a,cake])

prolog programming....Dr.Yasser Nada How to Extract a Parse Tree We can add an extra argument which can be used to return a result. sentence([[np,NP],[vp,VP]]) --> noun phrase(NP), verb phrase(VP). noun phrase([[det,Det],[noun,Noun]]) --> determiner(Det), noun(Noun). determiner(the) --> [the]. and so on. What we have done above is declare predicates sentence/3, noun phrase/3, verb phrase/3, determiner/3 and so on. The explicit argument is the first and the two others are added when the clause is read in by Prolog. Basically, Prolog expands grammar rule with in arguments into a corresponding clause with n+2 arguments.

prolog programming....Dr.Yasser Nada what structure is returned from solving the goal: sentence(Structure,[the,man,ate,a,cake],[]) The result is: [[np,[[det,the],[noun,man]]],[vp,[... Not too easy to read! We can improve on this representation if we are allowed to use Prolog terms as arguments. For example, in foo(happy(fred),12) the term happy(fred) is one of the arguments of foo/2. Such a term is known as a compound term. we could tidy up our representation of sentence structure to something akin to: sentence([np([det(the),noun(man)]),vp([...

prolog programming....Dr.Yasser Nada Adding Arbitrary Prolog Goals Grammar rules are simply expanded to Prolog goals. We can also insert arbitrary Prolog subgoals on the right hand side of a grammar rule but we must tell Prolog that we do not want them expanded. This is done with the help of braces {}.

prolog programming....Dr.Yasser Nada For example, here is a grammar rule which parses a single character input as an ASCII code and succeeds if the character represents a digit. It also returns the digit found. digit(D) --> [X], { X >= 48, X =< 57, D is X-48 }. The grammar rule looks for a character at the head of a list of input characters and succeeds if the Prolog subgoals { X >= 48, X =< 57, D is X-48 }. succeed.

prolog programming....Dr.Yasser Nada Note that we assume we are working with ASCII codes for the characters and that the ASCII code for 0 is 48 and for 9 is 57. Also note the strange way of signifying equal to or less than as =<.

prolog programming....Dr.Yasser Nada End of chapter