Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="tracery.js"></script>
<title>Text Generator</title>
</head>
<body>
<button onclick="addNewTrace()">One more!</button>
<div id="main"></div>
<script type="text/javascript">
let rules = {
start: ["In the year 2025, #protagonist# must #action# #antagonist#."],
protagonist: ["#qty# #adj# #plnoun#", "one #adj# #noun#", "#protagonist# and #protagonist#"],
adj: ["beleaguered", "sad", "lonely", "angry", "tiny", "giant", "#adj# #adj#", "very #adj#"],
qty: ["three", "two", "twelve", "an army of"],
noun: ["man", "dolphin", "zookeeper", "scientist", "robot"],
plnoun: ["men", "dolphins", "zookeepers", "scientists", "robots"],
action: ["save #target# from", "eat", "destroy", "look deep within to understand", "risk #protagonist# to save"],
target: ["humanity", "the world", "one special lady", "snow white and the seven dwarves"],
antagonist: ["#protagonist#"]
}
let grammar = tracery.createGrammar(rules);
function addNewTrace() {
var trace = grammar.flatten('#start#');
$('#main').append($('<div>').text(trace));
}
addNewTrace();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.