class MarkovPolo::Chain

Constants

END_TOKEN
START_TOKEN

Public Class Methods

new(hash={}) click to toggle source
# File lib/markov-polo.rb, line 8
def initialize hash={}
  @data = hash
end

Public Instance Methods

<<(content;) click to toggle source
# File lib/markov-polo.rb, line 12
def << content; push content; end
generate() click to toggle source
# File lib/markov-polo.rb, line 33
def generate
  last = START_TOKEN
  total = []
  while last != END_TOKEN
    choices = []
    @data[last].each do |key, val|
      val.times { choices << key }
    end
    chosen = choices.sample

    total << chosen unless chosen == END_TOKEN
    last = chosen
  end
  total.join " "
end
load(hash;) click to toggle source
# File lib/markov-polo.rb, line 31
def load hash; @data = hash; end
push(content) click to toggle source
# File lib/markov-polo.rb, line 14
def push content
  last = START_TOKEN
  content.split.each do |word|
    add_member last, word
    last = word
  end
  add_member last, END_TOKEN
end
to_h() click to toggle source
# File lib/markov-polo.rb, line 29
def to_h; @data; end
to_hash() click to toggle source
# File lib/markov-polo.rb, line 30
def to_hash; to_h; end

Private Instance Methods

add_member(last, word) click to toggle source
# File lib/markov-polo.rb, line 23
def add_member last, word
  @data[last] = {} unless @data.include? last
  @data[last][word] = 0 unless @data[last].include? word
  @data[last][word] += 1
end