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 JS objects (or valid JSON formatted strings, or Java Maps), as follows

{
start: [ $rule1 | $rule2 | $rule3 ],

rule2: [ terminal1 | terminal2 | $rule1 ],

...
}

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();
Parameters
ObjectA JS object, a JSON string, or a Java Map representing the grammar rules (optional)
ObjectA context object (or Map in Java), optional
Object
(or Map in Java)
(optional) options

{boolean} options.trace:
if true, output verbose log to console for debugging (default=false)

{boolean} options.onepass:
if true, run only one pass through the parser (default=false)

{boolean} options.v2Compatible:
if true, use RiScript v2-compatible syntax (default=false)
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