grammar icon indicating copy to clipboard operation
grammar copied to clipboard

A random sentence generator based on a context-free grammar (golang)

grammar

Random sentence generator based on a context-free grammar.

grammar randomly generates sentences based on a user-supplied context-free grammar.

Format

The definition consists of a series of newline-separated "rules." Each rule corresponds to a part of speech, and follows the syntax <part> <definition>. For example:

NPAdj Noun

In this rule, a noun phrase ("NP") is defined as consisting of an adjective followed by a noun. Any part of speech which is not later defined by a rule is considered a word on its own. For example:

NPAdj Noun
Adjblue
Adjred

This set of rules will defines "Noun" as its own word since it is nowhere defined. However, since "Adj" is defined, it will resolve to either "blue" or "red," but never the literal "Adj." (Note that definitions are processed serially, and thus parts of speech must be seen before they are defined) This example illustrates another feature of rules, which is that multiple rules are allowed, and will be chosen among randomly when resolving a part of speech.

Finally, the first line of the definition does not follow the normal syntax, but instead simply defines the structure of a whole sentence. For example:

Prep NP VP
NPAdj Noun
VPAdv Verb
Prepa
Prepthe
Adjblue
Adjred
Nounbird
Noundog
Advquickly
Advslowly
Verbran
Verbflew

This set of rules will create sentences such as "the red dog quickly flew."

Of course, grammar definitions are as flexible as any context-free grammar. Consider adding features such as plural vs singular phrases, or make up your own nonsensical definitions.