Created
July 8, 2014 18:36
-
-
Save dariusk/f66f49d52b225baa7251 to your computer and use it in GitHub Desktop.
Code snippet from @autocharts with an explanation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I search twitter for the following strings. I'm looking specifically for questions that start with | |
// 'do you' or 'are you', but do not contain certain key words. | |
_.when( | |
search('"do you" -what -why -who -where -which -when -how'), | |
search('"are you" -what -why -who -where -which -when -how') | |
) | |
.done(function(doYou,areYou) { | |
// I take the raw search results and I snip away | |
// the "do you" or "are you" and just grab the rest of the sentence. So "Are you a magician?" becomes | |
// "a magician". I collect a bunch of these. (I strip away "what" and similar, because "what are you | |
// doing?" would parse to "doing", which isn't interesting. I want a subject in the subclause.) | |
// the output of the function is actually an array of objects: { preQ: "are you", preA: "you are", text: "a magician" } | |
doYou = filterResults(doYou, 'do you', 'you'); | |
areYou = filterResults(areYou, 'are you', 'you are'); | |
// Now I flatten both result arrays into one array | |
var results = _.flatten([ | |
doYou, | |
areYou | |
]); | |
// Now I have a list of results like [{preQ: "do you", preA: "you do", text: "even lift bro"}, | |
// {preQ: "are you", preA: "you are", text: "my mother"}] | |
console.log(results); | |
// So that generates a huge list of potential questions and answers. | |
// next comes the graph generation part. I won't go into much detail but basically I make 5 question nodes, | |
// 3 answer nodes, and then for each question I require two outgoing edges: a Yes and a No. I pick a random other question | |
// or other answer for each Yes/No edge to connect to. Answers themselves just sit there. If they get connected to, great. | |
// If not, they're orphans and that's okay too. This is an essentially random graph layout, which leads to some wacky | |
// results sometimes. But MOST of the time it looks pretty plausible. | |
tweet(results); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment