Projects

GITTA

Discovering textual structures to create generators

Code Cite

GITTA cover

GITTA (“Grammar Induction using a Template Tree Approach”) is a method for inducing context-free grammars from a few example texts. It works best on texts that follow hidden templates, such as forum topics, writing prompts and the output of template-based text generators. The grammars it finds can be converted for grammar languages such as Tracery and Babbly.

Why templates?

People who make text generators, such as Twitterbots, often write their grammars by hand: a template such as “<Hero> is going to <Action>”, with lists of names, jobs and actions to fill its slots. GITTA goes the other way: given a few texts such a grammar wrote, it looks for the grammar behind them. Most grammar induction algorithms look for structures like parts of speech, but generative grammars are mostly templates, so GITTA looks for templates.

How it works

GITTA arranges the texts in a template tree: every node is a template that is more general than the templates under it, with the texts themselves at the bottom.

< A >
< B > world
hi < C >
< D > universe
hello world
hi world
hi universe
howdy universe
  • < A >
    • < B > world
      • hello world
      • hi world
    • hi < C >
      • hi world
      • hi universe
    • < D > universe
      • hi universe
      • howdy universe

A template tree for four greetings. Each template covers the texts under it, so hi world fits both <B> world and hi <C>, and the one at the top covers them all. From these four greetings, GITTA learns a grammar that also writes hello universe and howdy world.

GITTA builds this tree from the bottom up: it joins the two closest texts into a template, keeping the words they share and putting a slot where they differ, and then keeps joining the closest templates until one covers everything. It then merges slots with similar values and simplifies the tree until nothing changes any more. Step through it for five sentences:

Join the closest texts, then the closest templates: the words they share stay, the rest becomes a slot.

< A >
I like my < B > and my < C >
< D > the < E > is < F >
I like my cat and my dog
I like my dog and my chicken
Alice the cat is jumping
Cathy the cat is walking
Bob the dog is walking
< G > the cat is < H >
< I > the < J > is walking
  • < A >
    • I like my < B > and my < C >
      • I like my cat and my dog
      • I like my dog and my chicken
    • < D > the < E > is < F >
      • < G > the cat is < H >
        • Alice the cat is jumping
        • Cathy the cat is walking
      • < I > the < J > is walking
        • Cathy the cat is walking
        • Bob the dog is walking

Slots

< A >
I like my < B > and my < C > | < D > the < E > is < F >
< B >
cat | dog
< C >
dog | chicken
< D >
< G > | < I >
< E >
cat | < J >
< F >
< H > | walking
< G >
Alice | Cathy
< H >
jumping | walking
< I >
Cathy | Bob
< J >
cat | dog

Merge slots with similar values: the animals become B, the names G and the verbs F.

< A >
I like my < B > and my < B >
< G > the < B > is < F >
I like my cat and my dog
I like my dog and my chicken
Alice the cat is jumping
Cathy the cat is walking
Bob the dog is walking
< G > the cat is < F >
< G > the < B > is walking
  • < A >
    • I like my < B > and my < B >
      • I like my cat and my dog
      • I like my dog and my chicken
    • < G > the < B > is < F >
      • < G > the cat is < F >
        • Alice the cat is jumping
        • Cathy the cat is walking
      • < G > the < B > is walking
        • Cathy the cat is walking
        • Bob the dog is walking

Slots

< A >
I like my < B > and my < B > | < G > the < B > is < F >
< B >
chicken | cat | dog
< F >
walking | jumping
< G >
Alice | Bob | Cathy
< C >
< B >
< D >
< G >
< E >
< B >
< H >
< F >
< I >
< G >
< J >
< B >

Simplify: a template that adds nothing to the one above it goes. Repeat until nothing changes.

< A >
I like my < B > and my < B >
< G > the < B > is < F >
I like my cat and my dog
I like my dog and my chicken
Alice the cat is jumping
Cathy the cat is walking
Bob the dog is walking
  • < A >
    • I like my < B > and my < B >
      • I like my cat and my dog
      • I like my dog and my chicken
    • < G > the < B > is < F >
      • Alice the cat is jumping
      • Cathy the cat is walking
      • Bob the dog is walking

Slots

< A >
I like my < B > and my < B > | < G > the < B > is < F >
< B >
chicken | cat | dog
< F >
walking | jumping
< G >
Alice | Bob | Cathy

What is left is a grammar. It writes 27 sentences, 22 of them new.

Grammar

< S >
I like my < B > and my < B > | < G > the < B > is < F >
< B >
chicken | cat | dog
< F >
walking | jumping
< G >
Alice | Bob | Cathy

All 27 sentences it writes

  • I like my chicken and my chicken
  • I like my chicken and my cat
  • I like my chicken and my dog
  • I like my cat and my chicken
  • I like my cat and my cat
  • I like my cat and my dog
  • I like my dog and my chicken
  • I like my dog and my cat
  • I like my dog and my dog
  • Alice the chicken is walking
  • Alice the chicken is jumping
  • Alice the cat is walking
  • Alice the cat is jumping
  • Alice the dog is walking
  • Alice the dog is jumping
  • Bob the chicken is walking
  • Bob the chicken is jumping
  • Bob the cat is walking
  • Bob the cat is jumping
  • Bob the dog is walking
  • Bob the dog is jumping
  • Cathy the chicken is walking
  • Cathy the chicken is jumping
  • Cathy the cat is walking
  • Cathy the cat is jumping
  • Cathy the dog is walking
  • Cathy the dog is jumping

GITTA’s steps for five sentences, as in the talk at ICCC 2020. The sentences it learns from are on the light sky, and the slots a step changed are on the sun.

GITTA can thus discover the patterns in a few texts and use them to write many similar texts.

Results

To test it, we let GITTA reverse-engineer ten Twitterbots made with Tracery, from 25, 50 or 100 of their texts, and counted what the grammars it found could write. From 25 texts, the grammars of seven of the bots wrote only texts the bot could have written itself, and often many more than GITTA had seen: 648 for @botdoesnot, and over 3,000 for @Whinge_Bot, with 6 wrong. From 100 texts of @SkoolDetention, it found a grammar that writes all 224 texts that bot can write, along with 29 it can’t. GITTA struggled with slots right after each other, especially when their values differ a lot in length: for @hometapingkills, its grammars wrote more wrong texts than right ones from 50 texts on.

Try it

You can run a demo of GITTA in Google Colab, and its code is on GitHub.

Talks about GITTA

Papers

Citation

 

What people say about GITTA

How to cite

Discovering Textual Structures: Generative Grammar Induction using Template Trees

@article{winters2020gitta,
    title={Discovering Textual Structures: Generative Grammar Induction using Template Trees},
    author={Winters, Thomas and De Raedt, Luc},
    journal={Proceedings of the 11th International Conference on Computational Creativity},
    pages = {177-180},
    year={2020},
    publisher={Association for Computational Creativity}
}

All projects