I recently created a Twitter bot that tweets one randomly generated metaphor every two minutes.
I’ve decided to make the source code available. Get the source on Github here. Read the README.md, as it requires installing some software and getting your own API keys.
The program uses the Wordnik API, which is a fantastic service that lets you specify things like “give me 4 random nouns, and now give me words commonly used in phrases with those nouns.” I don’t really use any of the fancy stuff; I just ask for random nouns and adjectives within a certain tolerance of common use (I’ve calibrated it so that the words can get pretty weird, but not so weird that the sentence is incomprehensible). What the bot does is call the Wordnik REST API using Chris Williams’ super-simple restclient, which returns a JSON object with my words that I add to a very simple string. The algorithm is:
(a/an) noun (is/considers/of) (a/an) noun: adjective (and / , not / , yet / but / , / , but not) adjective
That’s it. I play around with frequencies a bit, where “is” is much more likely to show up than “considers” or “of”.
When it chooses articles it can be buggy. I don’t check for phonemes (though you can do it with Wordnik), so sometimes you’ll get “an university” or “a hour,” things like that.
Weirdly, the Wordnik API needs to be coaxed to just give me regular nouns or adjectives. For example, to get singular nouns I had to ask for it to include nouns, but explicitly exclude proper nouns, plural nouns, proper plural nouns, possessive proper nouns, suffixes, family names, idioms, and affixes. Adjectives I didn’t filter so much as it seemed to mostly work, but I still get weird words that don’t seem to fit, like “generall” (archaic adjective), cist (a noun), youngling (archaic noun). But I also get wonderful conversational words like “lovey-dovey,” “sky-high,” and “Smithsonian.” So… I’m pretty happy with the results.
The only other interesting thing the bot does from a programming perspective is that every five hours, it grabs the last 20 retweets of its own tweets and favorites them. The result is that you can see a best-of list right here.