Sha256: 96a670577de38539a7bf9ea6fca2a4e78498008d8f5bfd8b6aa3536e6073658b

Contents?: true

Size: 952 Bytes

Versions: 152

Compression:

Stored size: 952 Bytes

Contents

#!/usr/bin/env ruby

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

cr = CouchRest.new("http://127.0.0.1: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

152 entries across 152 versions & 26 rubygems

Version Path
couchrest-2.0.1 examples/word_count/markov
couchrest-2.0.0 examples/word_count/markov
couchrest-2.0.0.rc3 examples/word_count/markov
couchrest-2.0.0.rc2 examples/word_count/markov
couchrest-2.0.0.rc1 examples/word_count/markov
couchrest-2.0.0.beta2 examples/word_count/markov
couchrest-2.0.0.beta1 examples/word_count/markov
couchrest-1.2.1 examples/word_count/markov
dpla-couchrest-1.2.1.pre.dpla examples/word_count/markov
brianmario-couchrest-0.23 examples/word_count/markov
derfred-couchrest-0.12.6.3 examples/word_count/markov
derfred-couchrest-0.12.6 examples/word_count/markov
gbuesing-couchrest-0.23 examples/word_count/markov
glasner-couchrest-0.2.2 examples/word_count/markov
gohanlonllc-couchrest-0.2.3.1 examples/word_count/markov
halfninja-couchrest-0.23.2 examples/word_count/markov
halfninja-couchrest-0.23.3 examples/word_count/markov
jchris-couchrest-0.12.2 examples/word_count/markov
jchris-couchrest-0.12.4 examples/word_count/markov
jchris-couchrest-0.12.5 examples/word_count/markov