Class: Remarkovable
- Inherits:
-
Object
- Object
- Remarkovable
- Defined in:
- lib/remarkovable.rb
Instance Attribute Summary collapse
-
#markov_model ⇒ Object
Returns the value of attribute markov_model.
Instance Method Summary collapse
-
#initialize(string:, prefix_length: 2) ⇒ Remarkovable
constructor
A new instance of Remarkovable.
- #speak(custom_key: nil) ⇒ Object
Constructor Details
#initialize(string:, prefix_length: 2) ⇒ Remarkovable
Returns a new instance of Remarkovable.
4 5 6 7 |
# File 'lib/remarkovable.rb', line 4 def initialize(string:, prefix_length: 2) return if string.nil? build_markov_model(string, prefix_length) end |
Instance Attribute Details
#markov_model ⇒ Object
Returns the value of attribute markov_model.
2 3 4 |
# File 'lib/remarkovable.rb', line 2 def markov_model @markov_model end |
Instance Method Details
#speak(custom_key: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/remarkovable.rb', line 9 def speak(custom_key: nil) return if @markov_model.nil? key = assign_key(custom_key) output = Array(key.capitalize.split(' ')) until (output & %w(. ! ?)).any? match = @markov_model[key].sample if match.nil? key = @markov_model.keys.sample next end output << match output.flatten! key = [output[-2], output[-1]].join(' ') end output.join(' ').gsub(/\s+([,.!?])/, '\1') end |