Sha256: 3fc857294f3da54af77bc4e382a0138356a044ad3d7c4d45e0e3f5a36164e1bb

Contents?: true

Size: 952 Bytes

Versions: 11

Compression:

Stored size: 952 Bytes

Contents

#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__)) + '/../../couchrest'

cr = CouchRest.new("http://localhost:5984")
@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]
words = [word]

while word
  $stdout.print ' ' if words.length > 1
  $stdout.print word
  $stdout.flush
  word = probable_follower_for(word)
  words << word
end

$stdout.print '.'
$stdout.flush
puts

# `say #{words.join(' ')}`

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
jchris-couchrest-0.9.10 examples/word_count/markov
jchris-couchrest-0.9.11 examples/word_count/markov
jchris-couchrest-0.9.12 examples/word_count/markov
jchris-couchrest-0.9.4 examples/word_count/markov
jchris-couchrest-0.9.5 examples/word_count/markov
jchris-couchrest-0.9.6 examples/word_count/markov
jchris-couchrest-0.9.7 examples/word_count/markov
jchris-couchrest-0.9.8 examples/word_count/markov
jchris-couchrest-0.9.9 examples/word_count/markov
juozasg-couchrest-0.10.1 examples/word_count/markov
topfunky-couchrest-0.9.4 examples/word_count/markov