class MarkovChain
Public Class Methods
from(data)
click to toggle source
data is data() of
the other MarkovChain. It becomes owned by
the returned MarkovChain.
# File lib/lib/markov_chain.rb, line 8 def self.from(data) new(data) end
new(data = {})
click to toggle source
creates an empty MarkovChain.
data is a map which becomes owned by this MarkovChain.
# File lib/lib/markov_chain.rb, line 17 def initialize(data = {}) @data = data @last_state = nil end
Public Instance Methods
append!(states)
click to toggle source
appends states to the end of this MarkovChain.
states is an Array of arbitrary objects.
It returns this (modified) MarkovChain.
# File lib/lib/markov_chain.rb, line 29 def append!(states) for next_state in states state_occurences_map = (@data[@last_state] or Hash.new) state_occurences_map[next_state] ||= 0 state_occurences_map[next_state] += 1 @data[@last_state] = state_occurences_map @last_state = next_state end return self end
data()
click to toggle source
predict()
click to toggle source
returns Enumerable of predicted states. The states are predicted by states passed to append!().
The result may contain nils. Each nil means that MarkovChain could not predict a state after the one before nil. Example:
markov_chain.predict().take(4) #=> ["a", "c", "b", nil]
That means markov_chain could not predict a state after “b”.
# File lib/lib/markov_chain.rb, line 51 def predict() self.extend(Prediction) end