Class: Markovite::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/markovite.rb

Constant Summary collapse

MIN_DEPTH =
1
MAX_DEPTH =
4
DEFAULT_DEPTH =
2
MAX_FILENAME_LENGTH =
255

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, dict_depth = DEFAULT_DEPTH) ⇒ Chain

Returns a new instance of Chain.



13
14
15
16
# File 'lib/markovite.rb', line 13

def initialize(filename = nil, dict_depth=DEFAULT_DEPTH)
  initialize_children
  parse_file(filename, dict_depth) if filename
end

Instance Attribute Details

#chainerObject

Returns the value of attribute chainer.



6
7
8
# File 'lib/markovite.rb', line 6

def chainer
  @chainer
end

#depthObject (readonly)

Returns the value of attribute depth.



7
8
9
# File 'lib/markovite.rb', line 7

def depth
  @depth
end

#dictionaryObject

Returns the value of attribute dictionary.



6
7
8
# File 'lib/markovite.rb', line 6

def dictionary
  @dictionary
end

#splitObject

Returns the value of attribute split.



6
7
8
# File 'lib/markovite.rb', line 6

def split
  @split
end

Class Method Details

.combine(left_chain, right_chain, dict_depth = nil) ⇒ Object



41
42
43
44
45
46
47
# 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

Instance Method Details

#<<(corpus) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/markovite.rb', line 70

def << (corpus)
  if is_file?(corpus)
    parse_file(corpus)
  else
    parse_string(corpus)
  end
end

#corpusObject



49
50
51
# File 'lib/markovite.rb', line 49

def corpus
  split.corpus
end

#load(filename) ⇒ Object



31
32
33
34
35
36
37
38
39
# 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_sentenceObject



82
83
84
# File 'lib/markovite.rb', line 82

def make_sentence
  chainer.make_sentence
end

#make_sentence_of_length(how_long) ⇒ Object



94
95
96
# 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) ⇒ Object



90
91
92
# File 'lib/markovite.rb', line 90

def make_sentence_starts_with(phrase)
  chainer.make_sentence_starts_with(phrase)
end

#make_sentences(amount) ⇒ Object



86
87
88
# File 'lib/markovite.rb', line 86

def make_sentences(amount)
  chainer.make_sentences(amount)
end

#parse_file(filename, dict_depth = DEFAULT_DEPTH) ⇒ Object



64
65
66
67
68
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# 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