class Markovite::Chain

Constants

DEFAULT_DEPTH
MAX_DEPTH
MAX_FILENAME_LENGTH
MIN_DEPTH

Attributes

chainer[RW]
depth[R]
dictionary[RW]
split[RW]

Public Class Methods

combine(left_chain, right_chain, dict_depth = nil) click to toggle source
# File lib/markovite.rb, line 41
def self.combine(left_chain, right_chain, dict_depth = nil)
  dict_depth = dict_depth || left_chain.depth
  new_chain = Markovite::Chain.new
  new_chain.parse_string(left_chain.corpus, dict_depth)
  new_chain.parse_string(right_chain.corpus, dict_depth)
  new_chain
end
new(filename = nil, dict_depth=DEFAULT_DEPTH) click to toggle source
# File lib/markovite.rb, line 13
def initialize(filename = nil, dict_depth=DEFAULT_DEPTH)
  initialize_children
  parse_file(filename, dict_depth) if filename
end

Public Instance Methods

<<(corpus) click to toggle source
# File lib/markovite.rb, line 70
def << (corpus)
  if is_file?(corpus)
    parse_file(corpus)
  else
    parse_string(corpus)
  end
end
corpus() click to toggle source
# File lib/markovite.rb, line 49
def corpus
  split.corpus
end
load(filename) click to toggle source
# File lib/markovite.rb, line 31
def load(filename)
  raise("Invalid file type") if !is_valid_file_ext?(filename, /.msg\z/i)
  data = File.read("#{filename}")
  model = MessagePack.unpack(data)
  @depth = model["depth"]
  split.corpus = model["corpus"]
  dictionary.sentences = model["sentences"]
  dictionary.chain = model["chain"]
end
make_sentence() click to toggle source

Future Self: Make this a module####

# File lib/markovite.rb, line 82
def make_sentence
  chainer.make_sentence
end
make_sentence_of_length(how_long) click to toggle source
# File lib/markovite.rb, line 94
def make_sentence_of_length(how_long)
  chainer.make_sentence_of_length(how_long)
end
make_sentence_starts_with(phrase) click to toggle source
# File lib/markovite.rb, line 90
def make_sentence_starts_with(phrase)
  chainer.make_sentence_starts_with(phrase)
end
make_sentences(amount) click to toggle source
# File lib/markovite.rb, line 86
def make_sentences(amount)
  chainer.make_sentences(amount)
end
parse_file(filename, dict_depth = DEFAULT_DEPTH) click to toggle source
# File lib/markovite.rb, line 64
def parse_file(filename, dict_depth = DEFAULT_DEPTH)
  raise "Invalid file type" if !is_valid_file_ext?(filename)
  text = File.read(filename)
  parse_string(text, dict_depth)
end
parse_string(text, dict_depth = nil) click to toggle source
# File lib/markovite.rb, line 53
def parse_string(text, dict_depth = nil)
  if self.depth
    depth_check(dict_depth)
    add_from_text(text)
  else
    dict_depth = dict_depth || DEFAULT_DEPTH
    is_valid_depth?(dict_depth)
    new_from_text(text, dict_depth)
  end
end
save(filename) click to toggle source
# File lib/markovite.rb, line 18
def save(filename)
  raise("Chain is empty") if dictionary.chain.empty?
  msg_hash = {}
  msg_hash["sentences"] = dictionary.sentences
  msg_hash["chain"] = dictionary.chain
  msg_hash["corpus"] = split.corpus
  msg_hash["depth"] = dictionary.depth
  File.open("#{filename}.msg", "w") do |file|
    test = file.write(msg_hash.to_msgpack)
  end
  true
end

Private Instance Methods

add_from_text(text) click to toggle source
# File lib/markovite.rb, line 136
def add_from_text(text)
  dictionary.expand_chain(text)
end
depth_check(dict_depth) click to toggle source
# File lib/markovite.rb, line 102
def depth_check(dict_depth)
    raise "Chain depth conflict" if !dict_depth.nil? && dict_depth != depth
end
initialize_children() click to toggle source
# File lib/markovite.rb, line 130
def initialize_children
  self.split = SplitSentence.new
  self.dictionary = Dictionary.new({sentence_split: split})
  self.chainer = Chainer.new(dictionary)
end
is_file?(str) click to toggle source
# File lib/markovite.rb, line 126
def is_file?(str)
  str.length < 255 && split_words(str).length == 1
end
is_valid_depth?(dict_depth) click to toggle source
# File lib/markovite.rb, line 106
def is_valid_depth?(dict_depth)
  dict_depth >= MIN_DEPTH && dict_depth <= MAX_DEPTH
end
is_valid_file_ext?(filename, ext = nil) click to toggle source
# File lib/markovite.rb, line 121
def is_valid_file_ext?(filename, ext = nil)
  re = ext || Regexp.union(FILE_EXT)
  filename.match(re)
end
new_from_text(text, dict_depth) click to toggle source
# File lib/markovite.rb, line 114
def new_from_text(text, dict_depth)
  @depth = dict_depth
  @corpus = split.corpus
  dictionary.expand_chain(text)
end
split_words(str) click to toggle source
# File lib/markovite.rb, line 110
def split_words(str)
  str.split(" ")
end