Code Annotated: How this Groupon dev built a news-poetry Twitter bot - Technical.ly Baltimore

Software Development

Code Annotated: How this Groupon dev built a news-poetry Twitter bot

Jonathan Julian shares a few lines of code from a fun side project he's working on.

What Jonathan Julian is working on.

(Screenshot)

This is a new Technical.ly series called Code Annotated, in which we ask local technologists what they’re developing. This is a guest post from Baltimore-based iOS developer Jonathan Julian.


I like the simplicity and playfulness of Twitter bots. Take some serious input, make some changes, and spit it out in a tweet.

Playing around with news headlines can be especially fun. Inspired by the wacky headlines of today as well as bots like @twoheadlines and @CNNyourmom, I started playing around with my own news-headlines-as-poetry Twitter bot (coming soon). This is the meaty center of it:

news = get_news()
titles = news[‘articles’].shuffle.take(3).map { |article| article[‘title’] }
phrases = titles.map { |title| title.gsub(/[^\w\s]/, ”).split(‘ ‘).shuffle.take(2).join(‘ ‘) }
tweet phrases.shuffle.join(‘ ‘)

The news object contains articles which each have a title. To make a six-word poem from three article titles, we have to randomly choose three articles, strip out all punctuation (gsub) and randomly choose two words. Then combine the three pairs of words to form the poem. Here are some examples:

  • Not All of Associates Ears Anchors
  • Cost Yiannopouloss Clintons in Russia Trump
  • Security McMaster Remark Nation of BackChannel

This code is written in Ruby, which is an easy-to-read programming language that is great at text processing. I lean on Ruby for my other Twitter bots too: @bmorejuryduty, @manningdotd and @artmeme14. I’m still thinking about what I can do with these “news poems,” what do you think?

As you can see, there’s not much to making a simple bot, so maybe you should give it a try.

Companies: Groupon
-30-
Subscribe to our Newsletters
Connect with companies from the Technical.ly community
New call-to-action

Advertisement

Technically Media

Sign-up for daily news updates from Technical.ly Baltimore