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 RiTa grammar, including transforms, sequences, and assignments.

rg = new RiGrammar(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 alternatively just 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();
rg.addRule("start", "( cat | dog | fish )");
let result = rg.expand();
Parameters
ObjectA JSON string, a JS object, or a Java Map
ObjectA context object (or Map in Java)
Returns
RiGrammara RiGrammar object
Syntax
// Initialize an empty RiGrammar object

rg = RiTa.grammar();


// Or specify a grammar in a JSON string (or JS object)

rg = RiTa.grammar(myJson);


// Or supply a context

rg = RiTa.grammar(myJson, context);
Platform Java / JavaScript