RiTa
index
Name RiGrammar
Description Implementation of a (probabilistic) 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:
  • expand() which simply begins at the <start> state and generates a string of terminals from the grammar.

  • expandFrom(String) which begins with the argument String (which can consist of both non-terminals and terminals,) and expands from there. Notice that expand() is simply a convenient version of expandFrom("<start>");.

  • expandWith(String, String) takes 2 String arguments, the 1st (a terminal) is guaranteed to be substituted for the 2nd (a non-terminal). Once this subsitution is made, the algorithm then works backwards (up the tree from the leaf) ensuring that the terminal (arg1) appears in the output string. For example, with the grammar fragment above, one might call:

          grammar.expandWith("hello", "<rule2>");
     
    assuring not only that <rule2>will be used at least once in the generation process, but that when it is, it will be replaced by the terminal "hello".
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.
Constructors
RiGrammar(parent, grammarFileName);
RiGrammar(parent);
Parameters
grammarFileName   grammar file in the 'data' directory
Methods
closeGrammarEditor()  

dumpDefinitions()   Prints the definition map to the console.

expand()   Expands a grammar from its '<start>' symbol one or more times.

expandFrom()   Expands the grammar from the given symbol, using a temporary buffer when preserveBuffer is true.

expandWith()   Expands the grammar after replacing an instance of the non-terminal productionName with the terminal in literalString.

This method guarantees that literalString will be present in the generated output, assuming at least one instance of productionName exists in the grammar.

getBuffer()   Return all the text generated since the last call to any of the expand() methods.

getDefinition()   Gets a production definition by name

getGrammarFileName()  

loadGrammarFile()   Loads a grammar from the file specified by grammarFileName, replacing any existing grammar file.

openGrammarEditor()  

setDefinition()   Adds a production definition by name, replacing the existing one it if it exists.

setGrammarFromString()   Initializes a grammar from a String containing the rules (rather than a file), replacing any existing grammar.

setIncludeSpaces()   Tells the grammar whether or not to add spaces between the terminals that are output (default=true).

Usage Web & Application