GithubHelp home page GithubHelp logo

vansul / ritajs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dhowe/ritajs-v2

0.0 0.0 0.0 6.23 MB

RiTa: generative language tools

Home Page: https://rednoise.org/rita

License: GNU General Public License v3.0

ANTLR 0.05% JavaScript 99.88% HTML 0.07% Shell 0.01%

ritajs's Introduction

Build Status npm version npm version CDNJS

RiTa: generative language tools for JavaScript

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.

Installation

  • For node: npm install rita
  • For browsers: <script src="https://unpkg.com/rita"></script>
  • For developers
  • For Java

Example (node)

let RiTa = require('rita');
console.log(RiTa.rhymes('sweet'));

let grammar = RiTa.grammar(jsonRules);
console.log(grammar.expand());

API

RiTa                                                      RiTa.Markov   RiTa.Grammar  
RiTa.addTransform()
RiTa.alliterations()
RiTa.analyze()
RiTa.concordance()
RiTa.conjugate()
RiTa.evaluate()
RiTa.hasWord()
RiTa.isAbbreviation()
RiTa.isAdjective()
RiTa.isAdverb()
RiTa.isAlliteration()
RiTa.isNoun()
RiTa.isPunctuation()
RiTa.isQuestion()
RiTa.isRhyme()
RiTa.isStopWord()
RiTa.isVerb()
RiTa.kwic()
RiTa.pastParticiple()
RiTa.phones()
RiTa.pos()
RiTa.posInline()
RiTa.presentParticiple()
RiTa.pluralize()
RiTa.randomOrdering()
RiTa.randomSeed()
RiTa.randomWord()
RiTa.rhymes()
RiTa.search()
RiTa.sentences()
RiTa.singularize()
RiTa.soundsLike()
RiTa.spellsLike()
RiTa.stem()
RiTa.stresses()
RiTa.syllables()
RiTa.tokenize()
RiTa.untokenize()
addText()
completions()
generate()
probability()
probabilities()
size()
toString()
toJSON()
fromJSON()









addRule()
addRules()
expand()
removeRule()
toJSON()
toString()
fromJSON()











 

RiTaScript

RiTaScript can be used as part of any grammar (via RiTa.Grammar) or can be run directly using RiTa.evaluate()

Choice

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" 
  ...

Weighted Choice

Assign probabilities to choice selection

The weather was (sad | gloomy [2] | depressed[4]).  ->  "The weather was depressed." 

Assignment

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 Assignment

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."

Transforms

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().

Conditionals

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.

 

Developing

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'

 

About

 

Environments

A simple sketch

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>

With node.js and npm

To install: $ npm install rita

let rita = require('rita');
let data = RiTa.features("The elephant took a bite!");
console.log(data);

 

Contributors

Code Contributors

This project exists only because of the people who contribute. Thank you!

Financial Contributors

ritajs's People

Contributors

cqx931 avatar dependabot[bot] avatar dhowe avatar kennyviperhk avatar real-john-cheung avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.