| Description |
Implementation of a (probabalistic) context-free grammar (with specific
literary extensions) that performs generation from user-specified grammars.
RiGrammar rg = new RiGrammar(this, "mygrammar.g");
System.out.println(rg.expand());
RiTa grammar files are plain text files (generally ending with the '.g'
extension and residing in the 'data' folder) that follow the format below:
{
<start>
<rule1> | <rule2> | <rule3>
}
{
<rule2>
terminal1 |
terminal2 | <rule1>
# this is a comment
}
...
Primary methods of interest:
Other items of note:
- Grammar files should be placed in the 'data' directory of a processing sketch,
along with images, fonts, and other resources.
- A RiGrammar object will assign (by default) equal weights to all choices in a rule.
One can adjust the weights by adding 'multipliers' as follows: (in the rule below,
'terminal1' will be chosen twice as often as the 2 other choices.
{
<rule2>
[2] terminal1 |
terminal2 | <rule1>
}
- The RiGrammar object supports callbacks, from your grammar, back into your Java code.
To generate a callback, add a method call in your grammar, surrounded by back-ticks, as follows:
{
<rule2>
The cat ran after the `getRhyme("cat");` |
The <noun> ran after the `pluralize(<noun>);`
}
Any number of arguments may be passed in a callback, but for each call,
there must be a corresponding method(with the same number and type or
arguments) in the sketch, e.g.,
String pluralize(String s) {
...
}
As of v81, the exec mechanism is enabled by default, so calls to
setExecEnabled() are no longer necessary. |