<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,23 +1,20 @@
 class Boop &lt; CampfireBot::Plugin
   
-  # Markov chain implementation courtesy of http://rubyquiz.com/quiz74.html
+  # Markov chain implementation courtesy of http://blog.segment7.net/articles/2006/02/25/markov-chain
   
-  on_message /.*/,                :build_chains
+  on_message /.*/,                :listen
   on_command 'speak',             :random_chatter
   on_command 'prime_chains',      :load_transcripts
   
   on_command 'test',              :debug
   
   def initialize
-    @words = Hash.new
+    @phrases    = Hash.new { |hash, key| hash[key] = [] } # phrase =&gt; next-word possibilities
+    @word_count = 0
   end
   
-  def debug(msg)
-    p @words
-  end
-  
-  def build_chains(msg)
-    add_line(msg[:message])
+  def listen(msg)
+    add_words(msg[:message])
   end
   
   def random_chatter(msg)
@@ -29,75 +26,65 @@ class Boop &lt; CampfireBot::Plugin
     
   end
   
-  # TODO strip tags, ignore images.
-  
   def load_transcripts(msg)
     speak(&quot;Filling my brain with transcripts...&quot;)
     bot.room.available_transcripts.each do |date|
       transcript = bot.room.transcript(date)
       transcript.each do |message|
         filtered_text = strip_messages(message)
-        add_line(filtered_text.gsub(/([^\.])$/, '\1.')) unless filtered_text.blank?
+        add_words(filtered_text.gsub(/([^\.])$/, '\1.')) unless filtered_text.blank?
         puts filtered_text.gsub(/([^\.])$/, '\1.') unless filtered_text.blank?
       end
     end
     speak(&quot;Primed!&quot;)
   end
   
-  protected
-  
-  # Building the chains
-  
-  def add_line(text)
-    wordlist = text.split
-    wordlist.each_with_index do |word, index|
-      add_word(word, wordlist[index + 1]) if index &lt;= wordlist.size - 2
+  private
+    
+  def add_line(line)
+    words        = line.scan(/\S+/))
+    @word_count += words.length
+    
+    words.each_with_index do |word, index|
+      phrase = words[index, phrase_length]             # current phrase
+      @phrases[phrase] &lt;&lt; words[index + phrase_length] # next possibility
     end
   end
   
-  def add_word(word, next_word)
-    @words[word] = Hash.new(0) if !@words[word]
-    @words[word][next_word] += 1
+  def generate_line
+    # our seed phrase
+    # phrase = words[0, phrase_length]
+    phrase = random_word
+
+    output = []
+
+    @max_words.times do
+      # grab all possibilities for our state
+      options = phrases[phrase]
+
+      # add the first word to our output and discard
+      output &lt;&lt; phrase.shift
+
+      # select at random and add it to our phrase
+      phrase.push options[rand(options.length)]
+
+      # the last phrase of the input text will map to an empty array of
+      # possibilities so exit cleanly.
+      break if phrase.compact.empty? # all out of words
+    end
+
+    # print out our output
+    puts output.join(' ')
   end
-  
-  # Fetching from the chains
-  
+    
   def random_word
-    @words.keys.rand
+    @phrases.keys.rand.first
   end
   
-  def get_word(word)
-    return &quot;&quot; if !@words[word]
-    followers = @words[word]
-    sum = followers.inject(0) {|sum,kv| sum += kv[1]}
-    random = rand(sum)+1
-    partial_sum = 0
-    next_word = followers.find do |word, count|
-      partial_sum += count
-      partial_sum &gt;= random
-    end.first
-    next_word
+  def phrase_length
+    1
   end
   
-  # Composing sentences
-  
-  def get_sentences(count = 1, start_word = nil)
-    puts &quot;get_sentences&quot;
-    
-    word      = start_word || random_word
-    
-    puts &quot;starting word is #{word}&quot;
-    
-    sentences = ''
-    until sentences.count('.') == count
-      sentences &lt;&lt; word &lt;&lt; ' '
-      word = get_word(word)
-    end
-    sentences
-  end
-  
-  # Utility
-  
   def strip_messages(msg)
     str = msg[:message].to_s
     </diff>
      <filename>plugins/boop.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>0f6e12195b145b9eb2d44d1148f698454b61e704</id>
    </parent>
  </parents>
  <author>
    <name>Tim Riley</name>
    <email>tim@openmonkey.com</email>
  </author>
  <url>http://github.com/timriley/campfire-bot/commit/372567b253437bbc8c55d547bfc143f12c602587</url>
  <id>372567b253437bbc8c55d547bfc143f12c602587</id>
  <committed-date>2008-10-30T06:45:21-07:00</committed-date>
  <authored-date>2008-10-30T06:45:21-07:00</authored-date>
  <message>get started with a different, simpler markov implementation</message>
  <tree>cf13287516487762fb1fa57e4764da949734e572</tree>
  <committer>
    <name>Tim Riley</name>
    <email>tim@openmonkey.com</email>
  </committer>
</commit>
