

In the above, underlining defines states, red denotes a candidate word generated by the model, and opacity corresponds to probability. From a random starting stage, we iteratively generate a chain of plausible words. Generating sequences of words using a Markov chain model. You can see that we actually ended up selecting the second most probable option (pork), generating the new state ‘cooked pork’. For this state, we’ve coloured the possible choices by their probability, with more opaque corresponding to higher probability. This gives us our next state -’ man cooked’- and the process begins again, up to some specified sentence length. Here ‘jumped’ and ‘cooked’ are equiprobable, and we end up choosing ‘cooked’. So if a word is 90% likely to follow from a given state, we pick it 90% of the time in our random walk. At each step, we choose from the likely next words according to their probabilities.

We then perform a random walk through the model. To start our sentence, we pick a random state – ‘the man’- from the corpus. The process of generating sequences of words is visualised below. It’s a rather ignorant model, but it still does a surprisingly good job! If you’d like to have a go at generating your own Markov model, you can do so here courtesy of Daniel Ecer. The model doesn’t know anything apart from what it saw in the data – no knowledge of semantics, or syntax, or grammar. Note also, that other words which might be plausible – like ‘ran’ or ‘awoke’- are also assigned p=0, because they didn’t appear in the training material. Other words, like ‘banana’ and ‘woman’ are impossible (that is, p=0). If we define our state as ‘the man’, we can quickly see that the only possible words are ‘jumped’ and ‘cooked’, which are each 50% likely. By doing this repeatedly, we can generate sequences of plausible words.įor example, let’s assume we trawl our text and find that we have the phrases ‘the man jumped’, and ‘the man cooked’. The model is trained by splitting up the training corpus into these ‘states’, and for each state noting what the likely next word is. Despite being rather intimidatingly named, Markov models capture a simple idea: that you can work out the likely next word given the past few words. We used a Markov chain model to generate sentences in a word-by-word fashion. Here we describe an approach using Markov chains (don’t worry, we unpack that term below) in a later blog post we will discuss how neural networks can provide a powerful alternative approach. So how do we actually do this? We need to specify a model by which the bot can learn what sentences are probable, and then produce them. In this manner, we could teach a machine to write like Burns, or Wikipedia, or Shakespeare. The algorithm thus learns to generate sentences that have a high probability in ‘Burns language’ which as you’ll apreciate if you’ve read any of his poems, is not quite the same as normal English. We can go one step further: in order to generate sentences that sound like they were written by Robert Burns, we train upon sentences written by Burns. Similarly, the combination of letters arranged as ‘the’ is very probable the combination ‘xyo’ is not very probable. For example, the sentence ‘I ate some haggis’ has a higher probability than the sentence ‘carpet grab bag leg’. We want the machine to generate words and sentences which seem plausible, which is another way of saying that they have high probability. In both cases, the problem boils down to one of probability. We do this over and over, and we end up with sentences. To do this, we have a collection of possible letters (A-Z), and we train the algorithm to select letters that produce real words. The second, more fine-grained option, is to generate words letter by letter. The first strategy is to start with a big collection of words – say all of the words that Burns ever used – and train the algorithm to pick collections of words that form plausible sentences. How can a machine generate meaningful text? You can think of a couple of ways to approach this problem. This year, the ASI team decided to celebrate Burn’s night in a creative manner: building a robot to produce Burns-esque poetry*. Each year on January 25th people throughout the UK come together to celebrate his life, for a night that typically revolves around two of Scotland’s most famous culinary exports: haggis and whisky. Robert (or Rabbie) Burns was a Scottish poet whose corpus includes ‘An Ode to A Haggis’ and the New Year’s Eve favourite ‘Auld Lang Syne’.
