How I built Metaphor-a-Minute

by Darius Kazemi on May 15, 2012

in philosophy,programming

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, as it requires installing some software and getting your own API keys.

The whole thing is pretty simple and hacky. The program is pure JavaScript, running in node.js, which lets you run server-side JavaScript totally distinct from a web browser. I host the bot over at Nodejitsu — you can actually see the app “running” here: (Nodejitsu requires all its apps to respond to HTTP requests, so I have Express, a very simple web server, running just so the hosting service doesn’t mess things up.)

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.

Finally, I have the program tweeting on a setInterval using the twit npm package, which is the simplest JavaScript Twitter API interface I was able to find. It works exactly as advertised.

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.


knutaf May 15, 2012 at 8:42 pm

I feel so distanced from web-type programming these days (it moves so quickly!). I would totally use a post like this as a tutorial of sorts for how to tie all these crazy services together into something non-trivial, yet not mammoth.

a metaphoraminute is a tutorial: non-trivial yet manageable

Matthew Farmer May 16, 2012 at 9:59 am

I set up a random sentence generator several weeks ago, but I went about it making my own word database. I had no idea about Wordnik, but it seems awesome. So I’m going to use that now.

Jatin March 7, 2013 at 1:34 am

Would like to create a twitter bot. Want to start from basics. dont know much about it. Its my clients requirement. Please help me.

My requirement is to create a bot that will send infor about latest release for its items.

Darius Kazemi March 7, 2013 at 10:36 am

Sorry, writing this article and providing the source code is the best I can do right now.

Ian January 23, 2014 at 4:12 am

How do I know if I am being sold an advertising service which is using a bot to falsely generate followers on twitter?

Darius Kazemi January 25, 2014 at 5:22 pm

Good question. It depends on the service, but generally speaking if someone simply promises you “more followers”, then it’s probably a bot net. Maybe you could ask to speak to other clients?

Belgian Chocolate March 12, 2014 at 6:30 am

I was looking for examples on Pinterest bots and I came across your example for Twitter. My main focus would be to automate a lot of tasks for several social websites built inside one single application. For example, following, liking, tweeting/pinning/sharing in order to get REAL followers. I do not mean to spam but to stop spending hours and hours doing that instead of focusing on more creative tasks.

I have never used nodejs (although I heard a lot about it) but it seems that things can be set pretty fast. I would need more features but it seems easy to build the twit interface. But, as you said, this would be the simplest one so there are surely more complete interfaces.

Is it fast to setup the server?

Comments on this entry are closed.

{ 5 trackbacks }

Previous post:

Next post: