refactored markov to be clearer

This commit is contained in:
Chris Anderson 2008-06-02 09:34:04 -07:00
parent 8ff959a6f7
commit b36552e710

View file

@ -1,28 +1,32 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require '../../couchrest' require File.expand_path(File.dirname(__FILE__)) + '/../../couchrest'
cr = CouchRest.new("http://localhost:5984") cr = CouchRest.new("http://localhost:5984")
db = cr.database('word-count-example') @db = cr.database('word-count-example')
@word_memoizer = {}
def probable_follower_for(word)
@word_memoizer[word] ||= @db.view('markov/chain-reduce', :startkey => [word,nil], :endkey => [word,{}],:group_level => 2)
# puts
# puts "search #{word} #{wprobs[word]['rows'].length}"
# @word_memoizer[word]['rows'].sort_by{|r|r['value']}.each{|r|puts [r['value'],r['key']].inspect}
rows = @word_memoizer[word]['rows'].select{|r|(r['key'][1]!='')}.sort_by{|r|r['value']}
row = rows[(-1*[rows.length,5].min)..-1].sort_by{rand}[0]
row ? row['key'][1] : nil
end
word = ARGV[0] word = ARGV[0]
words = [word] words = [word]
wprobs = {}
while word while word
$stdout.print ' ' if words.length > 1 $stdout.print ' ' if words.length > 1
$stdout.print word $stdout.print word
$stdout.flush $stdout.flush
word = probable_follower_for(word)
wprobs[word] ||= db.view('markov/chain-reduce', :startkey => [word,nil], :endkey => [word,{}],:group_level => 2)
# puts
# puts "search #{word} #{wprobs[word]['rows'].length}"
# wprobs[word]['rows'].sort_by{|r|r['value']}.each{|r|puts [r['value'],r['key']].inspect}
rows = wprobs[word]['rows'].select{|r|(r['key'][1]!='')}.sort_by{|r|r['value']}
row = rows[(-1*[rows.length,5].min)..-1].sort_by{rand}[0]
word = row ? row['key'][1] : nil
words << word words << word
end end