markov.py
This was a little for-fun project that I built: a Python module/script that can be used to semi-randomly generate words, based on Markov chains.
Background, implementation
I was inspired by recalling the story of the Automated Curse Generator, which seemed like something that would be interesting to implement for fun in my own time, as it did indeed turn out to be. In short, the module examines input text and generates a graph with edges weighted based on character frequency, then traverses the graph to generate a word.
To generate the chains, the module builds a directed graph based on the seed text, where characters are linked to all the characters which are known to follow them, with edges weighted according to the percentage of all following characters any particular character consists of. For example, the string "zezifadi r00lz dr" would generate the following graph, where the value of each edge is the probability of choosing that edge to leave the associated vertex:
To generate a word, then, it can be as simple as starting at ' ' (the red node) and continuing to traverse the graph until another ' ' is encountered. In reality, while that worked, it was awfully boring. When seeded with some text in English, there was a disappointing number of short, boring (not to mention unpronounceable) words and far too few amusing longer ones. Think 'ad' and 's' rather than 'throm'.
It was rather easy to generate more interesting words, however, by simply adding some word-length limits, defaulting to a minimum of 4 character and a maximum of 12, tunable via arguments to the word generation method of the map. Rather than blindly following edges, as long as the word generated is shorter than the minimum, any chaining result of ' ' will be ignored. When maximum length is reached, the word will be immediately terminated provided the current character has any connection to blank space. If not, generation continues until such a connection is found.
What makes this so entertaining, I think, is its versatility. Since word generation is based entirely on the character frequency statistics of the input text, it works for any language. By extension, that means it could be easily be made to generate whole phrases in $(East-Asian language of your choice) by feeding it ideographs rather than Latin characters (ばかです (yes, I'm aware this is actually Kana)), or just nonsense that pronounces a lot like Simlish by putting in some other Simlish nonsense.
The script
Having implemented word generation in the module, it was reasonably short work to wrap the whole thing in a script so it could be invoked from the command line for great lulz. Something like the following does a decent job of providing amusement by generating a word every 15 seconds. For more fun, pipe the output into a speech synthesizer.
Tari@Kerwin ~ $ while markov.py; do sleep 15; done
Of course, before anything can be generated, a graph must be generated, which can be done via the -s option on the script or by invoking the addString method of MarkovMap. Quick example:
Tari@Kerwin ~ $ # Add the given string to the current graph, or to a new one. Tari@Kerwin ~ $ markov.py -s"String to seed with" -ffoo.pkl IO error on foo.pkl, creating new map seeeeed Tari@Kerwin ~ $ # Add some Delmore Schwartz to the map via stdin Tari@Kerwin ~ $ markov.py -ffoo.pkl -s- << EOF > (This is the school in which we learn...) >What is the self amid this blaze? >What am I now that I was then >Which I shall suffer and act again, >The theodicy I wrote in my high school days >Restored all life from infancy, >The children shouting are bright as they run >(This is the school in which they learn...) >Ravished entirely in their passing play! >(...that time is the fire in which they burn.) >EOF idagheam Tari@Kerwin ~ $ # Generate a word from the default graph in file markov.pkl Tari@Kerwin ~ $ markov.py awaike Tari@Kerwin ~ $
Easy enough. I've found that a Maori seed (via Project Gutenburg) makes for some of the more easily pronounced words, but any language will (mostly) generate words that are pronounceable via that language's pronunciation rules.
For seeding with non-Latin character sets, the script can take the -l or --lax option ('strict' keyword parameter to MarkovMap.addString()), which removes the restriction keeping graphed characters as only alphabetic. The downside, then, is that everything in the input is mapped out, so you're much more likely to get garbage out unless the input is carefully sanitized of punctuation and such (GIGO, after all).
Code
Enough talk, I'm sure you just want to pick apart my code and play with nonsense words at this point. Download link is below. I'm providing the code under the Simplified BSD License so you're allowed to do nearly anything with it, I just ask that you credit me for it in some way if you reuse or redistribute it.
Wednesday link dump
Because I have nothing better to do right now, it's a good time to dump the interesting links that I've been accumulating.
- While radioactive hunks of matter are often portrayed as glowing with a green tinge, we all know that's not actually true.. unless there's Cherenkov Radiation involved, as in many nuclear reactors- that's not green, though.
- Google have (for now) won the suit against them by Viacom regarding copyrighted content being uploaded to YouTube, which is good news for everyone except maybe Viacom. It's still fun to read choice excerpts of correspondence involving all sorts of mudslinging in the case (warning: lots of curses).
- OpenStreetMap is a neat project to create free maps, similar to Google Maps, Bing Maps, etc. Cool stuff, and all the map data is Creative Commons, meaning it could be used for any number of shiny projects.
- There might be life on Saturn's moon, Titan, observations courtesy of the NASA/ESA/ASI Cassini mission, which has been bouncing around the Saturnian system since mid-2004 after launch way back in 1997. It's far from a sure thing, but it's really exciting that predictions of how life might work on Titan have been supported by observation.
- This study (PDF) of internet routing to previously unused blocks is quite interesting, especially the numerous SIP streams pointed at 1.1.1.1 (section 5.1).
- The EFF (kind of like the ACLU of internet, if you're not familiar with them) recently put out the HTTPS Everywhere extension for Firefox. When it's this easy to lock down your web traffic, there's no reason not to. What's your excuse?
- Huge things are cool. Want to feel tiny? Go ask Wikipedia about the local supercluster, then consider how tiny everything humanity knows is, relative to that. When you're done scrabbling about in your own Total Perspective Vortex, consider epic timescales for extra kicks. Yeah.. cosmology is awesome.
- Exasecond timescales are impressive, but then there's the depressing concept of the universe's heat death. What if there were a way to reverse entropy?
- How did I not know this album exists? There are some really good mixes on it..
..and that's several weeks of accumulated cool-things. Enjoy.
PuTTYJL
After putting up with the lack of support for Windows 7's jump lists in PuTTY for a while, I finally got tired enough of it to do something. Nothing as cool as patching PuTTY to do them itself, but I wrote a wrapper which indexes the saved sessions, allowing the user to select which ones should be included in the list.
From the project page:
PuTTYJL is a wrapper and patch for PuTTY written in C# for .NET 3.5 and Windows 7, adding support for the new Jump Lists, allowing you to create jump list entries for saved sessions in the registry and optionally just launch the wrapper to start a default session in PuTTY.
Get it here.
Hacking life
Today (er, yesterday) was a big day for science. In the May 20th issue of Science, there was an interesting paper detailing how researchers at the J. Craig Venter Institute (yup, name means nothing to me there) successfully created a life-form containing entirely artificial DNA [abstract,PDF]. This is really exciting stuff.
As the authors of the paper note, sequencing genomes is nothing new, but there's a gigantic leap between just knowing how something is made and being able to make it yourself. Although this modified strain of yeast has mostly stock genes from other yeast and just over a million base pairs, Wired Science notes that our ability to manufacture chunks of DNA has grown by around 100x in the last five years. Following such a linear pattern, we would be able to build a human genome from scratch (~3 billion base pairs) within ten years. From here, where can we go? Anywhere.
Consider what living things do in nature. Now take some of that variety and modify it a little to do something more useful. Say, design an enzyme allowing yeast to break down oil from spills and removing any other metabolic pathways. You suddenly have a bacterium which eats oil spills, then the colony dies when the oil goes away.
Sure, something like that is a ways off; we don't have anywhere the necessary knowledge of the biochemistry involved in such a thing (or do we..? I could be entirely wrong). Proteins are amazingly complex molecules, and their assembly/folding is rather poorly understood at best. However, give it a while, and we could begin to do radical things within the framework of living things. Say, custom-designed viruses to patch our genomes. Literally, life hacking.
This is simply incredible stuff, and it's the first step toward the singularity, IMHO. More thoughts on that in the coming days.
CPU Comparison Shopping
I've been slowly working towards putting together a new PC build to replace my current one, a Core 2 Duo- based system I built about three years ago, which is starting to show its age. In the interest of comparison shopping, I put together a spreadsheet and some charts looking at the newer Intel (i5/i7) and AMD (Phenom X4/X6) processors. Turns out that Intel's Core i5-750 seems to be the best deal in processors for what I'm looking for in a system at the moment.
Raw Data
Clock speeds are in MHz, TDP in Watts, and cost is price in USD at newegg as of 5/3/2010. Processors with SMT (hyperthreading) are noted in the Cores column.
| Manufacturer | Model | Cores | Clock | TDP | Cost |
| AMD | Phenom II X4 955 BE | 4 | 3200 | 125 | 159.99 |
| AMD | Phenom II X4 940 BE | 4 | 3000 | 125 | 161.99 |
| AMD | Phenom II X4 965 BE | 4 | 3400 | 125 | 180.99 |
| AMD | Phenom II X6 1090T | 6 | 3200 | 125 | 309.99 |
| Intel | Core i5-650 | 2 | 3200 | 73 | 184.99 |
| Intel | Core i5-661 | 2 | 3330 | 87 | 199.99 |
| Intel | Core i7-920 | 4 (SMT) | 2660 | 130 | 279.99 |
| Intel | Core i7-930 | 4 (SMT) | 2800 | 130 | 294.99 |
| Intel | Core i5-750 | 4 | 2660 | 95 | 199.99 |
| Intel | Core i7-860 | 4 (SMT) | 2800 | 95 | 279.99 |
Gurren Lagann Redux
I totally just got this joke, after seeing it a while ago and just now seeing Gurren Lagann. It's funny if you've done so, I promise.
Tengen Toppa Gurren Lagann
Having just recently finished watching Tengen Toppa Gurren Lagann, I figured I'd share the fun with whoever happens to stumble across this. Perhaps this is the beginning of a pattern, in which I'll be posting mini-reviews of media I consume. Who knows. Anyway, let's get into it.
Generally
Gurren Lagann is the product of Gainax, the same studio (notably) behind Neon Genesis Evangelion, but that doesn't really mean anything- the two are completely different, even in animation style, which took a bit of getting used to. It's a mecha, so you've got plenty of robots stomping around, explosions, and post-modern themes. Most importantly, however, Gurren Lagann is loaded with silliness. To get a feel for exactly how silly, let's just say that at times there are multiple levels of mecha-piloting-mecha recursion.
"Your drill will pierce the heavens"
See what I did there? That's basically the point of the entire thing, and it's the source of most of the silliness. There's a very good reason for it all, but whenever something happens that makes no sense, it can be justified because "drills are magic".
The setting
It's some undisclosed alternate reality or future, where it seems all of humanity lives underground in small villages, and it's been that way long enough that it seems like the way things have always been. Simon the Digger lives in one such village, working to expand the village, but it's not really why he digs. He digs to find treasure. In addition to Simon, we have Kamina, a bit of a troublemaker, who wishes to escape the conservative village elder and escape to the surface, dragging Simon along, piercing the heavens to freedom. It's a pattern that gets repeated multiple times throughout the 27-episode run: fighting for freedom.
Bottom Line
Gurren Lagann is silly, fun, filled with explosions, and totally worth watching.
Oh, and how's this for a tidbit? 40% of the animation budget for the entire series was spent on the last 5 episodes. The sheer insanity is.. mind-blasting.
Now in production
For everyone who didn't know about my tests with WordPress, you're probably very surprised by the sudden change in the page, but this is the new taricorp.net, as I've moved (well, redirected) the site root to WordPress now.
Comments, questions, suggestions are all welcome, and enjoy the new setup.
Additional slow growth
The site continues to grow slowly, as I've just finished the first project page, for fb-hitler.
All's quiet on the Western front, it's a balmy 70º in Houghton for some reason.





