Discovering Textual Structures: Generative Grammar Induction using Template Trees

Thomas Winters, Luc De Raedt

Abstract

Text generation for creative applications often relies on hand-crafted grammars to maintain control and interpretability, but manual authoring is labor-intensive. This paper introduces GITTA, an algorithm that automatically induces non-recursive, interpretable context-free grammars from small sets of text examples using a novel "template tree" structure. By bridging the gap between the predictability of rule-based systems and the automation of machine learning, GITTA enables more efficient human-AI co-creation of generative text models.

Introduction

In computational creativity—such as generating poetry, game content, or chatbots—designers require fine-grained control over output. While modern neural text generators produce impressive results, they often function as unpredictable "black boxes." Conversely, rule-based approaches like the Tracery language offer absolute control but are tedious to author manually. GITTA addresses this by learning interpretable, rule-based generative grammars directly from a few examples of desired text.

Methodology

GITTA (Grammar Induction using a Template Tree Approach) learns non-recursive Context-Free Grammars (CFGs) through a three-step process:

  1. Template Trees: The algorithm constructs a directed acyclic graph where leaves represent raw input sentences. Nodes higher in the tree represent increasingly generalized templates.
  2. Bottom-Up Merging: Using an adapted Wagner-Fischer algorithm for longest common subsequences, GITTA iteratively merges similar strings. When differences are found, it inserts "slots" (variables) to create a generalized parent template.
  3. Grammar Conversion: Redundant nodes are pruned, and similar slots are merged based on independence assumptions. The tree is then collapsed into standard production rules (e.g., S -> I like <T>).
A template tree structure showing how generalized templates (e.g., "hello <B>") merge into a root node <A> to represent multiple sentences.

Unlike standard grammar induction that focuses on parsing or recursion, GITTA intentionally avoids recursion. This ensures the generation of finite, controllable text lengths, which is a desirable constraint in creative contexts.

Experimental Setup

To evaluate the algorithm, the authors reverse-engineered 10 human-made Tracery grammars sourced from BotWiki and CheapBotsDoneQuick. These original grammars were used to generate training datasets of 25, 50, and 100 sentences, which served as the input for GITTA to attempt to reconstruct the underlying logic.

Key Results and Insights

GITTA demonstrated a high generative yield, successfully inducing grammars that could produce thousands of valid sentences from very few examples. For example, with only 100 training examples from the "botdoesnot" grammar, GITTA induced a grammar capable of generating 1,596 valid outputs with minimal errors.

Performance metrics for GITTA across 10 human-authored grammars, showing generative yield and error rates for different training set sizes.

Key Insights: * Non-Recursion as a Feature: Non-recursive grammars define a finite, controllable space of interesting texts, avoiding the risk of infinite loops. * Effective Variable Isolation: Template trees successfully isolate independent slot values (like lists of nouns) from static structural text. * Few-Shot Viability: A few dozen examples are often sufficient to reverse-engineer the core logic of a human-designed template.

Limitations and Future Work

GITTA assumes strict independence between slot values. If a template contains hidden dependencies (e.g., subject-verb agreement), the algorithm may overgeneralize and produce grammatically inconsistent results. Additionally, it cannot currently learn recursive structures.

Future work includes extending GITTA to learn rule probabilities and non-terminal constraints. The ultimate goal is to integrate GITTA into collaborative UIs where designers can provide a few examples and then manually refine the AI-generated grammar prototype.

Summary updated

BibTeX
@inproceedings{winters2020discovering,
  author = {Thomas Winters and Luc De Raedt},
  title = {{Discovering Textual Structures: Generative Grammar Induction using Template Trees}},
  booktitle = {ICCC},
  year = {2020},
  url = {https://openreview.net/forum?id=FCvAXvobr07},
}
Figure