Numb3rs Grammar

Base code for Tracery story grammar

by Kathleen Tuite

HTML

<script src="https://cdn.rawgit.com/galaxykate/tracery/master/js/tracery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/codemirror.min.js"></script>

<div class="col-lg-12">
    
    <h3>Numb3rs Grammar
        <small>by <a href="https://adamsmith.as/">Adam Smith</a></small></h3>
    
<!-- Put your tracery grammar inside this #source textarea!-->    
<textarea id="source">
{   
    "MATH": ["#CHARLIE# devises a mathematical approach to solving the crime and asks for more data.", "#CHARLIE# makes a math-based prediction."],
    
    "GATHER_EVIDENCE": ["#FBI_AGENT# gathers evidence."],
    
    "EVIDENCE_LOOP": ["#GATHER_EVIDENCE#", "#GATHER_EVIDENCE# #MATH#", "#GATHER_EVIDENCE# #MATH# #EVIDENCE_LOOP#"],
    
     
    "FIGHT_CRIME": ["#CORE_MEET_CRIME# #FAMILY_DOWN# #EVIDENCE_LOOP# #FAMILY_DOWN# #RESOLUTION#"],
    
    "RESOLUTION": ["#FBI_AGENT# and #FBI_AGENT# act on #CHARLIE#'s last prediction to finally discover how the crime really happened.", "#FBI_AGENT# and #FBI_AGENT# act on #CHARLIE#'s last prediction to stop crime before it could happen."],
    
    "FAMILY_MEMBERS": ["#CHARLIE#, #DON#, and #ALAN#"],
    "TV": ["the game"],
    "DRINK": ["beer", "wine", "sake"],
    "FOOD": ["a home cooked meal", "leftovers", "takeout"],
    "TOGETHERNESS_ACTIVITY": ["#FOOD#", "#DRINK#", "#TV#"],
    "FAMILY_TOGETHERNESS_SCENE": ["#FAMILY_MEMBERS# enjoy #TOGETHERNESS_ACTIVITY# and share kind words of understanding."],
    "RESOLVE_FAMILY": ["#FAMILY_UP# #FAMILY_TOGETHERNESS_SCENE#"],
    "FAMILY_DOWN": ["There is a scene where the family tension increases."],
    "FAMILY_UP": ["There is a scene where part of the family tension is relieved."],
    
    "CHARLIE_MEETS_CRIME": ["#DON# calls #CHARLIE# and informs him of the crime and #FBI_AGENT# provides details."],
    "FBI_MEETS_CRIME": ["#FBI_AGENT# receives a tip.", "#FBI_AGENT# talks about ongoing investigation.", "#FBI_AGENT# gets a call."],
    
    "OTHER_CHARACTER":...

CSS

@import url("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.0.0/codemirror.min.css");

.CodeMirror {
    border: 1px solid #888;
    height: auto;
}

JavaScript

var editor = CodeMirror.fromTextArea($('#source')[0], {
    lineNumbers: true,
    mode: {
        name: "javascript",
        json: true
    }
});

// tracery in action
function generate() {
    var source_text = editor.getValue();
    var grammar = tracery.createGrammar(JSON.parse(source_text));
    var example = grammar.flatten('#origin#');
    $('#example').text(example);
}

// when text changes, generate expansion
editor.on('change', generate);

// when button clicked, generate expansion
$("#generate").on('click', generate);

// Tracery takes some time to load. Wait a second, then try to generate
setTimeout(generate, 1000);