Template-based Text Generation – Part 2

Written by on October 11, 2020 in LISP, Natural Language Processing, Programming with 0 Comments

In my previous article, I showed how “iLangGen” framework facilitates text generation using templates. I talked about the various “patterns” that can be used in a template. However, in that article, I did not go into the details of the “Embedded Template” pattern. That is the focus of today’s article.

Embedded Template

This pattern allows us to instantiate and embed another template in the current template. The format is:

{{ < another-template.txt par1 par2 par3 }}

As you can see, optional parameters can be passed to the template and used as part of the instantiation process.

The idea of embedded templates is appealing because it allows us to define reusable template fragments and compose them, as needed, to form a larger template.

Take a look at the following template:

Main Template

Main Template

Here, we have a reference to an embedded template file “cpp-for-loop.txt” along with 4 parameters. When the template processor sees this reference, it attempts to instantiate it using the passed context (when embedding templates, there should be no cycles).

Here is the referenced template:

Embedded Template - For Loop

Embedded Template – For Loop

We have a code section that defines a Lisp function, followed by the text template. You will notice that this corresponds to a C++ “for” loop. What is interesting is that we use several patterns in this template in order to give a concrete syntax after instantiation. The patterns of the form “{{$1}}” refer to the parameters passed from the main template. If you have read the earlier article, you will also notice that I have omitted the special marker “!” used to refer to the contextual parameter. Instead of “{{! $1}}”, I have used “{{$1}}”. This is a permitted shortcut.

There is also a call to a Lisp function “get-delta” inside the “for” loop. This function, as we mentioned earlier, is defined in the “code” block.

Let us look at the actual output produced by the template processor:

Instantiated Output

Instantiated Output

This shows how the different parameters passed from the main template are used inside the embedded template.

What happens if we instantiate again?

Another Instantiation

Another Instantiation

Because the Lisp function uses a random number, you can see that the RHS of the assignment has changed this time.

Let us try to make the “for” template more interesting, by using grammar-based generation. Here is the enhanced template:

Using Grammar

Using Grammar

Inside the “for” template, I am using a “grammar” pattern. It refers to a grammar named “Test” and a start node called “stmt”. The grammar itself is defined in a separate “code” block (I have left the Lisp function code block undisturbed, although it is not used in this example). What really happens is that when the template processor encounters the grammar pattern, it invokes “iLanGen” to generate a “statement” and substitutes it in the text.

Since the above “for” template uses only 3 parameters $1, $2 and $3, there is no need to pass the fourth parameter from the main template. Here is the modified main template:

Updated Main Template

Updated Main Template

OK, what happens when we run the template processor this time?

Template Instantiation

Template Instantiation

Let us run it one more time:

Another Run

Another Run

We get a slightly different output this time.

Using a grammar pattern gives us greater power and hence we can generate text with better variations. Theoretically, it is possible to generate the entire “for” loop using only grammar pattern, but using a textual “template” along with the grammar pattern makes it more manageable, and is a trade-off.

This completes the discussion on template-based text generation. Hope you find this idea interesting. If I make further enhancements to the template processor, I will be glad to share the updates with you.

Have a nice weekend!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top