Giter VIP home page Giter VIP logo

rita4j's Introduction

Maven Central npm version

RiTa: tools for generative natural language

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

Features in v2.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
  • Powerful new options for generation via grammars and Markov chains

Note: Version 2.0 contains breaking changes! Please check the release notes

Installation

<dependency>
  <groupId>org.rednoise</groupId>
  <artifactId>rita</artifactId>
  <version>2.4</version>
</dependency>

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()
toString()
toJSON()
fromJSON()











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













RiScript

RiScript is a simple, writer-focused scripting language included in RiTa. It enables several generative primitives within plain text for expansion at runtime. RiScript primitives can be used as part of any RiTa grammar or executed directly using RiTa.evaluate(). For documentation, see this interactive notebook.




Developing

$ git clone https://github.com/dhowe/rita4j.git
$ cd rita4j
$ mvn install      # when done, you should see "BUILD SUCCESS"

The project requires a minimum version of Java 8 and Maven 3.6 to build.

Eclipse

  1. Do steps above under Developing
  2. In eclipse, File->Import->Maven->Existing Maven Projects and select your 'rita4j' folder
  3. Right-click on project, and select 'Run-as' -> 'Maven install' or 'JUnit tests'

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



A Simple Example (Java)

    For online examples in JavaScript, see this page

  1. Create a new Java project in Eclipse (or your IDE of choice)
  2. Download rita.jar and add it to the build path for the project. In eclipse: 'Project' > 'Properties' > 'Java Build Path' > 'Libraries' > 'Add External JARs...'
  3. Create and run a new class, SimpleExample.java, with the following code:
import rita.*;

public class SimpleExample {

  public static void main(String[] args) {

    System.out.println(RiTa.analyze("The elephant took a bite!"));
  }
}

In Processing

To install:

  1. Open Processing and select 'Sketch' menu > 'Import Library...' > 'Add Library...'
  2. Search for 'RiTa' and then install it

Create an example sketch as follows (and/or see the included examples):

import rita.*;
import java.util.*;

void setup() {

  size(600, 200);
  background(50);
  textSize(20);
  noStroke();

  Map data = RiTa.analyze("The elephant took a bite!");

  float y = 15;
  for (Iterator it = data.keySet().iterator(); it.hasNext();) {
    String key = (String) it.next();
    text(key + ": " + data.get(key), 25, y += 26);
  }
}

Contributors

Code Contributors

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

Financial Contributors

rita4j's People

Contributors

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rita4j's Issues

Fix Action: Pub to Central

gpg: directory '/home/runner/.gnupg' created
gpg: keybox '/home/runner/.gnupg/pubring.kbx' created
gpg: no default secret key: No secret key
gpg: signing failed: No secret key
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.769 s
[INFO] Finished at: 2020-11-18T04:30:35Z
[INFO] ------------------------------------------------------------------------
Error: Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (sign-artifacts) on project rita: Exit code: 2 -> [Help 1]
Error:
Error: To see the full stack trace of the errors, re-run Maven with the -e switch.
Error: Re-run Maven using the -X switch to enable full debug logging.
Error:
Error: For more information about the errors and possible solutions, please read the following articles:
Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Error: Process completed with exit code 1.
##[debug]Finishing: Publish to the Maven Central Repository

Bug in about reading children in Markov.toString()

if the code don't visit (e.g. print to console) this.root.children and the last word's children before calling Node.asTree(), the children will be missing somehow causing problems in calculating the probability

Markov Workbench

Should be a single .html page with (jquery is fine):

  1. 1 div/text-area for input text
  2. 1 div (no text-area needed) for output
  3. 2 buttons (one for 'load', one for 'execute')
  4. One knob called 'temperature' (0-1, default value is 0)
  5. One knob called 'speed' (0-1, default value is .5)
  6. One pulldown or knob called 'N' (1-5, default value is 3, integers only)

The 'input' text area should have a vertical slider if text is longer than window and should wrap at the width. The output div should auto-wrap at the width of the window. New lines will appear at top (don't worry about this for now)

  • ‘load’ can handle local-files only for now (should bring up the finder/explorer)
  • ‘execute’ should be disabled if there is no text in the input text-area (top)

Add Android test app

We need to verify the library can be used on Android as well. I'm not sure the best way to do this with maven, but we certainly don't want to open android studio

Problem in Riscript - transform

is add to knownIssues by #53
transform will fail when assign and choice is use together, e.g
$a=a\n($a).toUpperCase() and $a=a\n($a | $a).toUpperCase()

this problem cause many fails in Grammar and Riscript

seems that with choice, transform is wrongly added before the choice is done
it might be some problem in the tree...
@dhowe maybe you will want to look into this

more info in the code comment in KnownIssues

kwic parameters

in rita.js

kwic: function(word, numWords) {
..
}

int Rita

String[] kwic(String text, String word, Map<String, Object> opts)
or
String[] kwic(String text, int numWords, Map<String, Object> opts)

?

Inline posTags() return type

in rita-script
normally posTags will return String Array
RiTa.posTags("He outnumbers us") ===> ["prp", "vbz", "prp"]

and for { inline: true } will return String
RiTa.posTags("asdfaasd", { inline: true }) ===> "asdfaasd/nn"

are we doing the same in RiTa Java? or we should align both to return String Array?

toPhoneArray test

in toPhoneArray test

should this be public or private or test in another way?

let result = RiTa._lexicon().toPhoneArray(RiTa._lexicon()._rawPhones("tornado"));     eql(result, ['t', 'ao', 'r', 'n', 'ey', 'd', 'ow'], 'got:' + result);

isAlliteration, isRhyme

is third parameter optional?

isRhyme(String word1, String word2, boolean useLTS)
isAlliteration(String word1, String word2, boolean useLTS)

if yes, what is default (true or false)

Remaining Problems in RiScript

  • dealing with line breaks like \n
  • dealing with rules starting with number, like $1foo
  • throw on bad choice
  • dealing with Rita function transform e.g. using RiTa.env() in the input -> on hold
  • Custom transform using RiTa.addTransform()
  • dealing with norep()
  • rseq() -> the dont repeat part only
  • seq()
  • using assignment with symbol in context refer to #64 (grammar-tests)

RiTa.evaluate()/riscript.eval()

In js

RiTa.evaluate('I ate the\n$dog.', { dog: 'lab' }, { trace: 0 })

or

let ctx = { bar: { color: 'blue' } };
RiTa.evaluate('$foo=$bar.color\n$foo', ctx, { trace: 0 })

What's the best way to define ctx in java? It could be any data type in js...

LexParser.java is empty at the moment.

Util.shuffle(results, RiTa);

in js

Util.shuffle(results, RiTa);

What is the second parameter in JAVA and the usage of it?

*in Lexicon.java

Finish Java classes and tests

  • Tokenizer
  • Markov (completions(), probability(), probabilities())
  • Grammar (on hold)

For each class above, do the following:

  1. make sure tests are in sync with JS
  2. implement functions to make tests pass
  3. ask questions about the best way to do things in Java
  4. make sure code compiles and is correctly formatted (using this formatter)

RE Class?

do we need in rita2?
opening a new re.java?

class RE {

  constructor(regex, offset, suffix) {
    this.raw = regex;
    this.regex = new RegExp(regex);
    this.offset = offset;
    this.suffix = suffix || '';
  }

  applies(word) {
    return this.regex.test(word);
  }

  fire(word) {
    //return this.truncate(Utils.trim(word)) + this.suffix;
    return this.truncate(word) + this.suffix;
  }

  analyze(word) {
    return this.suffix != '' && word.endsWith(this.suffix);
  }

  truncate(word) {
    return (this.offset === 0) ? word : word.substr(0, word.length - this.offset);
  }

  toString() {
    return '/' + this.raw + '/';
  }
}

Add tests for all forms of RandGen.randomOrdering

Just a simple test or two for each should be fine:

public static final <T> List<T> randomOrdering(List<T> list);
public static final float[] randomOrdering(float[] arr);
public static final boolean[] randomOrdering(boolean[] arr);
public static final double[] randomOrdering(double[] arr);
public static final int[] randomOrdering(int[] arr);
public static final <T> T[] randomOrdering(final T[] arr);

Questions on assertThrows()

When I'm working on RiScriptTests.java I was trying to find the best way to translate the following in js:
expect(() => RiTa.evaluate('{$a<} foo', { a: 2 }, { silent: 1 })).to.throw();

For some of the throw error tests I have used the following syntax that is available in JUnit 4.12:

@Test(expected = RiTaException.class)
	public void testBadConditionals() {
		Map<String, Object> ctx = opts();
		ctx.put("a", 2);
		RiTa.evaluate("{$a<} foo", ctx, opts("silent", true));
	}

But I'm not sure what's the best method I could use for multiple throw errors asserts within one test case.
Example: https://github.com/cqx931/rita2/blob/master/src/test/java/rita/test/RiScriptTests.java#L633

We are now using JUnit 4.12 and JUnit 5 Jupiter assertions API introduces the assertThrows method for asserting exceptions that seem to be very easy to use. Could we switch to JUnit 5?

Reference:
https://www.baeldung.com/junit-assert-exception

Match tests with rita2js

  • riscript-tests.js 61 (priority)
  • analyzer-tests.js 7
  • conjugator-tests.js 4
  • grammar-tests.js 15
  • lexicon-tests.js 25
  • markov-tests.js 25
  • rita-tests.js 6
  • tagger-tests.js 14
  • tokenizer-tests.js 3

Total: 160

Make all tests pass

  • tagger-tests.js (sc)
  • tokenizer-tests.js (sc)
  • conjugator-tests.js (sc)
  • markov-tests.js (sc)
  • analyzer-tests.js (dh)
  • lex-loader-tests.js (dh)
  • lexicon-tests.js (dh)
  • riscript-tests.js (dh)
  • grammar-tests.js (dh)
  • rita-tests.js

MarkovTests.callGenerate error (very occasional error)

[ERROR] Tests run: 26, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.43 s <<< FAILURE! - in rita.test.MarkovTests
[ERROR] rita.test.MarkovTests.callGenerate Time elapsed: 0.099 s <<< ERROR!
rita.RiTaException: [Markov] Exceeded maxAttempts:99
at rita.test.MarkovTests.callGenerate(MarkovTests.java:231)

Integrate generated docs into website

To generate docs (for now):

  1. Right-click on docgen.DocGenerator.java and select 'run-as-application'
  2. This should generate the docs in /web/reference

Note: the html here was converted from php so may need some cleanup. It also doesn't include RiGrammar or RiMarkov

Synchronize test naming between java and js

We need to do better with the test naming. See example below.

Given the following JS-tests

describe('Symbol',` () => {
    it('Should eval linebreak-defined variables', () => {
       ... 
    });

    it('Should return input for undefined', () => {
       ... 
    });
})

Map to Java tests as follows:

@Test
public void evalLinebreakDefinedVariablesSYMBOL() {
  ... 
}

@Test
public void returnInputForUndefinedSYMBOL() {
  ...
}

Support for Android

We need to verify the library can be used on Android (with and without processing) -- I'm not sure the best way to do this with junit/maven... we may need a separate way to test

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.