2008-06-02 00:07:56 +02:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2008-06-02 18:34:04 +02:00
|
|
|
require File.expand_path(File.dirname(__FILE__)) + '/../../couchrest'
|
2008-06-02 00:07:56 +02:00
|
|
|
|
|
|
|
cr = CouchRest.new("http://localhost:5984")
|
2008-06-02 18:34:04 +02:00
|
|
|
@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
|
|
|
|
|
2008-06-02 00:07:56 +02:00
|
|
|
|
|
|
|
word = ARGV[0]
|
|
|
|
words = [word]
|
|
|
|
|
|
|
|
while word
|
|
|
|
$stdout.print ' ' if words.length > 1
|
|
|
|
$stdout.print word
|
|
|
|
$stdout.flush
|
2008-06-02 18:34:04 +02:00
|
|
|
word = probable_follower_for(word)
|
2008-06-02 00:07:56 +02:00
|
|
|
words << word
|
|
|
|
end
|
|
|
|
|
|
|
|
$stdout.print '.'
|
|
|
|
$stdout.flush
|
|
|
|
puts
|
|
|
|
|
|
|
|
# `say #{words.join(' ')}`
|
|
|
|
|