Permalink
Browse files

First pass on corpus support.

See documentation. This is a bit hacky, but will only operate on
passages tagged “corpus”.
  • Loading branch information...
Matthew Balousek
Matthew Balousek committed Sep 12, 2016
1 parent 9d2442a commit f08034585057f8ea35f8bb6166349411aeecf464
Showing with 118 additions and 8 deletions.
  1. +28 −3 README.md
  2. +90 −5 twinecery.txt
View
@@ -25,7 +25,8 @@ If you run into any problems with twinecery, or this guide, or if you just want
4. [With an Internally-Acting Link](#3iv-with-an-internally-acting-link)
4. [Importing and Exporting Grammars](#4-importing-and-exporting-grammars)
1. [Importing JSON](#4i-importing-json)
- 2. [Exporting JSON](#4ii-exporting-json)
+ 2. [Importing Corpora](#4ii-importing-corpora)
+ 3. [Exporting JSON](#4iii-exporting-json)
## 1. GETTING STARTED
First, create a new story in Twine or open an existing .tws file. In the application menu, go to **Story > Special Passages > StoryIncludes**. Add the following to that passage:
@@ -220,7 +221,30 @@ If you want to bring that JSON into Twine in order to edit it more, you can use
...and then you can save that to a plaintext file and import it into your .tws file via **File > Import > Twee Source Code** for further editing.
-### 4.ii. Exporting JSON
+### 4.ii. Importing Corpora
+There are lots of cool sources of data out there. For example, [dariusk/corpora]. If you create a passage and tag it `corpus`, twinecery will do some importing for you.
+
+On each line of the corpus passage, include a URL to a data source and path of properties. The goal is to get an array of strings. The URL comes first, and the path is separated by `#` symbols.
+```
+:: dog [corpus]
+https://rawgit.com/dariusk/corpora/master/data/animals/dogs.json#dogs
+
+:: dinosaur [corpus]
+https://rawgit.com/dariusk/corpora/master/data/animals/dinosaurs.json#dinosaurs
+```
+If there are multiple corpuses, they'll be concatenated. The name of the symbol is the name of the passage.
+```
+:: animal [corpus]
+https://rawgit.com/dariusk/corpora/master/data/animals/dogs.json#dogs
+https://rawgit.com/dariusk/corpora/master/data/animals/dinosaurs.json#dinosaurs
+```
+To help with arrays-of-objects, items in the path that are preceded by a `!` will be skimmed for those properties. In the example case below, each item in `crayola.json["colors"]` has a `color` property and a `hex` property. This will make a symbol named `color` consisting of all the values of `color` in crayola.json.
+```
+:: color [corpus]
+https://rawgit.com/dariusk/corpora/master/data/colors/crayola.json#colors#!color
+```
+...these two formats won't work for gnarlier corpuses, but they're hopefully helpful enough to work for rapid prototyping.
+### 4.iii. Exporting JSON
If the grammar you've authored is destined for a non-Twine application (e.g.: a Twitter bot made with [Cheap Bots Done Quick]), you can print out the JSON using the following macro in a passage:
```
<<print tale.story.toHTML()>>
@@ -236,4 +260,5 @@ Then, just copy-paste the JSON into wherever it needs to go.
[setter links]: <https://twinery.org/wiki/link#setting_variables_with_setter-links>
[Cheap Bots Done Quick]: <http://cheapbotsdonequick.com>
[pseudomacro]: <https://twinery.org/wiki/macro#pseudo-macros>
- [mrfb]: <http://twitter.com/mrfb>
+ [mrfb]: <http://twitter.com/mrfb>
+ [dariusk/corpora]: <https://github.com/dariusk/corpora>
View
@@ -1236,6 +1236,83 @@ tracery.test = function() {
:: Story [script]
//requires jquery
+// input: an array of objects
+// a property that each of those object have
+// output: an array of the properties of all the objects
+window.skimObjectArray = function(objectArray, property){
+ console.log("skimObjectArray(", "objectArray", objectArray, "property", property, ")")
+ var values = [];
+
+ for (var i = 0; i < objectArray.length; i++) {
+ var thingToAdd = objectArray[i][property];
+ if(typeof thingToAdd === "object"){
+ // i hope to god this works
+ thingToAdd = objectArray[i][property].join("\n")
+ }
+ values.push( thingToAdd );
+ };
+
+ values = values.join("\n");
+ values = values.split("\n")
+
+ return values;
+}
+
+Story.prototype.appendCorpora = function(){
+ var corporaToAppend = tale.lookup("tags", "corpus");
+ if(!corporaToAppend.length) return;
+
+ for(var i in corporaToAppend){
+ var currentPassage = corporaToAppend[i].title;
+
+ // the rules are the concatenation of each symbol in this passage
+ var rules = [];
+ var lines = tale.passages[currentPassage].text.split("\n")
+ for(var j in lines){
+ var line = lines[j];
+ var location = line.split("#");
+ console.log("location: ", location);
+ var corpusLocation = location[0];
+
+ // fetch me that sweet sweet boy
+ var corpus = $.ajax({
+ dataType: "json",
+ url: corpusLocation,
+ async: false
+ });
+ corpus = corpus.responseJSON;
+
+ // drill down to the array we want
+ for (var i = 1; i < location.length; i++) {
+ console.log("corpus: ", corpus);
+ // if there's a ! at the beginning of a location, skim the objArray for that property
+ if(location[i][0] === "!"){
+ corpus = skimObjectArray(corpus, location[i].substring(1));
+ continue;
+ }
+
+ corpus = corpus[ location[i] ]
+ };
+
+ console.log("corpus: ", corpus);
+ // add this into the symbol-in-progress
+ rules = rules.concat(corpus);
+ console.log("rules: ", rules)
+ }
+
+ // the name of this symbol is the name of the passage
+ var finalJSON = "{ \"" + currentPassage + "\": " + JSON.stringify(rules) + " }";
+
+ // save our dark deeds to the passage
+ tale.passages[currentPassage].text = finalJSON;
+
+ //tag this as JSON so it gets appended in the next step
+ tale.passages[currentPassage].tags.push("JSON")
+ }
+
+ console.log("corpora loaded")
+}
+
Story.prototype.appendJSON = function() {
var JSONtoAppend = tale.lookup("tags", "JSON");
if(!JSONtoAppend.length) return;
@@ -1244,7 +1321,7 @@ Story.prototype.appendJSON = function() {
var newJSON = JSON.parse(JSONtoAppend[i].text);
$.extend(this.data, newJSON);
}
- console.log("appended");
+ console.log("JSON appended");
}
function Story(){
@@ -1269,7 +1346,7 @@ function Story(){
var linkIsGrammar = false;
var tags = tale.get(targetLink).tags
for(var i = 0; i < tags.length; i++){
- if(tags[i] == "grammar"){
+ if(tags[i] == "grammar" || tags[i] == "corpus"){
linkIsGrammar = true;
break;
}
@@ -1292,6 +1369,7 @@ function Story(){
this.data[newSymbol] = newText.split('\n');
}
+ this.appendCorpora();
this.appendJSON();
console.log("Story: ", this);
}
@@ -1343,11 +1421,18 @@ window.grammar = function(rule){
:: trace
-/%
-
+<<silently>>
Uses the parameter if one was passed. Then, checks for the $symbol variable. If neither is present, uses "origin". Clears $symbol at the end.
-%/<<if parameter(0)>><<set $symbol to parameter(0)>><<else>><<if $symbol>><<else>><<set $symbol to "origin">><<endif>><<endif>><<print console.log("trace " + $symbol)>><<print tale.grammar.flatten("#" + $symbol + "#")>><<forget $symbol>>
+<<if parameter(0)>>
+ <<set $symbol to parameter(0)>>
+<<else>><<if $symbol>>
+ No need to do anything.
+<<else>>
+ <<set $symbol to "origin">>
+<<endif>><<endif>>
+
+<<endsilently>><<print console.log("trace " + $symbol)>><<print tale.grammar.flatten("#" + $symbol + "#")>><<forget $symbol>>
:: window.trace [script]

0 comments on commit f080345

Please sign in to comment.