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

Before you go...

Please consider supporting Technical.ly to keep our independent journalism strong. Unlike most business-focused media outlets, we don’t have a paywall. Instead, we count on your personal and organizational support.

Our services Preferred partners The journalism fund
Engagement

Join our growing Slack community

Join 5,000 tech professionals and entrepreneurs in our community Slack today!

Trending

The person charged in the UnitedHealthcare CEO shooting had a ton of tech connections

From rejection to innovation: How I built a tool to beat AI hiring algorithms at their own game

Where are the country’s most vibrant tech and startup communities?

The looming TikTok ban doesn’t strike financial fear into the hearts of creators — it’s community they’re worried about

Technically Media