Using Templates in Computer-Generated Works
Published: 2016-07-17 01:53:38

<blockquote>
<p>&ldquo;Templates may seem like a terrible way to produce sentences until you consider the alternatives.&rdquo; &ndash;<a href="https://github.com/dariusk/NaNoGenMo-2015/issues/16">Chris Pressey</a></p>
</blockquote>
<p>How can you build an algorithm to build &lsquo;human-readable&rsquo; novels? While there was <a href="https://en.wikipedia.org/wiki/Just_This_Once">several</a> <a href="https://en.wikipedia.org/wiki/Racter">attempts</a> made, humans never stumbled on a good &ldquo;answer&rdquo; to that particular question&hellip; yet.</p>
<p>However, people are still planning to sort out this &ldquo;AI hard&rdquo; problem. NaNoGenMo (National Novel Generation Month) is held every November&hellip;to enter, all you have to do is write an algorithm that can generate 50,000 words and then make your source code publically available. We previously discussed techniques to provide <a href="tra/structure-in-computer-generated-novels">&ldquo;structure&rdquo; to computer-generated novels</a> to make these novels easier for humans to read.</p>
<p>But what if we take a different approach to text generation? What if, instead of trying to induce structure in computer-generated works, we directly teach a computer how to write a novel?</p>
<p>There are rules that we have to memorise and use when writing stories. We can call these patterns &ldquo;grammars&rdquo;. We have to teach a human being how to write. We can also &lsquo;teach&rsquo; a computing machine how to compose too&hellip;by essentially hardcoding in the rules of literature. We can name this model a template. We can also write &lsquo;nested&rsquo; templates, having rules-within-rules to try and add more variation to the generated stories.</p>
<p><a href="https://github.com/maetl/calyx">Calyx</a> is a Ruby gem that can be used to quickly define templates. Here&rsquo;s an example of how one such template can be defined:</p>
<pre class="language-ruby"><code>class HelloWorld &lt; Calyx::Grammar
  start '{greeting} {world_phrase}.'
  greeting 'Hello', 'Hi', 'Hey', 'Yo'
  world_phrase '{happy_adj} world', '{sad_adj} world', 'world'
  happy_adj 'wonderful', 'amazing', 'bright', 'beautiful'
  sad_adj 'cruel', 'miserable'
end

HelloWorld.new.generate
#"Hello bright world."</code></pre>
<p>Calyx was originally built to generate the NaNoGenMo novel <a href="https://github.com/dariusk/NaNoGenMo-2015/issues/189">The Gamebook of Dungeon Tropes</a>, a Choose-Your-Own-Adventure with procedurally-generated dungeon rooms. I have personally used it as a good prototyping tool for generating stories. <a href="https://github.com/dariusk/NaNoGenMo-2014/issues/133">Somewhere, Something</a> is another computer-generated novel based on templates, though the programmer used Python instead of Ruby. In fact, it is far easier to find examples where templates are used in some fashion&hellip;than it is to find examples where templates are <em>not</em> used at all.</p>
<p>Templates are probably the easiest way to produce computer-generated literature, and has already been used commercially by companies such as <a href="https://www.narrativescience.com">Narrative Science</a>, <a href="https://automatedinsights.com">Automated Insights</a>, and <a href="http://yseop.com/EN/home">Yseop</a>. These companies produce automated reports and news stories that are founded on real-life data and are consumed by human beings. I have also attempted to produce a similar type of system when I wrote an algorithm to generate the novel <a href="https://github.com/dariusk/NaNoGenMo-2015/issues/45">The Atheists Who Believe In God</a>.</p>
<p>Templates had also been very popular with &ldquo;black hat SEO&rdquo; specialists. These specialists are interested in quick content generation to appease the search engines, no matter how spammy or repetitive this content is. Therefore, these specialists resort to <a href="https://en.wikipedia.org/wiki/Article_spinning">article spinning</a>: taking a prewritten article and then replacing most of the words with equivalent words. As a result, one article can be used as a basis to generate hundreds of &ldquo;unique&rdquo; content pieces. There&rsquo;s even a unique syntax used for article-spinning called Spintax&hellip;and there are many parsers for this format written in languages such as <a href="https://gist.github.com/irazasyed/11256369">PHP</a>, <a href="https://github.com/johnhenry/spintax">JavaScript</a>, and&nbsp;<a href="https://github.com/flintinatux/spintax_parser">Ruby</a>. Spintax has also been written to generate <a href="https://gist.github.com/shanselman/5422230">spam blog comments as well</a>, modifying the possible replies in the hopes of tricking spam filters and human beings into treating the comments as genuine.</p>
<p>The main trouble with templates is the &lsquo;manual labor&rsquo; involved. After all, you still need a human being to produce the templates that the computer uses to compose stories. Yet, this &lsquo;manual labor&rsquo; can be reduced with the use of automation. Spammers has written algorithms to &ldquo;automatically&rdquo; generate spintax based on a human-written article. While the output of the resulting templates may be ugly, they can be cleaned up later by human beings. Thomson-Reuters also received a patent in 2015 to <a href="http://www.google.com/patents/US20150261745">use machine learning to generate templates based on a corpus of preexisting news material</a> that will then be cleaned up by human beings.</p>
<p>Templates can also be criticized for avoiding the problem of actually adding &lsquo;creativity/intelligence&rsquo; to machines. The machine is not really being inspired to write in the same way as a human is&hellip;all it really does is follow orders encoded within a template. But other methods of text generation, such as <a href="https://github.com/karpathy/char-rnn">Char-RNN</a> and <a href="https://en.wikipedia.org/wiki/Markov_chain">Markov chains</a>, has failed in producing long-form stories, though they are technically impressive and are evocative to read in short bursts. The smarter the algorithm, the dumber the creative output&hellip;<a href="http://qz.com/682814/i-want-to-talk-to-you-see-the-creepy-romantic-poetry-that-came-out-of-a-google-ai-system/">at least, for now</a>.</p>
<p>Since templates are so effective in generating human-readable works, they will most likely be used in the near-future in a variety of different fields. It is likely that the first human-readable computer-generated novel will be based on templates used in aggregation with other algorithmic techniques.</p>
<h2>Appendix</h2>
<p>Even this blog post has been generated using &lsquo;spintax&rsquo;, and this &lsquo;spintax&rsquo; was generated using tools that can be easily found using Google. You can see the source Spintax <a href="https://gist.githubusercontent.com/tra38/5ca218e02b470a5b117468b21ab17215/raw/030827cb429ea69309d55e02a340c00b3a1f03b0/spintax.rb">here</a>. The generated spintax is very awful and required a lot of work to clean up&hellip;but AI is always advancing. Who knows what may come next?</p>
<p>I only used spintax only as a proof of concept of templating. I strongly disapprove of article spinning, and so does Google. That&rsquo;s why it has <a href="http://www.seoblog.com/2014/08/panda-update-effectively-killed-autoblogs/">attempted to penalize the practice</a> during its Panda updates to its search engine algorithm.</p>