class Chainer
class
Constants
- BEGINNING
- ENDING
- MAX_ATTEMPTS
Attributes
depth[R]
dictionary[RW]
Public Class Methods
new(dictionary)
click to toggle source
# File lib/markovite/chainer.rb, line 11 def initialize(dictionary) self.dictionary = dictionary @depth = dictionary.depth #takes in a dictionary object #constructs chain from dictionary object public interface end
Public Instance Methods
depth=(arg)
click to toggle source
# File lib/markovite/chainer.rb, line 44 def depth=(arg) raise "Depth cannot be changed" end
make_sentence()
click to toggle source
# File lib/markovite/chainer.rb, line 48 def make_sentence raise "No corpus in memory" if dictionary.nil? attempts = 0 while attempts < MAX_ATTEMPTS sentence = generate_text if is_valid_sentence?(sentence) return sentence else attempts += 1 end end nil end
make_sentence_of_length(how_long)
click to toggle source
# File lib/markovite/chainer.rb, line 28 def make_sentence_of_length(how_long) begin make_sentence_with_block {|sentence| sentence.length <= how_long} rescue NoMethodError return nil end end
make_sentence_starts_with(phrase)
click to toggle source
# File lib/markovite/chainer.rb, line 18 def make_sentence_starts_with(phrase) chunk = get_chunk(phrase) begin partial = generate_text(chunk) rescue ArgumentError return nil end "#{phrase} #{partial}" end
make_sentences(amount, condition=true)
click to toggle source
# File lib/markovite/chainer.rb, line 36 def make_sentences(amount, condition=true) sentences = [] amount.times do sentences << make_sentence end sentences.join(' ') end
Private Instance Methods
close_open_quote(sentence)
click to toggle source
# File lib/markovite/chainer.rb, line 107 def close_open_quote(sentence) if has_open_quote?(sentence) "#{sentence}\"" else sentence end end
generate_text(current_chunk = [BEGINNING] * depth)
click to toggle source
# File lib/markovite/chainer.rb, line 115 def generate_text(current_chunk = [BEGINNING] * depth) sentence = current_chunk while current_chunk.last != ENDING sentence << pick_next(current_chunk) current_chunk = sentence.last(depth) end remove_markers(sentence) sentence = sentence.join(' ') close_open_quote(sentence) end
get_chunk(phrase)
click to toggle source
# File lib/markovite/chainer.rb, line 76 def get_chunk(phrase) words = phrase.split(' ') chunk = words.last(depth) while chunk.size < depth chunk.unshift(BEGINNING) end chunk end
has_open_quote?(sentence)
click to toggle source
# File lib/markovite/chainer.rb, line 102 def has_open_quote?(sentence) count = sentence.count("\"") count.odd? end
is_valid_sentence?(sentence)
click to toggle source
# File lib/markovite/chainer.rb, line 98 def is_valid_sentence?(sentence) !dictionary.has_sentence(sentence) end
make_sentence_with_block() { |sentence| ... }
click to toggle source
# File lib/markovite/chainer.rb, line 64 def make_sentence_with_block attempts = 0 while attempts < MAX_ATTEMPTS sentence = make_sentence if yield(sentence) return sentence else attempts += 1 end end end
pick_next(words)
click to toggle source
# File lib/markovite/chainer.rb, line 85 def pick_next(words) word_list = dictionary.chain[words] raise ArgumentError, "No matching state" if word_list.empty? word_list.sample end
remove_markers(sentence)
click to toggle source
# File lib/markovite/chainer.rb, line 91 def remove_markers(sentence) #removes BEGINNING and ENDING markers sentence.shift while sentence.first == BEGINNING sentence.pop while sentence.last == ENDING sentence end