RiTa is a toolkit for natural language and generative literature. It is implemented in Java and JavaScript, with a common API for both, and it is free/libre/open-source via the GPL license.
- For node:
npm install rita
- For browsers:
<script src="https://unpkg.com/rita"></script>
- For developers
- For Java
let RiTa = require('rita');
console.log(RiTa.rhymes('sweet'));
let grammar = RiTa.grammar(jsonRules);
console.log(grammar.expand());
RiTaScript can be used as part of any grammar (via RiTa.Grammar) or can be run directly using RiTa.evaluate()
Select a random choice from a group of options:
The weather was (sad | gloomy | depressed). -> "The weather was gloomy."
I'm (very | super | really) glad to ((meet | know) you | learn about you). -> "I'm very glad to know you."
Use the seq() transform to output the Choice options in a linear sequence:
The weather was (sad | gloomy | depressed).seq() ->
0) "The weather was sad"
1) "The weather was gloomy"
2) "The weather was depressed"
3) "The weather was sad"
...
Use the rseq() transform to output the Choice options in a randomized, non-repeating sequence:
The weather was (sad | gloomy | depressed).rseq() ->
0) "The weather was depressed"
1) "The weather was gloomy"
2) "The weather was sad"
3) "The weather was gloomy"
...
Use the norep() transform to ensure outputs never repeats:
The weather was (sad | gloomy | depressed).norep() ->
0) "The weather was depressed"
1) "The weather was gloomy"
2) "The weather was depressed"
3) "The weather was sad"
4) "The weather was gloomy"
...
Assign probabilities to choice selection
The weather was (sad | gloomy [2] | depressed[4]). -> "The weather was depressed."
Basic assignments do not have output, they simply create or update a variable to be used elsewhere (variables in JavaScript may also be used when passed in via the scripts 'context')
$desc=wet and cold
The weather was $desc -> "The weather was wet and cold"
Inline assignments allow one to easily set a variable, output it, and refer to it later:
Jane was from [$place=(New York | Berlin | Shanghai)].
$place is cold and wet.
-> "Jane was from Berlin. Berlin is cold and wet."
$place=(New York | Berlin | Shanghai)
$place is cold and wet in winter.
-> "Berlin is cold and wet in the winter."
In [$place=(New York | Berlin | Shanghai)] it is cold and wet in winter.
-> "In Berlin it is cold and wet in the winter."
Allow for modification of variables, choices, and raw text. RiScript comes with a number of useful transforms enabled (including pluralize(), capitalize(), and articlize()), which can be nested to create complex expressions. User-defined transforms can be added using RiTa.addTransform() or by passing a transform function as part of the script's 'context'.
How many (tooth | menu | child).pluralize() do you have?
How many (tooth | menu | child).pluralize().toUpper() do you have?
He grew up to be $animal.articlize().
He grew up to be (anteater).articlize().
He grew up to be (anteater).articlize().myCustomTransform().
Allow for conditional execution, based on the values of one or more variables
// 'desc' can be defined in JS or RS */
{desc='party'} The party was happening
{desc='party', user=$john} The party was happening and John was wearing $John.color.
To install/build the library and run tests (with yarn/mocha):
$ git clone https://github.com/dhowe/ritajs.git
$ cd ritajs
$ yarn install (run again if you get an error)
$ yarn build
$ yarn test.prod
If all goes according to plan, you should see a list of successful tests and find the library built in 'dist'
- Author: Daniel C. Howe
- Web Site: https://rednoise.org/rita
- Github Repo: https://github.com/dhowe/rita
- Issues: https://github.com/dhowe/rita/issues
- Reference: https://rednoise.org/rita/reference
Create a new file on your desktop called 'test.html' with the following lines, save and drag it into a browser:
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/rita"></script>
<script>
window.onload = function() {
let words = RiTa.tokenize("The elephant took a bite!");
$('#content').text(words);
};
</script>
<div id="content" width=200 height=200></div>
<html>
With p5.js
Create a new file on your desktop called 'test.html' and download the latest rita.js from here, add the following lines, save and drag it into a browser:
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.3/p5.min.js"></script>
<script src="https://unpkg.com/rita"></script>
<script>
function setup() {
createCanvas(200,200);
background(50);
textSize(20);
noStroke();
let words = RiTa.tokenize("The elephant took a bite!")
for (let i=0; i < words.length; i++) {
text(words[i], 50, 50 + i*20);
}
}
</script>
</html>
To install: $ npm install rita
let rita = require('rita');
let data = RiTa.features("The elephant took a bite!");
console.log(data);
This project exists only because of the people who contribute. Thank you!