{"id":2152,"date":"2020-09-28T20:52:13","date_gmt":"2020-09-28T15:22:13","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2152"},"modified":"2020-09-28T20:52:13","modified_gmt":"2020-09-28T15:22:13","slug":"template-based-text-generation","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/","title":{"rendered":"Template-Based Text Generation"},"content":{"rendered":"<p>I had written earlier about <a href=\"https:\/\/www.rangakrish.com\/index.php\/2018\/04\/08\/natural-language-generation\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>natural language generation<\/strong><\/em><\/a>\u00a0using my <em><strong>iLangGen<\/strong><\/em> framework. I used a <em><strong>&#8220;template&#8221;<\/strong><\/em> text file which was instantiated dynamically based on predefined <em><strong>&#8220;grammars&#8221;<\/strong><\/em> and external data. The sample application I show-cased demonstrated its utility and versatility.<\/p>\n<p>Today I would like to touch upon a few other <em><strong>&#8220;pattern&#8221;<\/strong><\/em> elements that can be embedded in the template. All these patterns, including the ability to specify grammar rules,<span class=\"Apple-converted-space\">\u00a0 <\/span>make this template-based approach quite powerful and applicable in real-world applications.<span class=\"Apple-converted-space\">\u00a0 \u00a0<\/span><\/p>\n<h3>Structure of Template File<\/h3>\n<p>The template file can have normal text that will be instantiated, as well as supporting code fragments (only <em><strong>Lisp<\/strong><\/em> is supported as of now).<\/p>\n<p>The <em><strong>&#8220;text&#8221;<\/strong><\/em> section starts with the marker <em><strong>&#8220;@text&#8221;<\/strong><\/em> and ends with <em><strong>&#8220;@end&#8221;<\/strong><\/em>. Code section starts with the marker <em><strong>&#8220;@code&#8221;<\/strong><\/em> and ends with <em><strong>&#8220;@end&#8221;<\/strong><\/em>. In case many functions are to be defined, it might be convenient to keep the code in a separate file and reference it using <em><strong>&#8220;@code-file&#8221;<\/strong><\/em> marker.<\/p>\n<p>Here is the overall structure example:<\/p>\n<blockquote><p><strong><span style=\"color: #0000ff;\">@code-file &#8220;utils.lisp&#8221;<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">@text<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">The actual template text goes here&#8230;<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">@end<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">Anything in this area (outside of sections) is ignored..<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">@code<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">Lisp code goes here&#8230;<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">@end<\/span><\/strong><\/p><\/blockquote>\n<h3>Supported Patterns<\/h3>\n<p>The text section contains the actual text that gets instantiated based on different patterns embedded in it.<\/p>\n<p>A pattern is defined within the delimiters <em><strong>&#8220;{{&#8220;<\/strong><\/em> and &#8220;<em><strong>}}&#8221;<\/strong><\/em>. The different patterns are<\/p>\n<h4>Grammar Pattern:<\/h4>\n<p>We saw this example in the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2018\/04\/08\/natural-language-generation\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>earlier article<\/strong><\/em><\/a>. It is of the form <em><strong>{{Grammar::Non-Terminal}}<\/strong><\/em>. During instantiation, the grammar is invoked starting at the specified non-terminal.<\/p>\n<h4>Immediate Value:<\/h4>\n<p>This has the form <em><strong>{{! $1}}<\/strong><\/em>. The variable <em><strong>&#8220;$1&#8221;<\/strong><\/em> specifies the first argument passed to the template processor function. Allowed variables are <em><strong>&#8220;$1&#8221; &#8230; &#8220;$5&#8221;<\/strong><\/em>. As part of template expansion, the variable&#8217;s value is directly substituted at the place where the pattern appears.<\/p>\n<h4>Function Call:<\/h4>\n<p>Similar to the grammar pattern, function application gives tremendous expressive power to the template structure. It looks like this: <em><strong>{{function arg1 arg2 arg3}}<\/strong><\/em>. The arguments can be literals or the run-time parameters <em><strong>&#8220;$1&#8221; &#8230; &#8220;$5&#8221;<\/strong><\/em>.<\/p>\n<h4>Embedded Template:<\/h4>\n<p>It is possible to specify another template file to be instantiated and the result embedded as part of this template instantiation. The syntax is <em><strong>{{&lt; \u00a0another-template-file.txt}}<\/strong><\/em>. This is another feature of immense value since we can now allow template composition and hence take advantage of reusable structures.<\/p>\n<p>Here is a sample template file.<\/p>\n<figure id=\"attachment_2153\" aria-describedby=\"caption-attachment-2153\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2153\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/template\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg\" data-orig-size=\"958,622\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601313302&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=\"Template File\" data-image-description=\"&lt;p&gt;Template File&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Template File&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg\" class=\"wp-image-2153\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?resize=600%2C390&#038;ssl=1\" alt=\"Template File\" width=\"600\" height=\"390\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?resize=300%2C195&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?resize=768%2C499&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?resize=140%2C90&amp;ssl=1 140w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template.jpg?w=958&amp;ssl=1 958w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2153\" class=\"wp-caption-text\"><strong>Template File<\/strong><\/figcaption><\/figure>\n<p>The above template contains one <em><strong>&#8220;Immediate Value&#8221;<\/strong><\/em> pattern and two<em><strong> &#8220;Function Call&#8221;<\/strong><\/em> patterns. You can also see the code section that contains a single function definition and also a reference to an external code file. Here is the code file:<\/p>\n<figure id=\"attachment_2154\" aria-describedby=\"caption-attachment-2154\" style=\"width: 512px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2154\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/mycode\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg\" data-orig-size=\"512,103\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601313223&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=\"External Lisp Code\" data-image-description=\"&lt;p&gt;External Lisp Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;External Lisp Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg\" class=\"wp-image-2154 size-full\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg?resize=512%2C103&#038;ssl=1\" alt=\"External Lisp Code\" width=\"512\" height=\"103\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg?w=512&amp;ssl=1 512w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/mycode.jpg?resize=300%2C60&amp;ssl=1 300w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><figcaption id=\"caption-attachment-2154\" class=\"wp-caption-text\"><strong>External Lisp Code<\/strong><\/figcaption><\/figure>\n<h3>Instantiating the Template<\/h3>\n<p>Template instantiation occurs through the function call <em><strong>&#8220;process-template-file&#8221;<\/strong><\/em>. See the invocation below:<\/p>\n<figure id=\"attachment_2155\" aria-describedby=\"caption-attachment-2155\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2155\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/session-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg\" data-orig-size=\"933,234\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1601313539&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=\"Instantiating the Template\" data-image-description=\"&lt;p&gt;Instantiating the Template&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Instantiating the Template&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg\" class=\"wp-image-2155\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg?resize=600%2C150&#038;ssl=1\" alt=\"Instantiating the Template\" width=\"600\" height=\"150\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg?resize=300%2C75&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg?resize=768%2C193&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Session.jpg?w=933&amp;ssl=1 933w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2155\" class=\"wp-caption-text\"><strong>Instantiating the Template<\/strong><\/figcaption><\/figure>\n<p>The first argument is the template file to be instantiated and the remaining arguments (optional) represent the run-time arguments <em><strong>&#8220;$1&#8221; &#8230; &#8220;$5&#8221;<\/strong><\/em> we mentioned earlier. You can see how the template gets instantiated.<\/p>\n<p>This example does not use the <em><strong>&#8220;Embedded Template&#8221;<\/strong><\/em> pattern. I will discuss this pattern and its usage in detail in a future article.<\/p>\n<p>Hope you find the idea interesting. <em><strong>iLangGen<\/strong><\/em> framework and the template-based generation engine are all written in <a href=\"http:\/\/www.lispworks.com\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>LispWorks Lisp<\/strong><\/em><\/a>.<\/p>\n<p>Have a great week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had written earlier about natural language generation\u00a0using my iLangGen framework. I used a &#8220;template&#8221; text file which was instantiated dynamically based on predefined &#8220;grammars&#8221; and external data. The sample application I show-cased demonstrated its utility and versatility. Today I would like to touch upon a few other &#8220;pattern&#8221; elements that can be embedded in [&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_post_was_ever_published":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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,107,17],"tags":[109,19,139,108],"class_list":["post-2152","post","type-post","status-publish","format-standard","hentry","category-lisp","category-natural-language-processing","category-programming","tag-ilanggen","tag-lisp","tag-natural-language-generation","tag-text-generation"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-yI","jetpack-related-posts":[{"id":2162,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/10\/11\/template-based-text-generation-part-2\/","url_meta":{"origin":2152,"position":0},"title":"Template-based Text Generation &#8211; Part 2","author":"admin","date":"October 11, 2020","format":false,"excerpt":"In my previous article, I showed how \u201ciLangGen\u201d framework facilitates text generation using templates. I talked about the various \u201cpatterns\u201d that can be used in a template. However, in that article, I did not go into the details of the \u201cEmbedded Template\u201d pattern. That is the focus of today\u2019s article.\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Main Template","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/10\/main-template-300x137.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":884,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/04\/08\/natural-language-generation\/","url_meta":{"origin":2152,"position":1},"title":"Natural Language Generation","author":"admin","date":"April 8, 2018","format":false,"excerpt":"I had written a series of posts on my iLangGen framework last year. It aims to provide a flexible and expressive approach for building natural language generation systems. In today's post, I would like to describe a concrete example of how iLangGen can be used for generating natural language text\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Overall Approach","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/04\/overall-1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":575,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/08\/06\/text-generation-using-ilanggen-framework\/","url_meta":{"origin":2152,"position":2},"title":"Text Generation Using iLangGen Framework","author":"admin","date":"August 6, 2017","format":false,"excerpt":"The two primary areas in Natural Language processing are Natural Language Understanding and Natural Language Generation. The former is concerned with processing and making sense of natural language text, whereas the latter is concerned with synthesizing text, possibly from some deep representation. Both are fascinating and at the same time,\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"iLangGen Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2668,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/","url_meta":{"origin":2152,"position":3},"title":"Template Mixins in D Programming Language","author":"admin","date":"January 23, 2022","format":false,"excerpt":"When I heard the name Mixin\u00a0for the first time in the context of Dlang, I imagined it would be something similar to the mixins\u00a0of Common Lisp, but I was completely wrong!\u00a0 Mixins are a very interesting feature of D language. They allow code to be \"injected\" into the source at\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Simple Template Mixin","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn-300x98.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1410,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/01\/27\/generating-poetry-using-ilanggen\/","url_meta":{"origin":2152,"position":4},"title":"Generating Poetry Using iLangGen","author":"admin","date":"January 27, 2019","format":false,"excerpt":"In an earlier article, I wrote about using iLangGen to generate natural language text. iLangGen is a powerful text generation library that I have been working on over the years. Today, I would like to show how we can use that library to generate \"poetry\". Be warned, however, that the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Sample Output 2","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/01\/Output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":637,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/","url_meta":{"origin":2152,"position":5},"title":"Composition of Grammars","author":"admin","date":"September 27, 2017","format":false,"excerpt":"In the last post, we saw how iLangGen text generation framework supports reuse of grammars through inheritance, akin to object-oriented languages. The good news is that we can achieve reuse through composition as well. The following is a simple grammar, nothing fancy to elaborate. Here is the output when you\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Simple Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2152","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=2152"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}