class MarkovChains::Generator
Public Class Methods
new(text, order = 1)
click to toggle source
Initializes the generator
@example Create a new generator
MarkovChains::Generator.new(text)
@param Text source to generate sentences from
# File lib/markov_chains/generator.rb, line 11 def initialize(text, order = 1) @dict = MarkovChains::Dictionary.new(text, order) end
Public Instance Methods
get_sentences(n)
click to toggle source
Returns a given number of randonly generated sentences
@example Get 5 sentences
get_sentences(5)
@param n [int] number of sentences to generate @return Array conataining generated sentences
# File lib/markov_chains/generator.rb, line 23 def get_sentences(n) sentences = [] n.times do sentence = @dict.get_start_words while nw = @dict.get(sentence[-@dict.order, @dict.order]) sentence << nw end sentences << (sentence[0...-1].join(" ").gsub(/\s([,;:])/, '\1') << sentence.last) end sentences end