Twitter Personalities (#208)

Uops. real link:
http://github.com/sandropaganotti/Twitter-Sapienshttp://github.com/sandropaganotti/Twitter-Sapiensit

On Wed, Jun 10, 2009 at 8:13 AM, Sandro P. <

On Jun 5, 2:54 pm, Daniel M. [email protected] wrote:

Visit:http://rubyquiz.strd6.com/suggestions

The programs will consist of two parts: a component for interacting
save them until everyone has had a chance to consider their own
implementations.

Have Fun!

-Danielhttp://rubyquiz.strd6.com

Here’s the twitter “personality” I made: http://twitter.com/iloveweather

It posts about the weather conditions of a random US city. The phrase
list needs some expanding, and there’s a little code to clean up
before I post it. It was pretty fun, and I’m really enjoying seeing
tweets like “Step outside, Bliss. It’s nice.” and “It’s days like this
that make people happy to be in Beverly.”

It doesn’t do replies (yet).

More to come,
Chris

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 C.’ @ewdbot uses the Twitter4r gem1. 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 => "?"
end

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

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

Sandro Pagonatti’s @twsapiens uses the Twitter gem2.

# 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 Archive3. 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 gem4 to create a collection
of words related to the selected message. Those words are then
arranged according to grammar rules from Ola B.'s port5 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 H. 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:

  • @iloveweather - Chris S.
  • @ewdbot - Justin C.
  • @twsapiens - Sandro Pagonatti

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