{"id":541,"date":"2017-06-04T14:00:25","date_gmt":"2017-06-04T14:00:25","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=541"},"modified":"2017-08-10T03:34:45","modified_gmt":"2017-08-10T03:34:45","slug":"definite-clause-grammars-in-lisp-part-2","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/06\/04\/definite-clause-grammars-in-lisp-part-2\/","title":{"rendered":"Definite Clause Grammars in Lisp &#8211; Part 2"},"content":{"rendered":"<p>In the<a href=\"http:\/\/www.rangakrish.com\/index.php\/2017\/05\/22\/definite-clause-grammars-dcg-in-lisp\/\" target=\"_blank\"> last post<\/a>, I showed how we can implement DCGs in LispWorks using the <a href=\"http:\/\/www.lispworks.com\/products\/knowledgeworks.html\" target=\"_blank\">KnowledgeWorks<\/a> package. The grammar discussed in that post did not take into account subject\/predicate number agreement. This is one of the basic constraints in English grammar. Today I will show how easy it is to encode this constraint.<\/p>\n<p>Here is a Prolog DCG:<\/p>\n<figure id=\"attachment_542\" aria-describedby=\"caption-attachment-542\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Prolog-Grammar.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"542\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/06\/04\/definite-clause-grammars-in-lisp-part-2\/prolog-grammar\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Prolog-Grammar.png\" data-orig-size=\"254,220\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Prolog Grammar\" data-image-description=\"&lt;p&gt;Prolog Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Prolog Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Prolog-Grammar.png\" class=\"wp-image-542\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Prolog-Grammar.png?resize=400%2C346\" alt=\"Prolog Grammar\" width=\"400\" height=\"346\" \/><\/a><figcaption id=\"caption-attachment-542\" class=\"wp-caption-text\"><strong>Prolog Grammar<\/strong><\/figcaption><\/figure>\n<p>You can see that <em><strong>Number<\/strong><\/em>\u00a0is passed as a parameter to the main non-terminals <em><strong>NP<\/strong><\/em> and <em><strong>VP<\/strong><\/em>. As is expected, different words have a corresponding encoding, for example <em><strong>book<\/strong><\/em>\u00a0is marked as <em><strong>singular noun<\/strong><\/em>\u00a0and <em><strong>sleep<\/strong><\/em>\u00a0is marked as <em><strong>plural intransitive verb<\/strong><\/em>.<\/p>\n<p>Similar to what we did in the earlier grammar, each grammar rule in Lisp will take an argument that represents the <em><strong>Number<\/strong><\/em> to use.<\/p>\n<figure id=\"attachment_543\" aria-describedby=\"caption-attachment-543\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"543\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/06\/04\/definite-clause-grammars-in-lisp-part-2\/grammar-in-lisp\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png\" data-orig-size=\"334,400\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Grammar in Lisp\" data-image-description=\"&lt;p&gt;Grammar in Lisp&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar in Lisp&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png\" class=\"wp-image-543\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png?resize=500%2C599\" alt=\"Grammar in Lisp\" width=\"500\" height=\"599\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png?w=334&amp;ssl=1 334w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/Grammar-in-Lisp.png?resize=251%2C300&amp;ssl=1 251w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-543\" class=\"wp-caption-text\"><strong>Grammar in Lisp<\/strong><\/figcaption><\/figure>\n<p>Note how we ignore the <em><strong>Number<\/strong><\/em> agreement for the <em><strong>Noun Phrase<\/strong><\/em> that appears after the <em><strong>Transitive Verb<\/strong><\/em>.<\/p>\n<p>The top level grammar for sentence <em><strong>s<\/strong><\/em> says that whatever number is chosen for the <em><strong>Noun Phrase<\/strong><\/em> is to be satisfied by the matching <em><strong>Verb Phrase<\/strong><\/em>. So, if the input sentence has a <em><strong>plural Noun Phrase<\/strong><\/em>, then it must be followed by a <em><strong>Verb Phrase<\/strong><\/em> that is also <em><strong>plural<\/strong><\/em>.<\/p>\n<p>Here are some inputs to our parser:<\/p>\n<p><span style=\"color: #0000ff;\">CP-USER 2 &gt; (parse-grammar &#8216;s &#8216;(he sleeps))<\/span><\/p>\n<p><span style=\"color: #0000ff;\">(S (HE SLEEPS) NIL)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">T<\/span><\/p>\n<p>So the input is accepted.<\/p>\n<p><span style=\"color: #0000ff;\">CP-USER 3 &gt; (parse-grammar &#8216;s &#8216;(he sleep))<\/span><\/p>\n<p><span style=\"color: #0000ff;\">NIL<\/span><\/p>\n<p><span style=\"color: #0000ff;\">NIL<\/span><\/p>\n<p>The input is rejected.<\/p>\n<p><span style=\"color: #0000ff;\">CP-USER 4 &gt; (parse-grammar &#8216;s &#8216;(they write books))<\/span><\/p>\n<p><span style=\"color: #0000ff;\">(S (THEY WRITE BOOKS) NIL)<\/span><\/p>\n<p><span style=\"color: #0000ff;\">T<\/span><\/p>\n<p>Input is accepted.<\/p>\n<p>That is it! Fairly straightforward to model.<\/p>\n<p>One thing you would have noticed is that we do not get a parse tree of the input if parsing is successful. This is not very useful.<\/p>\n<p>In the next post, I will show how we can enhance the grammar to support Number agreement and also construct the parse tree along the way.<\/p>\n<p>You can download the grammar <a href=\"http:\/\/www.rangakrish.com\/downloads\/DCGAgreementExample.lisp\" target=\"_blank\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last post, I showed how we can implement DCGs in LispWorks using the KnowledgeWorks package. The grammar discussed in that post did not take into account subject\/predicate number agreement. This is one of the basic constraints in English grammar. Today I will show how easy it is to encode this constraint. Here is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[18,107,17],"tags":[100,101,99],"class_list":["post-541","post","type-post","status-publish","format-standard","hentry","category-lisp","category-natural-language-processing","category-programming","tag-dcg","tag-definite-clause-grammar","tag-lispworks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-8J","jetpack-related-posts":[{"id":2832,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/12\/definite-clause-grammars-in-lisp-part-4\/","url_meta":{"origin":541,"position":0},"title":"Definite Clause Grammars in Lisp &#8211; Part 4","author":"admin","date":"June 12, 2022","format":false,"excerpt":"In a series of articles\u00a0written earlier, I had shown how it is possible to model Definite Clause Grammars (DCG) in LispWorks Lisp (Enterprise Edition). We use defgrammar\u00a0in Common Prolog (available as part of KnowledgeWorks package) to define our grammar rules. Here is a toy English grammar represented using defgrammar: This\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Using Defgrammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":534,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/05\/22\/definite-clause-grammars-dcg-in-lisp\/","url_meta":{"origin":541,"position":1},"title":"Definite Clause Grammars (DCG) in Lisp","author":"admin","date":"May 22, 2017","format":false,"excerpt":"Definite Clause Grammars (DCG) are an elegant formalism for specifying context free grammars, and part of their popularity is due to their support in the Prolog language. Most books on Natural Language processing usually include a brief coverage of DCGs, even though Natural languages are not context-free. Because of the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=525%2C300 1.5x"},"classes":[]},{"id":548,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/06\/23\/definite-clause-grammars-in-lisp-part-3\/","url_meta":{"origin":541,"position":2},"title":"Definite Clause Grammars in Lisp &#8211; Part 3","author":"admin","date":"June 23, 2017","format":false,"excerpt":"In today's post, let us see how we can enhance the grammar representation discussed so far to include both Number constraint and Parse Tree. Fortunately, this turns out to be quite straightforward. Just as we do in Prolog, we need to include additional parameters, as needed, to each grammar rule.\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"POS Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/POS-Function.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/POS-Function.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/06\/POS-Function.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1659,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/08\/04\/generating-poetry-in-prolog\/","url_meta":{"origin":541,"position":3},"title":"Generating Poetry in Prolog","author":"admin","date":"August 4, 2019","format":false,"excerpt":"In an earlier article, I showed how we can generate poetry (with limitations, of course!) using my iLangGen framework. That implementation (in Lisp) made use of iLexicon, a large dictionary of English words, which I have been building over the years. I subsequently ported iLexicon to Prolog and it now\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Generation Logic","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1817,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/12\/08\/using-definite-clause-grammars-dcg-for-information-extraction\/","url_meta":{"origin":541,"position":4},"title":"Using Definite Clause Grammars (DCG) for Information Extraction","author":"admin","date":"December 8, 2019","format":false,"excerpt":"In the previous article, I showed how we can use ATNs for extracting key information from natural language text. I also pointed out in that article that Definite Clause Grammars (DCG) are a more compact formalism for doing this. That will be the focus of today's article. For a nice\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Processing the Text","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/12\/Processing-file-code.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/12\/Processing-file-code.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/12\/Processing-file-code.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2366,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/03\/28\/implementing-ilexicon-using-litedb\/","url_meta":{"origin":541,"position":5},"title":"Implementing iLexicon using LiteDB","author":"admin","date":"March 28, 2021","format":false,"excerpt":"iLexicon is an \"intelligent\" dictionary that can be used to build Natural Language applications. I have two implementations, one in Lisp and another in Prolog. Both implementations are memory-based, in order to speed up performance. I have written several articles referencing it, for example see this. \u00a0 LiteDB is a\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Sample Commands","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/03\/Session1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=541"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/541\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}