My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
The following was generated using a 7th order Markov chain and several of my blog posts as source text:


For the monetary hurdles? A computer to prove anything on you.

If you are serving with talented coders so it doesn't change. Can you understand the case study present in different paradigm shifts in science. I was on vacation, "I can't see my old search terms every time you run into a code monkey? Are you a code monkey? Are you stick to standards so you could have implement our own develop good sound-editor to do it. Even more diplomatic terms, of course of action - if a proxy) the objectives and different approach much better, and how can you believe it?" I think it would solve those domain you're willing to him speak than after a little over a million users so it doesn't "mean that a competitors. Of course of action - if a proxy) the objectives and different paradigms to help out in the to-read list: The Power of integration in asking what other hand, JSF, Seam, and Tapestry are based on my to-read list.) Because I think you can keep the view updated - but there are made, and borders on absurd side of the elements in the code for you to record an improvement. That's not a far leap to see that you do?" Here he actually pointless. How do I know - I was doing before I started to deploy to XBOX 360 or PC (Developers created." Instead, you should use is a contest to avoid conversations. It can be threatened." What are the Maximizers are drive-in traffic and implementation of an old professor of mine, be honest about it. Thanks XNA!

As always came back in January. Perhaps you understand it. If you want to conversations. It can be threatened." What are the number of integration in asking people something to do it. Even more diplomatic terms, of course, you can do. In particular, by posting a good developers created." Instead, you should.

The point is that you're not passion.


There are a couple of repeats noticeable, which is mostly due to my lack of source text. At least, it worked a little better using one-hundred 8.5" x 11" pages of Great Expectations (still gibberish though).

Anyway, here's the Ruby source code:

class MarkovModel
    def create_model(file, order, unit)
    #unit = "word" or "character"
        @unit = unit
        entire_file = ""
        @order = order
        #read the entire file in
        File.open(file, "r") do |infile|
                cnt=0
                while (line = infile.gets)
                        entire_file += line
                        cnt+=1
                end
        end

        #split the file according to characters or words
        if unit == "word"
                split_on = /\s/
        else
                split_on = ""
        end
        @entire_file_split = entire_file.split(split_on)

        #construct a hash like:
        #first 'order' letters, letter following, increment count
        @model = Hash.new
        i = 0
        @entire_file_split.each do |c|
            this_group = @entire_file_split[i,order]
            next_letter = @entire_file_split[i+order,1]

            #if group doesn't exist, create it
		   if @model[this_group] == nil
		       @model[this_group] = { next_letter => 1 }
            #if group exists, but this "next_letter" hasn't been seen, insert it
            elsif @model[this_group][next_letter] == nil
                @model[this_group][next_letter] = 1
            #if group and next letter exist in model, increment the count
            else
                cur_count_of_next_letter = @model[this_group][next_letter] + 1
                @model[this_group][next_letter] = cur_count_of_next_letter
            end
            i+=1
        end
    end

    def generate_and_print_text(amount)
        start_group = @entire_file_split[0,@order]
        print start_group

        this_group = start_group
        (0..(amount-@order)).each do |i|
            next_letters_to_choose_from = @model[this_group]

            #construct probability hash
            num = 0
            probabilities = {}
            next_letters_to_choose_from.each do |key, value|
                    num += value
                    probabilities[key] = num
            end

            #select next letter
            index = rand(num)
            matches = probabilities.select {|key, value| index <= value }
            sorted_by_value = matches.sort{|a,b| a[1]<=>b[1]}
            next_letter = sorted_by_value[0][0]
            print " " if @unit == "word" #if we're splitting on words
            print next_letter

            #shift the group
            this_group = this_group[1,@order-1] + next_letter.to_ary
        end
    end

    def print_model
        require 'pp'
        PP.pp(@model)
    end
end

file = File.expand_path "source_text.txt"
mm = MarkovModel.new
mm.create_model(file, 7, "character")
mm.generate_and_print_text(2000)
mm.create_model(file, 1, "word")
mm.generate_and_print_text(250)
#mm.print_model


Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

Fun stuff! Do you have other interesting projects like this? I'd like to know about them

Posted by Max on Feb 01, 2010 at 08:01 PM UTC - 6 hrs

Hey Max,

Somewhere I've got some GA code, but I don't think I ever put it on the blog. I'll try to look it up and post it. I've been wanting to get back into some of these algorithm posts, so you might have given me a good excuse.

I also had a post about using suffix arrays to help in search, http://www.codeodor.com/index.cfm/2007/12/24/The-S... and one of my favorite unfinished ones is about a partial order planner: http://www.codeodor.com/index.cfm/2007/5/4/Constru...

I tried to find the k in k-means clustering (but failed to do so) in this post: http://www.codeodor.com/index.cfm/2006/12/4/k-Mean...

Hope some of that interests you.

Sam

Posted by Sammy Larbi on Feb 02, 2010 at 07:32 AM UTC - 6 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner