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 generated poem is devoid of meaning and coherence, similar to what I had generated with the RiTa library. The idea is to show the kind of interesting things that one can do with iLangGen.
First, we need to get words with interesting patterns and properties, for example, “adjectives” that end in “ate“, or “verbs” with 4 syllables, and so on. For this I use my iLexicon as backend.
Here is how we set up the words to use:
The LHS is a variable that gets bound to the evaluated expression on the RHS.
The next step is to define one or more grammars that control the generation process. Here is a simple version of “Verb Phrase“:
You will notice that in some of the productions, the RHS involves invoking a function to dynamically fetch a word from the word list. For instance, the production with “adjective” as the LHS gets a random adjective from our word list.
The last production in “VerbPhrase” defines a “word separator“, which is itself defined in another grammar called “Utils“. This ability to reference productions in other grammars is an example of grammar re-use in iLangGen and allows us to develop complex grammars. I have described the features of iLangGen in a series of articles earlier (August 2017). Here is the simple “Utils” grammar:
To make the generated poem look more realistic, we define a few more grammars:
To add further variety, let us define two grammars to generate simple “why” and “who” questions:
The third step in the generation strategy is to define “sentence patterns“. Each line (or sentence) in the poem is generated using this pattern. The pattern itself refers to a grammar and one or more non-terminals in that grammar. The generator synthesizes a line of the poem from the referenced grammar, randomly choosing a non-terminal from the list. Here is how we define the patterns:
When the generator decides to generate a line according to “pat3“, for instance, it will use “AdjPhrase” grammar and one of its non-terminals “phrase3“, “phrase5“, or “phrase6” at random.
Finally, to generate a poem, we use the “generate-poem” function, supplying a list of patterns. Here is a sample output:
Each group of patterns defines a “stanza” or “para“.
If we execute the function again, passing the same set of arguments as patterns, we are guaranteed to get a new poem. The text is not repeated!
Here is a simpler version using just the pattern “pat1“:
As you can see, generating text of arbitrary nature, including poems, is possible with iLangGen. Of course, to get acceptable quality, considerable effort needs to be put in in terms of the grammar and choice of words. For example, take a look at “Sent” grammar. It is used in “pat6” and is capable of generating sentences that rhyme by using words that end in “ate“. For this, the generator is ably assisted by the other core component, “iLexicon“.
Hope you enjoyed reading this post. Have a nice weekend.
Recent Comments