Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created February 22, 2012 22:18
Show Gist options
  • Save jakevsrobots/1887816 to your computer and use it in GitHub Desktop.
Save jakevsrobots/1887816 to your computer and use it in GitHub Desktop.
Processing sketch that does a fake markov chain type reprocessing of a twitter search feed
void setup() {
// Fetch recent tweets from twitter with a certain keyword
XMLElement xml = new XMLElement(this, "http://search.twitter.com/search.atom?q=lasertag");
XMLElement[] tweets = xml.getChildren("entry/title");
String corpus = "";
for(int i=0; i<tweets.length; i++) {
corpus += tweets[i].getContent();
}
// Pick a random word from the corpus to start with
String[] allWords = corpus.split("\\s+");
int firstWordIndex = int(random(allWords.length));
String currentWord = allWords[firstWordIndex];
// String together 20 words from the corpus into one new tweet
String weirdTweet = "";
for(int i=0; i < 20; i++) {
// Find all the points in the list of words where the current word occurs
ArrayList indices = new ArrayList();
for(int j=0; j < allWords.length; j++) {
if(allWords[j].equals(currentWord)) {
indices.add(j);
}
}
// Pick a random one of those points, and get the index of the word after it
Collections.shuffle(indices);
int nextWordIndex = (Integer)indices.get(0) + 1;
// Error checking
if(nextWordIndex >= allWords.length) {
nextWordIndex = 0;
}
// Get the new word and add it to the new tweet.
currentWord = allWords[nextWordIndex];
weirdTweet += " " + currentWord;
}
// Print the text
println(weirdTweet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment