208 Twitter Personalities


Merhaba Rubyists,

This week’s quiz is to create a program that will generate messages 140 characters in length. There primary use will be to create a Twitter “personality”. At the end of the quiz period these “personalities” will be unleashed on the internet and we’ll see how they do in the wild.

The programs will consist of two parts: a component for interacting with Twitter, and a top secret “personality” module.

For the Twitter interface component there will be no no-spoiler period. Please feel encouraged to discuss different libraries or methods on the mailing list. Let’s all work together to find the best interface.

The “personality” component can take any inputs and will produce a 140 character message when called. The “personality” may remember state. The no-spoiler period applies for the “personality” component; please save them until everyone has had a chance to consider their own implementations.

Have Fun!


There were two main components to this week’s quiz, the interface to Twitter and the personality that generates messages.

Let’s start with the Twitter interfaces.

Justin Collins’ @ewdbot uses the Twitter4r gem. The usage is straightforward. When given the account credentials it creates an object that provides methods to post status updates and get information from Twitter.

def initialize
  @twitter = Twitter::Client.new :login => "?", :password => "?"

# Posting a quote
@twitter.status :post, quote

# Getting replies
replies = @twitter.status(:replies)

Sandro Pagonatti’s @twsapiens uses the Twitter gem.

# Initialize Twitter credentials
base = Twitter::Base.new(Twitter::HTTPAuth.new('twsapiens', <psw here>))

# Get a random status text from the account's friends timeline
base.friends_timeline.sort{|a,b| rand()<=>rand()}.first.text

One comment about randomizing arrays is that Ruby 1.9.1 provides a shuffle method to Array, so now you no longer need to use sort_by{ rand } or the like if you are on 1.9.1.

Both of the gems make connecting to Twitter via HTTP a breeze, though Twitter4r seems a little bit simpler to use. No matter which one you choose it shouldn’t be more than a couple of lines to connect.

Now that we’re all connected to Twitter let’s examine some ways to generate messages to send.

@ewdbot by Justin selects a random Edsger Dijkstra quotation from the Edsger W. Dijkstra Archive. The quotation is selected by downloading a random page from the archive, selecting sentences that meet certain size and regex requirements, then choosing a random sentence from that list. The trimmed word lists are cached locally to make future access easier. Justin’s solution is well written and definitely worth examining if you are interested in learning more.

Remember that Sandro was selecting a random status message from @twsapiens friends timeline? Well that message is used to seed the personality. Sandro uses the Linguistics gem to create a collection of words related to the selected message. Those words are then arranged according to grammar rules from Ola Bini’s port of Peter Norvig’s Paradigms of Artificial Intelligence Programming. Some of these sentences come out a little crudely constructed, but it is a very difficult problem for a program to construct it’s own sentences. This sentence in particular is rather thought provoking:

the character in table on a variation by he by volume with he on a variation typecast a notebook computer

Thorsten Hater submitted a Markov chain text generator. I tested it out on some of the EWD quotations and it had some successful results. It would be interesting to combine the Markov chain text generation with trending topics in an attempt to create a popularity bot. Let the mailing list know when your bot hits 1,000,000 followers!

Twitter Personality Accounts:

Thank you everyone for your great responses to this week’s quiz!

Twitter Personalities (#208) - Solutions

Saturday, June 06, 2009