Un-probable Sentences

Andrei » 12 April 2006 » In Hacks »

I decided to start a section on Language and Linguistics, since it’s one of my passions and I am, after all, pursuing a graduate degree in it. So, I will be posting some interesting tidbits and such from the classes, the Web, and my own experiments.

This semester’s class is Computers and Written Language. It basically deals with introductory computational linguistics. Last week we covered n-gram language models, which are statistical models of word sequences. They are called n-gram because n-1 previous words are used to predict the probability of the next one. Such models are useful for a variety of tasks, including speech recognition (”The sign says key pout” vs. “The sign says keep out”), handwriting recognition, spellchecking, document identification, etc.

The programming assignment we had from the class required us to build a trigram (n=3) model of a given corpus of text. This involves counting occurrences of each trigram and calculating the probability of the final word following two preceding ones. For example, probability of see following want to can be calculated as:

Ρ(see|want to) = C(want to see) / C(want to)

That is, probability of see given want to is the number of times we’ve seen want to see trigram divided by the number of times we’ve seen want to bigram, and it turns out to be low, since want to can be followed by many different verbs. P(tonic|gin and), on the other hand, is much higher. You also want to take sentence boundaries into account, since I is very likely to begin a sentence in a fiction corpus, but not so much in a financial one.

So the idea is: read corpus, tokenize, count, calculate probabilities. Probably 30-40 lines of code in a language like PHP or Ruby (which is what I used, just for fun). Once I was done though, I thought, well, I have this nice trigram model, what else can I do with it? Ah, apply it in reverse, to generate sentences!

This is also a fairly simple task. Take a pair of words, then look in the list of the words that can possibly follow them, as learned from corpus, pick a probabilty at random and use it to make a selection from the list. Shift the sequence, so that the last word becomes next to last and the current one becomes last, rinse, repeat. The whole process is basically a Markov chain. I added some heuristics for comma insertion, a couple of controls, and called the resulting generator furby because it reminded me of that weird little toy from a few years ago that would sit there, absorb the sounds of the outside world, and regurgitate them back in a mangled, but eeriely recognizable manner.

So what kind of sentences did I obtain? Let me quote good old Chomsky first:

The notion “probability of a sentence” is an entirely useless one… — Noam Chomsky, 1969

I am not going to argue against his statement here, but I will apply it for my own purposes. You see, the sentences that furby generates are not improbable. They are… un-probable. Sometimes they are poetry, sometimes they are normal sentences you’d find in a book, but mostly they feel like someone who knows English as a second language had a hit of LSD and was asked to write down his thoughts. It’s English, with a big dollop of whoa-a-ah.

I got a few texts from Project Gutenberg site and fed them to furby. Everything from Sherlock Holmes stories to Alice in Wonderland to Robinson Crusoe. Here are some samples of what it spit out:

“I never have had a considerable household, he murmured.” (sane)
“I remember most vividly, three smashed bicycles in a fury of misery.” (poetry)
“He put his lips tight, and I wrote to the suspicion that the things had been shattered by his eager face.” (LSD)

The cool thing is that the results are in the style of the original text. Here are a couple generated from Twain’s Huckleberry Finn:

“There was them kind of a whoop nowheres.”
“You know bout dat chile stannin mos right in the night-time, sometimes another.”

Note that these are original sentence that do not occur in the texts. It was a lot of fun just running furby over and over again and seeing what it would come up with. But why not mix two authors? I tried a couple, but the best combination seemed to be DH Lawrence’s Sons and Lovers and the aforementioned Huckleberry Finn. Once it sucked in this unlikely duet, furby decided to become a comedian with a streak of soft-core pornography. Here are some gems:

“She wanted him and a half a sovereign.”
“Goodness man don’t be a fine woman.”
“Her mouth to begin working, till pretty late to-night.”
“She heard him buy threepennyworth of hot-cross buns, he talked to barmaids,
to almost any woman whom he felt.”
“He shoved his muzzle in the wet.”
“Joking, laughing with their shafts lying idle on the downward track.”
“As the lads enjoyed it when i realised that she was warm, on the pavement
then Dawes then Clara.”
“She had never been shaved.”
“He lay pressed hard against her and the electric light vanished, and I saw
the wrist and the coconut, and shook her head.”
“She could think of the body as it were, prowling abroad.”
“The three brothers sat with his finger-tips.”
“Eh, dear, if i’m a trying to get as drunk as a bubble of foam.”

Priceless.

My next goal is to feed it php-general archives and see if furby can be more intelligent that most of the postings on that list. Stay tuned.

Trackback URL

  1. andrei
    Dan
    13/04/2006 at 12:34 am Permalink

    Ah, great edutainment post — it reminds me why I enjoy your conference sessions so much.

    One of the IRC channels I hang out in has a bot that uses a Markov generator to spit out sentences based on the chatroom conversations. I don’t think it tracks and generates by nick, but that might have some interesting results… watching it adopt the “pffffft…” meme would be pretty funny.

  2. andrei
    Gemma
    13/04/2006 at 5:57 am Permalink

    Wow…spectacular. Thanks very much.

  3. andrei
    Abu Hurayrah
    13/04/2006 at 8:21 am Permalink

    This reminds me of a small project I had writing a rather simple article-based site with a close friend of mine (you can see it live at http://khutbah.com). To fill the system with dummy content, I made a simple generator that formed sentences of the form, “When (plural noun) (verb) (plural noun)”…if you think about it, that leads to lots of ready-for-FOX-TV shows. Amongst the verb list were words like “eat”, “attack”, “maul”, etc., and amongst the noun list were words like “Wolves”, “Babies”, “Elephants”. I’ll let you use your imagination as to what nice titles & articles resulted from this primitive sentence generator.

    On a slightly related note, your new word “un-probable” reminded me of this classic Ralph Wiggum quote: “Me fail English? That’s unpossible!”

  4. andrei
    taak
    13/04/2006 at 9:33 am Permalink

    Text_LanguageDetect uses trigrams to identify languages, though it uses character-trigrams, not word-trigrams.

  5. andrei
    Ivo Jansch
    13/04/2006 at 11:25 am Permalink

    Great stuff. :)
    I wonder if instead of books/authors, you feed it quotes from a particular person (for example speeches of Bush), the outcome would still resemble the way someone talks.

    Aren’t there any php conference session transcripts you could feed to furby? :)

  6. andrei
    Ivo Jansch
    13/04/2006 at 11:27 am Permalink

    Oh, and out of interest, did you make n=3 configurable? I wonder if the sentences clearly become more sane for n=4 or 5. But you would probably need larger amounts of texts to get something usable in that case, right?

  7. andrei
    Andrei
    13/04/2006 at 1:40 pm Permalink

    Ivo,

    I think the output would resemble the speech patterns of the particular person, but it wouldn’t be completely grammatically correct. Don’t forget that furby doesn’t know anything about grammar or semantics — it is a purely stochastic mechanism.

    I did not make n=3 configurable. I would expect n=4 to give results closer to the original because the state space of possible combinations is reduced.

  8. andrei
    Markus
    16/04/2006 at 7:47 am Permalink

    It would be interesting to feed epics like Lord of the rings ;)