Using Markhov Chaining to Generate Random Words

A while back I wrote a little application that performs Markhov chaining to create random names.

What Markhov chaining does is creates new words based on the probability of a particular letter or syllable following another one, which is calculated based on a list of words used for this purpose.

So for example, if you wanted to create random names which sounded Italian, or Greek, then you would provide a list of real Italian or Greek words, and then the application would generate random new words which had a similar feel. The output words retain the flavour of the input words. This works for different languages too. If you enter a lot of German words, then the words generated will sound very German too (even if they are not real words).

Now this can work either on a letter or syllable basis, but the principle is the same for both, where for syllables, you would split the word into syllables rather than letters, but I will work on letters for the rest of this post.

So, let’s look at an example of how this would work.

We have this list of words: house fallen horse raisin

To process the input words, we loop over the words, and for each word, we loop through each letter in the word. So for each letter in the word now, we add current letter to an array, and keep track of the number of occurrences we have had of that letter, and then look at the following letter, and then for main letter we add it to our array of letters following that letter with the number of occurrences.

To make that clearer, using our sample words, after processing the word house our array will look as follows (with number of occurrences in brackets). h(1) -> o(1) o(1) -> u(1) u(1) -> s(1) e(1) -> ~(1)

Here we used ~ to denote that that letter had no following letters.

Now processing the rest of the words we end up with h(2) -> o(2) o(2) -> u(1)            r(1)

u(1) -> s(1) s(3) -> e(2)            i(1) e(3) -> ~(2)            n(1) f(1) -> a(1) a(2) -> l(1)            i(1) l(1) -> l(1)            e(1) n(2) -> ~(2) r(2) -> s(1)            a(1) i(2) -> s(1)            n(1)

Now, when you generate a word, you can randomly choose an initial letter, and then using a random number generator, select which child letter to use as the next letter, and then randomly choose a child letter for that letter, and so on, until you reach an end of word character.

The source for the Markhov chaining application which I have written in C# is available on GitHub.

Originally posted on my old blog, Smoky Cogs, on 28 Sep 2009


Read More »

The Heroic Feat of Coding in Whitespace

I have encountered one of the world’s most unreadable programming language: Whitespace. It is not your conventional language by any stretch of the imagination.

What whitespace is, is a stack-based language, which means most of the operations involve pushing and popping numbers onto a stack and being able to do arithmetic operations on it. Beyond this there are also operations that allow jumping and conditional statements, basically the equivalent of if statements and goto statements in other languages, and subroutines are also supported.

This alone, however, would not rank Whitespace as a complicated language. It would be very limited in its application – allowing nothing more complicated than a calculator program or something similar.

The real reason Whitespace is such an incomprehensible language, is that EVERYTHING is written using only white space characters. As in the characters for space, tab and linefeed. Various combinations of these characters denote the various commands.

Now, when you open a file with this code in it, it would appear as if nothing was entered at all. For a good example of how a program written in Whitespace would look, have a look at the Whitespace site.

I seriously think that some people have way too much time on their hands…

Originally posted on my old blog, Smoky Cogs, on 25 Sep 2009


Read More »

Mathematical Modelling of a Zombie Attack

I came across a very interesting story on Reddit today.

A team of mathematicians, Philip Munz, Ioan Hudea, Joe Imad and Robert J. Smith, from the University of Ottawa and Carleton University, have published a paper entitled When Zombies Attack!: Mathematical modelling of an outbreak of zomie infection.

Normally, the world tends to view mathematicians as a staid, humourless bunch of nerds hiding away in a musty office playing with numbers all day. I think this goes to prove otherwise - or at the very least, they have a sense of humour.

The research paper is written in a serious way, and for those who are braver than most, it includes a lot of mathematical formulae describing the infection rate when a zombie bites a human, taking into account factors such as susceptibility, quarantine and treatment.

The paper is not pointless though. Even though these guys have chosen a topic featured in just about every B-grade horror film since the 50’s, the paper does model how an infectious disease would be able to propagate through a population. So, for example, you could replace Zombie infection, with Swine flu infection. The principle remains the same, but I certainly think it makes for a much more fun to read research paper when we talk about zombies.


Read More »

Navigation Doggie-style

Cars are not a natural part of life for dogs. Well, I would believe that, except they seem to have adapted well to these unnatural beasts.

Just this morning, I went down to the shops, and Noisette came along for the ride as she normally does.

Now, you would expect a dog to not know where they are going in a car, as the motion of the car is unnatural for a dog, but Noisette always sits up in the car and watches where she is going. If in her favourite spot in the front next to me, she will keep her gaze fixed firmly in front taking in the view, while when she sits in the back she will lean on the seat looking out of the window.

And to add credence to the fact that she knows where she is going, she knows exactly the route we take to the park for her daily run, or the way to the beach (her favourite place on earth), and several of the other places we visit. Often, even when we are still several kilometers away from where we are going, she will recognise the route and then start getting excited. This just goes to prove to me that dogs are aware of their surroundings and have a memory for routes even when travelling by car.

Another adaptation which Noisette has shown is that she is remarkably resilient to my rather bad driving. She has taught herself to lean into corners and compensates for my braking and acceleration superbly. She will sit on the seat and slip an inch.

Dogs truly have a great ability to learn.


Read More »

Tag Cloud

Algorithms (3) Android (10) Astronomy (25) Audio (1) Audiobooks (1) Barcodes (9) C# (69) Css (1) Deep sky (6) Esoteric languages (3) Family (3) Fractals (10) Gaming (1) Genealogy (14) General (2) Geodesy (3) Google (1) Graphics (3) Hubble (2) Humour (1) Image processing (23) Java (8) Javascript (5) jQuery (3) Jupiter (3) Maths (22) Moon (5) Music (4) Pets (5) Programming (88) Saturn (1) Science (1) Spitzer (4) Sun (4) Tutorials (68) Unity (3) Web (9) Whisky (13) Windows (1) Xamarin (2)