Git Product home page Git Product logo

grammy's Introduction

About

This is a JVM port of Kate Compton's story-grammar generator - Tracery

Maven Build Status codecov

Usage

Example 1

Given the following JSON:

{
  "emotion" : ["happy", "sad", "proud"]
}

We can create Grammar as follows:

String json = ...
Grammar grammar = Grammy.createGrammar(json);

String output = grammar.flatten("emotion");

// output is one of "happy", "sad" or "proud"

Example 2

Input:

{
  "sentence": ["The {color} {animal} of the {natureNoun} is called {name}"],
  "color": ["orange","blue","white","black","grey","purple","indigo","turquoise"],
  "animal": ["unicorn","raven","sparrow","coyote","eagle","owl","zebra","duck","kitten"],
  "natureNoun": ["ocean","mountain","forest","cloud","river","tree","sky","sea","desert"],
  "name": ["Arjun","Yuuma","Darcy","Mia","Chiaki","Izzi","Azra","Lina"]
}

Code:

Grammar grammar = ...
grammar.flatten("sentence");

Possible Output:

The orange zebra of the sky is called Mia

Example 3

Input:

{
  "origin" : ["{[helper:{name}]story}"],
  "story" : ["{greet.capitalize}! {introduce.optional(75)} {ask} {goodbye}", "{ask} {goodbye}"],
  "greet" : ["hi", "hello", "hi there", "hey", "good day"],
  "introduce" : ["My name is {helper.capitalize}. I'm your {helperJob} for today.", "The name's {helper.capitalize}. How do you do? I'll be your {helperJob} today.", "I'm {helper.capitalize} and I'm your {helperJob}."],
  "helperJob" : ["{jobType.optional(50)} assistant"],
  "jobType" : ["digital", "virtual"],
  "ask" : ["How {can} I help you?", "How {can} I be of service?", "What {can} I do for you?"],
  "can" : ["can", "may", "might"],
  "name" : ["Alice", "Bob", "Carl", "David", "Eve", "Francis", "Gerard", "Helen", "Ian", "Jenny"],
  "goodbye" : ["You can ask for {helper} next time you need help. {bye.capitalize}!"],
  "bye" : ["bye", "goodbye", "until next time", "see you", "take care"]
}

Code:

Grammar grammar = ...
grammar.flatten();

While the possible outputs follow the same (defined) structure, there's a great variety in what can be achieved:

Good day! My name is Helen. I'm your virtual assistant for today. What may I do for you? You can ask for Helen next time you need help. Take care!
Hey! How can I be of service? You can ask for Ian next time you need help. Bye!
Hi! The name's Helen. How do you do? I'll be your digital assistant today. What may I do for you? You can ask for Helen next time you need help. Goodbye!
How can I help you? You can ask for Eve next time you need help. Until next time!
What can I do for you? You can ask for Eve next time you need help. Until next time!
Good day! How may I help you? You can ask for Eve next time you need help. Goodbye!
Hi there! The name's Helen. How do you do? I'll be your virtual assistant today. What may I do for you? You can ask for Helen next time you need help. Take care!
How can I help you? You can ask for Bob next time you need help. Goodbye!
Hello! My name is Francis. I'm your digital assistant for today. How might I help you? You can ask for Francis next time you need help. Bye!
What can I do for you? You can ask for Jenny next time you need help. See you!
What can I do for you? You can ask for Helen next time you need help. Goodbye!
Hey! My name is Francis. I'm your virtual assistant for today. How can I be of service? You can ask for Francis next time you need help. See you!
How can I be of service? You can ask for Eve next time you need help. Goodbye!
What might I do for you? You can ask for David next time you need help. Take care!
How can I help you? You can ask for Francis next time you need help. Bye!
How can I be of service? You can ask for Alice next time you need help. See you!
Good day! I'm Helen and I'm your assistant. How can I help you? You can ask for Helen next time you need help. Until next time!
How may I be of service? You can ask for Francis next time you need help. See you!
How might I be of service? You can ask for Ian next time you need help. Until next time!
Hi there! How can I be of service? You can ask for Carl next time you need help. See you!
Hi there! My name is Alice. I'm your assistant for today. How might I help you? You can ask for Alice next time you need help. Take care!
How may I help you? You can ask for Alice next time you need help. Goodbye!
Hey! The name's Bob. How do you do? I'll be your virtual assistant today. How can I be of service? You can ask for Bob next time you need help. Until next time!
What may I do for you? You can ask for Gerard next time you need help. See you!
Hello! The name's Alice. How do you do? I'll be your digital assistant today. How may I be of service? You can ask for Alice next time you need help. Until next time!

Maven

<dependency>
    <groupId>com.github.almasb</groupId>
    <artifactId>grammy</artifactId>
    <version>0.0.2</version>
</dependency>

Gradle

compile 'com.github.almasb:grammy:0.0.2'

Uber-jar

Pre-compiled version available from Releases.

Notes

This implementation only loosely follows the original specification by Kate Compton. So, given the same data, the output may differ.

The word list is used from wordlist.

Contact

Gmail

grammy's People

Contributors

almasb avatar

Watchers

James Cloos 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.