A new year, and a new toy to begin it.

This all began when Tom started tweeting the prose from the back of a chocolate box.

Tt tweets

One look at that and, having gagged a little on the truly purple prose, there was only one obvious continuation: a machine to churn out chocolate descriptions infinitely.

Which was as good a time as any to play with Markov chains. Wikipedia will explain in more detail, but if you’ve never encountered them, a very rough explanation is: Markov chains are systems that model what the next item in a list will be based on the previous ones. The more previous items you have, the better it can predict the next thing.

They’re often used in toy text generators. You give them source text to seed them, randomly pick a word from the source text, and then start choosing what should come next. What’s nice about this is with nothing other than a piece of maths, and a tight corpus, we can produce things that usually read like English without having to teach a computer something as complex as grammar. Of course, sometimes you get grammatical-yet-nonsensical English out, but that’s hardly in a problem in our case.

So I took the full prose from the back of Tom’s chocolates (Thornton’s Premium selection, for reference), some Markov text-generation code from an illuminating installment of Rubyquiz, and fiddled for a bit.

A short piece of work later and I had Markov Chocolates.

Markov

Roughly once every four hours (but it varies), you’ll get a fresh, tasty new Markov Chocolate in your Twitter feed. It’s another of my daft toys, but it still makes me chuckle. I’m thinking of expanding the corpus soon, and I hear the Markov coroporation are keen to branch out into new product lines. For now, you can get your chocolate fix here.

  • "I built a working prototype of a Customer Service phone bot that has personal issues she'd like to talk about and over time falls in love with the caller. She uses the tools at her disposal (discounts, upgrades, hold music, confirmation numbers) to communicate her feelings towards you as best she can." Hah!

I got an email today, a bit sad that the bot I made to comment on Tower Bridge’s state had disappeared.

I wasn’t aware it had disappeared.

So I checked Twitter, and sure enough: @towerbridge is now owned by an “official” account. They joined Twitter on the 18th May this year. I’m not going to comment on the quality or usefulness of the account to date.

What I am going to comment on: the bot has disappeared. All those tweets are gone, basically. So there’s no history any more of bridge lists. There’s no instrumentation of a part of the city. A little bit of the heartbeat of London – for me, and the nearly 4000 other people who followed the bot – has disappeared.

Now, I use an old email address that I check rarely for that account – but I’ve not been contacted once about this issue. The account has just been gazumped, and a little, talking part of the city has been killed.

I’m about to get in contact with Twitter the second I’ve posted this. I’m more than a little furious; after all, all the URLs that link to it are now incorrect, all the lifts, all the (puppet-mastered) banter is gone. Cool URLs don’t change, and these have just gone. And in their place: marketing.

I’ve never pretended to be an official account; I’ve never dissimulated; no-one from the exhibition has ever got in touch with me about the bot.

So, for the time being: this is why the bot has disappeared. I’m very, very cross, and perhaps a little upset; the robots are our friends, after all.

  • "Nolan’s cities are iconless places. The Hong Kong sequence in The Dark Knight omits the harbour, the HSBC and Bank of China buildings, and that city’s famous apartment buildings, instead focussing on vertiginous aerial view of the masses of anonymous buildings in Central. Cobb and Mal’s ideal city four dreams deep in Inception is an infinity of curtain walled downtown, ordinary in the extreme and all the more unsettling because of it. In any case it will be interesting to see where Nolan takes Gotham city in its third outing, likely deeper into the fantastic generic." Interesting take on Christopher Nolan's nowhere-cities. Worth also noting that whilst Cobb and Ariadne build cities, Arthur's dreams tend towards interzones – airports and hotels. There's something on the Interzone and its relationship to that film to be said, too.
  • Straightforward guide to making bots work with OAuth.
  • "You could say that it’s tidal, but with the television schedules, rather than the moon."

As a little post-Christmas present, I thought I’d share a little code toy I’ve been working on recently.

You might know that I’m a fan of Twitter as a messaging bus, and I’ve already built some entertainingly daft bots in my time with it. Recently, I decided to flex my programming skills a bit and build not one but four bots. And, more specifically: four bots that talk to each other.

Enter @louis_l4d, @zoey_l4d, @bill_l4d and @francis_l4d. You might notice that they’re named for the characters in Left 4 Dead.

This is not a coincidence.

One of the most wonderful things in that game (which I’ve already commented on the brilliance of) is the banter between the four player characters. There’s so much dense, specific scripting, and enough dialogue so that it rarely repeats. I thought it would be interesting to see if you could simulate the four players’ dialogue over Twitter, sharing some state between the bots, but also finding a way to make them communicate a little with each other.

Well, a bit later, I worked out how, and this is the result:

twit4dead.jpg

You get the picture. They run a scenario, they bump into boss zombies, they find stuff, they get hurt (and help each other), they get scared (and reassure each other). At the moment, there are some dialogue overlaps; my main work at the moment is adding more unique dialogue for each bot. Bill is sounding pretty good, but the rest of them need work. It takes about 2-3 hours for them to run a scenario, and it’s usually fun to watch. (And, as you can see, it makes sense to follow all four of them).

So how does it work?

It turned out that rather than trying to build any real AI, it was much more fun just to simulate intelligence. The bots are state machines; they have a variety of states, which they transition in or out of dependent on factors, and suitable dialogue for each state.

I wrote the bots in Ruby. There are two main components to the twit4dead code: the Actor class and the Stage class. The Stage is a singleton; it’s where the state of the world is determined, and global variables tracked. It’s also where all the probabilities are run from. The Actor class is what each of the bots are, and it’s based on the Alter Ego state-machine library for Ruby. We have a lot of states, rules for transition, a selection of methods to handle being helped or talked to by friends, and a method to choose a random piece of dialogue appropriate to the current state.

All the bots are instantiated from a YAML file. For each bot, I store its Twitter username and password, and a nested tree of dialogue for each state. This means it’s really easy to add new states and maintain the dialogue for each bot. It’s also easy to add new bots – you just create a top-level entry in the YAML file.

Originally, I thought about the bots broadcasting and listening over Twitter, but the API calls were just going to get out of hand, and it turned out that Twitter didn’t like being bombarded with messages and would drop a few over time. So I separated out writing the script and broadcasting it. A small utility generates a script file; each line of this file consists of three delimited fields for username, password, and message to send to Twitter. Then, another program – which I currently run on a screened shell – reads that file and broadcasts one line of it every minute until it’s done.

And that’s it. I have to run it by hand for now, which is fine – it’s more something I fire up every now and then, rather than something you want to permanently run. I originally was going to keep track of loads of statistics – health, zombies in play, etc – but found a cruder set of rules worked much better. Every time they’re in combat, there’s a slim chance somebody gets hurt; every time somebody’s hurt, a friend will rush to save them. That sort of thing. Simulated Intelligence, then, rather than Artificial Intelligence.

Alter Ego turned out to be a lovely library; dead simple to use, and as a result the bots are really nicely modelled (or, at least, I’m very happy with how they’re modelled). The notion of a Stage with Actors, rather than a Game with Players, feels about right, and the modularity of it all is pretty nifty. It still requires a little refactoring, but the architecture is solid, and I’m proud of that.

I think my favourite aspect of it, though, is that at times, watching the bots play together is a little like magic. The first time I saw them talk to each other, cover each other whilst reloading, help each other up after a Boomer attacked, I felt a little (only a little, mind) like a proud father. They’re dumb as a sack of hammers, but they look convincing, and that was the real goal. It’s fun to watch them fight the horde amidst all my other friends on Twitter.

Nonsense, then, but a fun learning exercise about state machines, object orientation, and simulating conversations. State machines are a ton of fun and if you’ve not seriously played with them, I thoroughly recommend it.

Do follow the four of them, if you fancy; I’ll make sure I run them with reasonable regularity, and I’ll be fixing the dialogue over time. After all, I want to keep Francis happy.