GithubHelp home page GithubHelp logo

dhowe / ritajs Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 2.0 5.11 MB

RiTa for JavaScript

License: GNU General Public License v3.0

JavaScript 99.47% TypeScript 0.45% HTML 0.08%
creative-coding creative-writing generative-art natural-language-generation p5js-library natural-language writing

ritajs's Introduction

ritajs

ci tests npm version license

RiTa: tools for computational writing

RiTa is implemented in JavaScript and Java, with a common API for both, and is free/libre/open-source.

Features in v3.0

  • Smart lexicon search for words matching part-of-speech, syllable, stress and rhyme patterns
  • Fast, heuristic algorithms for inflection, conjugation, stemming, tokenization, and more
  • Letter-to-sound engine for feature analysis of arbitrary words (with/without lexicon)
  • Integration of the RiScript scripting language, designed for writers, now built with the blazing fast Chevrotain parser
  • New options for generation via grammars and Markov chains
  • Published in ESM, CommonJS and as an IIFE

Note: version 3.0 contains breaking changes -- please check the release notes

Installation

  • For esm: import { RiTa } from "https://esm.sh/rita";
  • For node: $ npm install rita
    let { RiTa } = require('rita');
  • For browsers: <script src="https://unpkg.com/rita"></script>
  • For p5.js: <script src="https://unpkg.com/rita"></script>
  • For developers

Example

import { RiTa } from "https://esm.sh/rita";

// to analyze a sentence
let data = RiTa.analyze("The elephant took a bite!");
console.log(data);

// to load a grammar
let grammar = RiTa.grammar(rulesObjectOrJSON);
console.log(grammar.expand());

API

RiTa RiMarkov RiGrammar
RiTa.addTransform()
RiTa.alliterations()
RiTa.analyze()
RiTa.concordance()
RiTa.conjugate()
RiTa.evaluate()
RiTa.grammar()
RiTa.hasWord()
RiTa.isAbbrev()
RiTa.isAdjective()
RiTa.isAdverb()
RiTa.isAlliteration()
RiTa.isNoun()
RiTa.isPunct()
RiTa.isQuestion()
RiTa.isStopWord()
RiTa.isRhyme()
RiTa.isVerb()
RiTa.kwic()
RiTa.markov()
RiTa.pastPart()
RiTa.phones()
RiTa.pos()
RiTa.posInline()
RiTa.presentPart()
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()
toJSON()
fromJSON()











addRule()
expand()
removeRule()
setRules()
toJSON()
fromJSON()













RiScript

RiScript (the minor language that powers RiTa) was designed specifically for writers working with code. RiScript primitives (choices, symbols, gates, transforms, etc) can be used as part of any RiTa grammar or executed directly using RiTa.evaluate. For more info, see this interactive notebook on observable.


 

Developing

To install/build the library and run tests (with npm/mocha and node v14.x):

$ git clone https://github.com/dhowe/ritajs.git
$ cd ritajs 
$ npm install
$ npm run build 
$ npm test

If all goes well, you should see a list of successful tests and find the library built in 'dist'


Please make contributions via fork-and-pull - thanks!


 

Visual Studio Code

Once you have things running with npm/mocha/tsup, you might also try VSCode.

Some of the following extensions may also be useful:

  • hbenl.vscode-mocha-test-adapter
  • hbenl.vscode-test-explorer
  • ms-vscode.test-adapter-converter

Here you can see the tests in the VSCode Testing view

vscode-tests

 

About

 

Quick Start

A simple browser 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://unpkg.com/rita"></script>
  <script>
    window.onload = function() {
      let words = RiTa.tokenize("The elephant took a bite!");
      document.getElementById("content").innerHTML = words;
    };
  </script>
  <div id="content" width=200 height=200></div>
<html>

An ESM browser sketch

Create a new file on your desktop called 'test.html' with the following lines, save and drag it into a browser:

<html>
<body>
  <div id="content" width=200 height=200></div>
  <script type="module">
    import { RiTa } from "https://esm.sh/rita";
    let words = RiTa.tokenize("The elephant took a bite!");
    document.getElementById("content").innerHTML = words;
  </script>
</body>
<html>

With p5.js

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://unpkg.com/p5"></script>
  <script src="https://unpkg.com/rita"></script>
  <script>
  function setup() {

    createCanvas(200,200);
    background(245);
    textAlign(CENTER);
    textSize(20);

    let words = RiTa.tokenize("The elephant took a bite!")
    for (let i=0; i < words.length; i++) {
        text(words[i], 100, 50 + i*20);
    }
  }
  </script>
</html>

If you already have a sketch, simply add <script src="https://unpkg.com/rita"></script> to your index.html to include RiTa.

With node.js and npm

To install: $ npm install rita

let RiTa = require('rita');
let data = RiTa.analyze("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

dhowe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ritajs's Issues

Small fix to p5.js editor example (Random Rhymes)

The official Rita.js "Random Rhymes" example currently throws an error (using Rita 3.1.1), TypeError: tmp.join is not a function. This is because the sketch is using the async version of rhymes().

For a quick and perfectly satisfactory fix, change rhymes() to rhymesSync() in line 29. The sketch performs flawlessly thereafter.

Might be a good opportunity to update p5.js to version 1.9.1+ in the index.html, while you're at it.

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.