Class: Dictionary

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

Overview

class that takes a training corpus and creates a hash that represents a markov chain state machine

Constant Summary collapse

BEGINNING =

make this a module???

"__BEGIN__"
ENDING =
"__END__"
DEFAULT_DEPTH =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Dictionary

Returns a new instance of Dictionary.



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

def initialize(attributes)
  attributes.each {|attribute, value| self.send("#{attribute}=", value)}
  set_default
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



12
13
14
# File 'lib/markovite/dict.rb', line 12

def chain
  @chain
end

#depthObject

Returns the value of attribute depth.



12
13
14
# File 'lib/markovite/dict.rb', line 12

def depth
  @depth
end

#sentence_splitObject

Returns the value of attribute sentence_split.



11
12
13
# File 'lib/markovite/dict.rb', line 11

def sentence_split
  @sentence_split
end

#sentencesObject

Returns the value of attribute sentences.



11
12
13
# File 'lib/markovite/dict.rb', line 11

def sentences
  @sentences
end

Instance Method Details

#clear_chainObject



67
68
69
# File 'lib/markovite/dict.rb', line 67

def clear_chain
  chain.clear
end

#clear_sentencesObject



71
72
73
# File 'lib/markovite/dict.rb', line 71

def clear_sentences
  sentences.clear
end

#construct_chain(new_sentences = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/markovite/dict.rb', line 45

def construct_chain(new_sentences = nil)
  self.depth = DEFAULT_DEPTH if depth.nil?
  new_sentences = new_sentences || sentences
  new_sentences.each do |sentence|
    words = sentence.split(" ")
    # each chunk is an array that represents a state in the markov chain
    # it is a key that points to the next possible states
    chunk = [BEGINNING] * depth
    words.each do |word|
      # using a clone of the chunk ensures the VALUE
      # of the chunk is used as the key, instead of
      # whatever is stored at the memory address
      # of the initial chunk
      chain[chunk.clone] << word
      chunk.shift
      chunk.push(word)
    end
    chain[chunk] << ENDING
  end
  chain
end

#expand_chain(text) ⇒ Object



38
39
40
41
42
43
# File 'lib/markovite/dict.rb', line 38

def expand_chain(text)
  new_sentences = sentence_split.split_text(text)
  sentence_split.expand_corpus(text)
  self.sentences += new_sentences
  construct_chain(new_sentences)
end

#has_sentence(sentence) ⇒ Object



19
20
21
# File 'lib/markovite/dict.rb', line 19

def has_sentence(sentence)
  sentences.include?(sentence)
end