Giter Site home page Giter Site logo

almasb / grammy Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 7.0 130 KB

A grammar generator based on tracery: https://github.com/galaxykate/tracery

License: Apache License 2.0

Kotlin 96.31% Java 3.69%
tracery tracery-grammar tracery-engine java kotlin grammar-rules grammar-parser text-generator text-expansion story-generator

grammy's Introduction

About

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

Maven 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!

Actions

Actions are a way to create and modify symbols at runtime.


The ! prefix means overwrite.

Input:

{
  "origin" : ["[name:Bob][!name:Adam]{story}"],
  "story" : ["{name}"]
}

Output:

Adam

The + prefix (or without a prefix) means add.

Input:

{
  "origin" : ["[name:Bob][name:+Adam]{story}"],
  "story" : ["{name}"]
}

Possible outputs are:

Adam
Bob

The - prefix means remove.

Input:

{
  "origin" : ["[name:Bob,Adam][name:-Adam]{story}"],
  "story" : ["{name}"]
}

Output:

Bob

Maven

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

Gradle

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

Notes

This implementation only loosely follows the original specification by Kate Compton and adds custom syntax for extra functionality. So, given the same data, the output may differ.

The word list is used from wordlist.

grammy's People

Contributors

almasb avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

grammy's Issues

Add modifier: optional

{key.optional}. After the tag is expanded, optional will with a 50% chance return the tag or ""

Rule distribution

By default it should be 1.0 / numItems chance for each rule. However, this can be altered by providing a value in the syntax, e.g. 0.5 for 50%, the rest of the rules will need to update accordingly. Requires new syntax

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.