Tracery is a brilliant tool to more easily create text grammars and structure. In this lesson, we’ll create a bot that tweets out tiny stories.
We'll learn what a grammar is in this context, and how to create one with Tracery. We'll first create a simple story with character, action, place, and object variables, and learn how to add modifiers. Then, we'll create a more complex one, and learn how to set variables that we want to be consistent throughout the story, such as pronouns.
More information and resources on Tracery can be found at: https://github.com/galaxykate/tracery and http://www.crystalcodepalace.com/traceryTut.html
[00:00] In addition to Twit, we'll also need Tracery, which is required Tracery-Grammar. To create a story with Tracer, you need to create a grammar, which is a specific type of format. We'll say var grammar = tracery.createGrammar.
[00:20] This is just JSON data. Let's make a simple story to start. We'll have a character, and let's make a Kafkaesque bot. We will say our character is either K, the inspector, the director, the lawyer, or the officer. We'll have an action, we'll say walk, stroll, meander.
[00:53] We need a place, office, bank, court. We'll have an object, so paper, bribe, official. We need an origin, which is the starting symbol for our story. We create a structure here that incorporates all of our variables.
[01:19] We'll say character, and we wrap these in the hash symbol, so character action to the place for the object. To get a story from this, we say grammar.flatten and then pass it our starting symbol, which is origin. If we log out our story, K strolled to the bank for the official.
[01:55] We do it again, the officer walked to the court for the paper. That's a good start, but what are we missing? First, there're some modifiers we can add. We can say character.capitalize() if we always want that word to be upper case.
[02:11] What else is weird? The officer walked to the court. Well, we want action to be in the past, so we can add another modifier here, action.ed() and then down here, we'll say grammar.addModifiers(tracery.baseEngModifiers).
[02:35] Now if we run this, the officer walked to the bank for the official. The director meandered to the bank for the paper. The officer meandered to the office for the paper, etc. Object.a() will add a or an in front of our object.
[02:53] The inspector meandered to the bank for a bribe. K walked to the court for an official. We can also say object.s(), say to get some, and object.s() will pluralize our variable. The lawyer strolled to the court to get some officials. The inspector meandered to the bank to get some bribes.
[03:20] Let's create a slightly more complex story. Let's have three characters. We'll come back to actions in a minute. We can keep our place. Let's change this up a little bit.
[03:36] Let's delete this for now. Here we're going to nest variables a little bit, and we're going to have a story variable, which is the structure of the story. Here, we're going to have a setJob variable, and this will tie two variables together. For example, if the job is a lawyer, the job's actions could be argued in court, and filed some paperwork.
[04:07] If the job is the inspector, the actions might be talked with the lawyer and conducted meetings. The third job could be an officer, who arrested people, or stood in the courtroom.
[04:32] In origin, we're going to have our story variable. We'll say setJob to pick the job, and we'll say that the protagonist variable is the character. This will pick a character and a job, and fill these in with the story structure. For our story, let's say protagonist the job, went to the place every day, usually they actions. Let's run that.
[05:14] Oops, spelled protagonist wrong. Try again. I'm also missing another hash after setJob, and I'm also missing an actions here.
[05:33] There's usually some troubleshooting involved in generating these stories. Once it works, you can generate a ton of mini stories. Karl, the officer, went to the office every day, usually they arrested people. Karl, the lawyer, went to the court every day, usually they filed some paperwork.
[05:50] We can do one more thing to make the story a little more personal, and that is setPronouns. We need pronouns in a couple different settings. We need they, like they went to the store, which will be they. We need them, like he talked to them. We need their, like their coffee mug, and we need theirs, like it was theirs.
[06:25] Let's do that for he and she too. Remember to add the comma. We want to add our setPronouns in our origin. In our story, let's say, then they picked up their object.
[06:55] Ada, the lawyer, went to the court every day, usually they argued in court, then she picked up her letter. Let's loop through our story for other pronouns, and replace this one with the variable. If we run this, Karl, the lawyer, went to the office every day. Usually he filed some paperwork, then he picked up his bribe.
[07:16] Aiden, the inspector, went to the office every day. Usually, he talked with the lawyers, then he picked up his bribe. It's consistent, and now, we have a story generator.
[07:27] From here, all we have to say is bot.post Status/update with our status which is our story, error data response, if there's an error, log it out. Otherwise, bot has tweeted plus our story. We can see that our story has been tweeted.