I was sorting through my collection of AI tutorials and discovered a large database of person names that I had forgotten about. I had a few hours to waste (Actually, this is a lie. The accurate explanation would be too long and still boring 😛), so I decided to put it to good use and make a person name generator. Here it is!
The Why’s & How’s
Aside from being amusing, name generators are useful for things like getting ideas for fantasy characters and forum usernames. In this case it was also good practice for understanding and implementing the Markov chain text generation algorithm (+ you’ll find the source code below).
The algorithm works like this (greatly simplified) –
- First, it processes all the words to determine how often each particular letter occurs in a given context. “Context” = one or more of the characters that directly precede the letter in question.
- Then these statistics are used to generate new words. The algorithm picks new letters at random. The characters that are more likely to occur in the context of already generated characters are picked more often.
- And that’s it.
The Markov generator produces names that look much more “natural” than the plain-old jumble of random letters would. On top of that, some of the generated names have a decidedly foreign feel – in addition to “Shery Fullo” and “Veener Grewski” there’s also “Utoivi Possar”. Ah well, I guess the database had some uncommon names in it.
Feel free to download the source code (RAR archive, 148 Kb) and see how it’s implemented. Beware! – it’s mostly-undocumented PHP, made available for the benefit of curious programmers 😉 The archive also contains a large text file that holds the entire context->character weight array that I used. You’ll need to unserialize the contents to make any sense of it.
By the way, I’ve done something similar before (in Delphi).Related posts :