class Markov::Generate
Attributes
length[R]
start[R]
weight[R]
Public Class Methods
new(dbname, options={})
click to toggle source
# File lib/markov/generate.rb, line 6 def initialize dbname, options={} @length = options[:length] || 200 @weight = options[:weight] || 1.2 @start = options[:start] || "The" @table = options[:table] @db = DB.new(dbname: dbname, chunk: options[:chunk] || 4) end
Public Instance Methods
current_word()
click to toggle source
# File lib/markov/generate.rb, line 14 def current_word @current_word = @word || @start end
lookup(word, table)
click to toggle source
# File lib/markov/generate.rb, line 25 def lookup word, table @lookup = @db.lookup(word, table) { suffices: @lookup.map { |s| s[0] }, probability: @lookup.map.each_with_index { |c,i| [ (c[1].to_i ** @weight).round.times.inject([]){ |r,a| r << i } ] }.flatten } end
next_word()
click to toggle source
# File lib/markov/generate.rb, line 18 def next_word word = current_word.match(/^,/) ? "," : current_word.split(",")[0] words = lookup(CGI.escape(word), @table) index = words[:probability].sample @word = CGI.unescape(words[:suffices][index]) end
text()
click to toggle source
# File lib/markov/generate.rb, line 35 def text (0..@length).inject("#@start "){ |r,a| next_word r << "#{current_word.match(/^,/) ? "," : current_word.split(",")[0]} " }.strip.squeeze(" ").gsub(/\s(\.|\,|:|;|`|'|\?|!)/,"\\1") end