class Remarkovable
Attributes
markov_model[RW]
Public Class Methods
new(string:, prefix_length: 2)
click to toggle source
# File lib/remarkovable.rb, line 4 def initialize(string:, prefix_length: 2) return if string.nil? build_markov_model(string, prefix_length) end
Public Instance Methods
speak(custom_key: nil)
click to toggle source
# 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
Private Instance Methods
add_triad(key:, match:)
click to toggle source
# File lib/remarkovable.rb, line 55 def add_triad(key:, match:) @markov_model[key] = {} unless @markov_model[key] @markov_model[key] += [match] end
assign_key(custom_key)
click to toggle source
# File lib/remarkovable.rb, line 47 def assign_key(custom_key) if !custom_key.nil? && @markov_model.include?(custom_key) custom_key else @markov_model.keys.sample end end
build_markov_model(string, prefix_length)
click to toggle source
# File lib/remarkovable.rb, line 30 def build_markov_model(string, prefix_length) @markov_model = Hash.new { |hash, key| hash[key] = [] } words = string.split(/([.!?])|\s+/) words.each_with_index do |word, i| key = [word] (prefix_length - 1).times do |n| next_word = i + n + 1 key << words[next_word] end key = key.join(' ') match = words[i + prefix_length] add_triad(key: key, match: match) if i < words.size - prefix_length end end