Rubykov
Sensible, easy Markov chains in Ruby.
Installation
Simply add:
gem 'rubykov'
to your Gemfile.
Usage
Markov models are generated by an order and a set of training data.
For example, to create a first-order Markov model given some weather data:
markov_model = Rubykov::MarkovModel.new(1, ['sunny','rainy','sunny','sunny','sunny','windy','sunny','rainy','windy','rainy'])
To obtain predictions, use the chain method, which returns an enumerator:
markov_chain = markov_model.chain
markov_chain.next
=> "sunny"
There is a text generator subclass which has useful methods for outputting character- and word-limited strings. The following is a second-order Markov model:
text_generator = Rubykov::TextGenerator.new(2, 'The quick brown fox jumps over the brown fox who is slow jumps over the brown fox who is dead.')
text_generator.character_limited_output(140)
=> "The quick brown fox who is dead."
Testing
To run the test suite simply execute:
rspec
(c) Evan Hemsley 2014