Reference

Class

RiTa

Name

grammar

Description Creates a new RiGrammar object: a probabilistic context-free grammar for text-generation. You may use any of the RiScript features in a grammar, including transforms, sequences, and assignments.

rg = RiTa.grammar(theRules);
result = rg.expand();

RiTa grammars are valid JSON formatted strings (or JS objects or Java Maps), as follows

{
"start": ( $rule1 | $rule2 | $rule3 ),

"rule2": ( terminal1 | terminal2 | $rule1 ),

...
}

If you're working in JavaScript, you can simply pass a JavaScript object instead. If you're not sure whether your grammar is valid JSON, check it at jsonlint.com.

Note: a RiGrammar object will assign (by default) equal weights to all choices in a rule. You can adjust the weights by adding 'multipliers' as described in the RiScript reference. For example, in the rule below 'terminal1' will be chosen twice as often as the two other choices.

rule2: ( terminal1 [2] | terminal2 | $rule1 )

Example
rg = RiTa.grammar(rulesObject);
result = rg.expand();

rg = RiTa.grammar();
rg.addRule("start", "( cat | dog | fish )");
let result = rg.expand();

rg = RiTa.grammar({"start", "( cat | dog | fish )");
let result = rg.expand();
Parameters
ObjectA JS object, a JSON string, or a Java Map representing the grammar rules (optional)
ObjectA context object (or Map in Java), optional
Returns
RiGrammara RiGrammar object
Syntax
// Initialize an empty RiGrammar object
rg = RiTa.grammar();

// Or specify a grammar in a JS object (or JSON string)
rg = RiTa.grammar(rules);

// Or optionally supply a context
rg = RiTa.grammar(rules, context);
Platform Java / JavaScript