Tutorial > Generating with RiGrammar


RiGrammar is a probabilistic context-free grammar with literary extensions for text-generation. This is a rather formal definition, but for this tutorial, it just means that we can use it to generate texts according to simple rules that we define.

For example, if we want to generate simple sentences following a Subject-Verb-Object pattern, the RiGrammar might look like this:



  <start>:    <subject> <verb> <object>
  <subject>:  I | You | They
  <object>:   coffee | bread | milk
  <verb>:     want | hate | like | love

One generation from this grammar would be I hate milk. Another would be They want bread.

The first line above defines the whole structure of your grammar. In this case, it will generate a <subject>, followed by a <verb> and then an <object>. For each part, RiTa looks into the grammar for the corresponding rule and then chooses one string from the options listed.

The grammar above is written as JSON. RiTa accepts both JSON and YAML formatted strings (or objects) for grammars. Below is our example grammar using native YAML format:


---
  <start>:
      - <subject>
      - <verb>
      - <object>
  <subject>:
      - I
      - You
      - They
  <object>:
      - coffee
      - bread
      - milk
  <verb>:
      - want
      - hate
      - like
      - love

To run the grammar, you can use either the expand() or expandFrom() functions...

The former, expand() runs the grammar from the <start> symbol, while expandFrom(), starts from whatever rule you pass in -- actually expand() is just a convenient version of expandFrom('<start>').


  riGrammar.expandFrom('<start>');

By default, a RiGrammar object will assign equal weights to all choices in a rule. However, one can adjust the weights by adding 'multipliers' as follows:


  <object>: coffee[2] | bread | milk

This says that the word “coffee” should be chosen, on average, twice as often as either “bread” or “milk”.

We can also make the grammar more complex by adding rules within rules:



  <start>:    <subject> <verb> <object>
  <subject>:  I | You | They
  <object>:   <food> | <animals>
  <verb>:     want | hate | like | love
  <food>:     coffee | bread | milk
  <animals>:  cats |dogs | fish

Now you might get You love coffee or They want fish ...

To get a better sense of how it all works, check out this example sketch using RiTa grammars...


Aside: its important to note that using rules related to English grammar (subject, verb, etc. as in our example above) is just one one of many ways to use grammars. One can construct rules based on any textual features (syllable-count, as in the linked example, or phonemes as we might in a rhyming grammar), or even use grammars to generate image or sound.