SlideShare a Scribd company logo
Clause Grammars in Prolog
OVERVIEWContext free grammarsCFG recognition using appendCFG recognition using difference listsDefinite clause grammarsAdding Recursive RulesA DCG for a simple formal language
Context Free GrammarsDefinite Clauses Grammars(DCGs) are a special notation for defining grammars.context free grammar are a finite collection of rules which tell us that certain sentences are grammatical (that is, syntactically correct) and what their grammatical structure actually is.
CFGConsider the following examples:s -> np vpnp -> det nvp -> v npvp -> vdet -> adet -> then -> womann -> manv -> shootshere, s, np, vp, det, n, v symbols are called non-terminal symbols;
The symbols: a, the, woman, man, and shootsare called terminal symbolsA context free rule consists of a single non-terminal symbol, followed by ->, followed by a finite sequence made up of terminal and/or nonterminalsymbols.
CFG recognition using appendwe can simply `turn the grammar into Prolog'.Ex: the string a woman shoots a man will be represented by the list[a,woman,shoots,a,man].the rule s -> np vp can be thought of as saying: a list of words is an s list if it is the result of concatenating an np list with a vp list.We can make use of append to turn these kinds of rules into Prolog.s(Z) :- np(X), vp(Y), append(X,Y,Z).np(Z) :- det(X), n(Y), append(X,Y,Z).vp(Z) :- v(X), np(Y), append(X,Y,Z).vp(Z) :- v(Z).v([shoots]).
det([the]).det([a]).n([woman]).n([man]).So, on posing the query s([a,woman,shoots,a,man]).We get yesIn fact, our little grammar generates 20 sentences. Here are the first five:s(X).X = [the,woman,shoots,the,woman] ;X = [the,woman,shoots,the,man] ;X = [the,woman,shoots,a,woman] ;X = [the,woman,shoots,a,man] ;X = [the,woman,shoots]
CFG recognition using difference listsA more efficient implementation can be obtained by making use of difference lists.The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists. For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists[a,woman,shoots,a,man] [].represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in.That is: the sentence we are interested in is the difference between the contents of these two lists.
Definite clause grammarsDCGs, quite simply is a nice notation for writing grammars that hides the underlying difference list variables.Ex: The previous grammar written as a DCG:s --> np,vp.np --> det,n.vp --> v,np.vp --> v.det --> [the].det --> [a].n --> [woman].n --> [man].v --> [shoots].
Definite clause grammarsTo find out whether a woman shoots a man is a sentence, we pose the query:s([a,woman,shoots,a,man],[]).That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind.Similarly, to generate all the sentences in the grammar, we pose the query:s(X,[]).
Adding Recursive RulesOur original context free grammar generated only 20 sentences. However it is easy to write context free grammars that generate infinitely many sentences: we need simply use recursive rules.EX: Let's add the following rules to our grammar:s -> s conj sconj -> andconj -> orconj -> butThis rule allows us to join as many sentences together as we like using the words and, but and or.
Adding Recursive RulesTurning this grammar into DCG rules. s --> s,conj,s.conj --> [and].conj --> [or].conj --> [but].First, let's add the rules at the beginning of the knowledge base before the rule s --> np,vp.If now, we pose the query s([a,woman,shoots],[])? Prolog gets into an infinite loop.
So, by just reordering clauses or goals, we won't solve the problem. The only possible solution is to introduce a new nonterminal symbol. We could for example use the category simple_s for sentences without embedded sentences. Our grammar would then look like this:s --> simple_s.s --> simple_s conj s.simple_s --> np,vp.np --> det,n.vp --> v,np.vp --> v.det --> [the].det --> [a].n --> [woman].n --> [man].v --> [shoots].conj --> [and].conj --> [or].conj --> [but].
A DCG for a simple formal languagewe shall define a DCG for the formal language a^nb^n.There are only two `words' in this language:The symbol a and the symbol b.The language consist of all strings made up from these two symbols that have the following form: the string must consist of an unbroken block of as of length n, followed by an unbroken block of bs of length n, and nothing else. So the strings ab, aabb, aaabbb and aaaabbbb all belong to a^nb^n.
CFG to generate this language:s -> epsilons -> l s rl -> ar -> bTurning this grammar into DCG.s --> [].s --> l,s,r.l --> [a].r --> [b].And this DCG works exactly as we would hope. For example, to the querys([a,a,a,b,b,b],[]).we get the answer `yes',

More Related Content

PPTX
Quantum computing - A Compilation of Concepts
PDF
Birthday Paradox explained
PDF
Unit8: Uncertainty in AI
PDF
Multiobjective optimization and Genetic algorithms in Scilab
PPTX
Non Deterministic and Deterministic Problems
PPTX
Knapsack problem solved by Genetic Algorithms
DOCX
KL Algorithm
PDF
Uniform-Cost Search Algorithm in the AI Environment
Quantum computing - A Compilation of Concepts
Birthday Paradox explained
Unit8: Uncertainty in AI
Multiobjective optimization and Genetic algorithms in Scilab
Non Deterministic and Deterministic Problems
Knapsack problem solved by Genetic Algorithms
KL Algorithm
Uniform-Cost Search Algorithm in the AI Environment

What's hot (20)

PDF
08 Exponential Random Graph Models (ERGM)
PDF
Dscrete structure
PDF
Lattice Based Cryptography - GGH Cryptosystem
PPTX
Neural Networks
PPT
Recurrence Relations for Discrete mathematics
PPTX
Art network
PDF
Elliptic Curve Cryptography
PPTX
Binomial Coefficient
PPTX
Stream Cipher.pptx
PPTX
Presentation on fuzzy logic and fuzzy systems
PPTX
Fuzzy logic Notes AI CSE 8th Sem
PPTX
Semantic net in AI
PPT
Rsa rivest shamir adleman
PPT
Dynamic programming
PPTX
Rsa cryptosystem
PPTX
Probabilistic Reasoning
PPTX
mesh algorithms
PDF
State Space Representation and Search
PPTX
Fuzzy logic - Approximate reasoning
08 Exponential Random Graph Models (ERGM)
Dscrete structure
Lattice Based Cryptography - GGH Cryptosystem
Neural Networks
Recurrence Relations for Discrete mathematics
Art network
Elliptic Curve Cryptography
Binomial Coefficient
Stream Cipher.pptx
Presentation on fuzzy logic and fuzzy systems
Fuzzy logic Notes AI CSE 8th Sem
Semantic net in AI
Rsa rivest shamir adleman
Dynamic programming
Rsa cryptosystem
Probabilistic Reasoning
mesh algorithms
State Space Representation and Search
Fuzzy logic - Approximate reasoning
Ad

Viewers also liked (20)

PPT
Seminar on Language Teaching : THE USE OF TOTAL PHYSICAL RESPONSE STORYTELLIN...
PPT
Total Physical Response (TPR) Method
PPT
Eugene SRTS Program
PPTX
Matlab: Saving And Publishing
PPTX
WEKA: Credibility Evaluating Whats Been Learned
PPTX
Data Mining The Sky
PPTX
LISP: Errors In Lisp
PPTX
InfoChimps.Org
PPTX
PPTX
LISP: Scope and extent in lisp
PPTX
SPSS: Data Editor
PPTX
RapidMiner: Nested Subprocesses
PPT
Excel Datamining Addin Intermediate
PPTX
MS Sql Server: Manipulating Database
PPTX
Ireland Apo University Fy 10 Tibbs Slideshare
PPTX
Txomin Hartz Txikia
ODP
Oratoria E RetóRica Latinas
PPTX
Quick Look At Clustering
PPTX
Graph Plots in Matlab
PPTX
R: Apply Functions
Seminar on Language Teaching : THE USE OF TOTAL PHYSICAL RESPONSE STORYTELLIN...
Total Physical Response (TPR) Method
Eugene SRTS Program
Matlab: Saving And Publishing
WEKA: Credibility Evaluating Whats Been Learned
Data Mining The Sky
LISP: Errors In Lisp
InfoChimps.Org
LISP: Scope and extent in lisp
SPSS: Data Editor
RapidMiner: Nested Subprocesses
Excel Datamining Addin Intermediate
MS Sql Server: Manipulating Database
Ireland Apo University Fy 10 Tibbs Slideshare
Txomin Hartz Txikia
Oratoria E RetóRica Latinas
Quick Look At Clustering
Graph Plots in Matlab
R: Apply Functions
Ad

Similar to PROLOG: Clauses Grammer In Prolog (20)

PPT
Lecture 7: Definite Clause Grammars
PDF
ToC_M1L3_Grammar and Derivation.pdf
PPTX
Mikhail Khristophorov "Introduction to Regular Expressions"
PPT
Pl vol1
PPTX
natural language processing
PPT
Pl vol1
PPT
Natural Language Processing 9th Chapter.ppt
PPT
Inteligencia artificial
PDF
Lda2vec text by the bay 2016 with notes
PDF
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
PPTX
Computational model language and grammar bnf
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
PPTX
Regular-expressions in NLP and regular expression with example
PDF
Regular expression
PPT
INFO-2950-Languages-and-Grammars.ppt
ODP
String interpolation
PDF
Compiler Construction | Lecture 8 | Type Constraints
PPT
computer Languages and Grammars.pptx and
ODP
Regular Expressions and You
PPT
Regex Experession with Regex functions o
Lecture 7: Definite Clause Grammars
ToC_M1L3_Grammar and Derivation.pdf
Mikhail Khristophorov "Introduction to Regular Expressions"
Pl vol1
natural language processing
Pl vol1
Natural Language Processing 9th Chapter.ppt
Inteligencia artificial
Lda2vec text by the bay 2016 with notes
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Computational model language and grammar bnf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Regular-expressions in NLP and regular expression with example
Regular expression
INFO-2950-Languages-and-Grammars.ppt
String interpolation
Compiler Construction | Lecture 8 | Type Constraints
computer Languages and Grammars.pptx and
Regular Expressions and You
Regex Experession with Regex functions o

More from DataminingTools Inc (20)

PPTX
Terminology Machine Learning
PPTX
Techniques Machine Learning
PPTX
Machine learning Introduction
PPTX
Areas of machine leanring
PPTX
AI: Planning and AI
PPTX
AI: Logic in AI 2
PPTX
AI: Logic in AI
PPTX
AI: Learning in AI 2
PPTX
AI: Learning in AI
PPTX
AI: Introduction to artificial intelligence
PPTX
AI: Belief Networks
PPTX
AI: AI & Searching
PPTX
AI: AI & Problem Solving
PPTX
Data Mining: Text and web mining
PPTX
Data Mining: Outlier analysis
PPTX
Data Mining: Mining stream time series and sequence data
PPTX
Data Mining: Mining ,associations, and correlations
PPTX
Data Mining: Graph mining and social network analysis
PPTX
Data warehouse and olap technology
PPTX
Data Mining: Data processing
Terminology Machine Learning
Techniques Machine Learning
Machine learning Introduction
Areas of machine leanring
AI: Planning and AI
AI: Logic in AI 2
AI: Logic in AI
AI: Learning in AI 2
AI: Learning in AI
AI: Introduction to artificial intelligence
AI: Belief Networks
AI: AI & Searching
AI: AI & Problem Solving
Data Mining: Text and web mining
Data Mining: Outlier analysis
Data Mining: Mining stream time series and sequence data
Data Mining: Mining ,associations, and correlations
Data Mining: Graph mining and social network analysis
Data warehouse and olap technology
Data Mining: Data processing

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPT
Teaching material agriculture food technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Teaching material agriculture food technology
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MYSQL Presentation for SQL database connectivity
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx

PROLOG: Clauses Grammer In Prolog

  • 2. OVERVIEWContext free grammarsCFG recognition using appendCFG recognition using difference listsDefinite clause grammarsAdding Recursive RulesA DCG for a simple formal language
  • 3. Context Free GrammarsDefinite Clauses Grammars(DCGs) are a special notation for defining grammars.context free grammar are a finite collection of rules which tell us that certain sentences are grammatical (that is, syntactically correct) and what their grammatical structure actually is.
  • 4. CFGConsider the following examples:s -> np vpnp -> det nvp -> v npvp -> vdet -> adet -> then -> womann -> manv -> shootshere, s, np, vp, det, n, v symbols are called non-terminal symbols;
  • 5. The symbols: a, the, woman, man, and shootsare called terminal symbolsA context free rule consists of a single non-terminal symbol, followed by ->, followed by a finite sequence made up of terminal and/or nonterminalsymbols.
  • 6. CFG recognition using appendwe can simply `turn the grammar into Prolog'.Ex: the string a woman shoots a man will be represented by the list[a,woman,shoots,a,man].the rule s -> np vp can be thought of as saying: a list of words is an s list if it is the result of concatenating an np list with a vp list.We can make use of append to turn these kinds of rules into Prolog.s(Z) :- np(X), vp(Y), append(X,Y,Z).np(Z) :- det(X), n(Y), append(X,Y,Z).vp(Z) :- v(X), np(Y), append(X,Y,Z).vp(Z) :- v(Z).v([shoots]).
  • 7. det([the]).det([a]).n([woman]).n([man]).So, on posing the query s([a,woman,shoots,a,man]).We get yesIn fact, our little grammar generates 20 sentences. Here are the first five:s(X).X = [the,woman,shoots,the,woman] ;X = [the,woman,shoots,the,man] ;X = [the,woman,shoots,a,woman] ;X = [the,woman,shoots,a,man] ;X = [the,woman,shoots]
  • 8. CFG recognition using difference listsA more efficient implementation can be obtained by making use of difference lists.The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists. For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists[a,woman,shoots,a,man] [].represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in.That is: the sentence we are interested in is the difference between the contents of these two lists.
  • 9. Definite clause grammarsDCGs, quite simply is a nice notation for writing grammars that hides the underlying difference list variables.Ex: The previous grammar written as a DCG:s --> np,vp.np --> det,n.vp --> v,np.vp --> v.det --> [the].det --> [a].n --> [woman].n --> [man].v --> [shoots].
  • 10. Definite clause grammarsTo find out whether a woman shoots a man is a sentence, we pose the query:s([a,woman,shoots,a,man],[]).That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind.Similarly, to generate all the sentences in the grammar, we pose the query:s(X,[]).
  • 11. Adding Recursive RulesOur original context free grammar generated only 20 sentences. However it is easy to write context free grammars that generate infinitely many sentences: we need simply use recursive rules.EX: Let's add the following rules to our grammar:s -> s conj sconj -> andconj -> orconj -> butThis rule allows us to join as many sentences together as we like using the words and, but and or.
  • 12. Adding Recursive RulesTurning this grammar into DCG rules. s --> s,conj,s.conj --> [and].conj --> [or].conj --> [but].First, let's add the rules at the beginning of the knowledge base before the rule s --> np,vp.If now, we pose the query s([a,woman,shoots],[])? Prolog gets into an infinite loop.
  • 13. So, by just reordering clauses or goals, we won't solve the problem. The only possible solution is to introduce a new nonterminal symbol. We could for example use the category simple_s for sentences without embedded sentences. Our grammar would then look like this:s --> simple_s.s --> simple_s conj s.simple_s --> np,vp.np --> det,n.vp --> v,np.vp --> v.det --> [the].det --> [a].n --> [woman].n --> [man].v --> [shoots].conj --> [and].conj --> [or].conj --> [but].
  • 14. A DCG for a simple formal languagewe shall define a DCG for the formal language a^nb^n.There are only two `words' in this language:The symbol a and the symbol b.The language consist of all strings made up from these two symbols that have the following form: the string must consist of an unbroken block of as of length n, followed by an unbroken block of bs of length n, and nothing else. So the strings ab, aabb, aaabbb and aaaabbbb all belong to a^nb^n.
  • 15. CFG to generate this language:s -> epsilons -> l s rl -> ar -> bTurning this grammar into DCG.s --> [].s --> l,s,r.l --> [a].r --> [b].And this DCG works exactly as we would hope. For example, to the querys([a,a,a,b,b,b],[]).we get the answer `yes',
  • 16. Visit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net