Class: MarkovPolo::Chain
- Inherits:
-
Object
- Object
- MarkovPolo::Chain
- Defined in:
- lib/markov-polo.rb
Constant Summary collapse
- START_TOKEN =
"__start__"
- END_TOKEN =
"__end__"
Instance Method Summary collapse
- #<<(content) ⇒ Object
- #generate ⇒ Object
-
#initialize(hash = {}) ⇒ Chain
constructor
A new instance of Chain.
- #load(hash) ⇒ Object
- #push(content) ⇒ Object
- #to_h ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Chain
Returns a new instance of Chain.
8 9 10 |
# File 'lib/markov-polo.rb', line 8 def initialize hash={} @data = hash end |
Instance Method Details
#<<(content) ⇒ Object
12 |
# File 'lib/markov-polo.rb', line 12 def << content; push content; end |
#generate ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Object
31 |
# File 'lib/markov-polo.rb', line 31 def load hash; @data = hash; end |
#push(content) ⇒ Object
14 15 16 17 18 19 20 21 |
# 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 ⇒ Object
29 |
# File 'lib/markov-polo.rb', line 29 def to_h; @data; end |
#to_hash ⇒ Object
30 |
# File 'lib/markov-polo.rb', line 30 def to_hash; to_h; end |