People normally use context-free grammars to parse text, so I thought, what if I use it to CREATE text instead?

What it does

Based on a word bank and a grammar specification, it generates a stream of sentences by randomly selecting syntactical structures and words to choose from.

How I built it

I created a framework of recursive "rewrite rules" that, when applied, choose a random string or rewrite rule from a set of choices and applies it. This eventually leads to complete sentences and words.

Challenges I ran into

English grammar is complicated, so many context-sensitive transforms had to be made so that the syntax and semantics of the output wasn't completely nonsensical.

Accomplishments that I'm proud of

Some of the text actually made some sense.

What I learned

How to make a decent functional programming API in Python, since that was the best paradigm for this task.

What's next for Random Sentence Generator

Aside from more syntax and words to support, it is possible to manipulate the probability distributions of the rewrite rules so that sentences which make for a better story are chosen by the program. Machine learning could be useful here.

Share this project: