Module: MarkovChain::Prediction
- Includes:
- Enumerable
- Defined in:
- lib/markov_chain.rb
Overview
This module is only intended for inclusion into MarkovChain.
Instance Method Summary collapse
Instance Method Details
#each ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/markov_chain.rb', line 73 def each # last_state = @last_state loop do # next_state = begin state_occurences_map = (@data[last_state] or Hash.new) occurences_sum = state_occurences_map.reduce(0) do |sum, entry| sum + entry[1] end choice = rand(occurences_sum + 1) chosen_state_and_occurences = state_occurences_map.find do |state, occurences| choice -= occurences choice <= 0 end chosen_state_and_occurences ||= [nil, nil] chosen_state_and_occurences[0] end # yield next_state # last_state = next_state end end |