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__"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sentence_split, depth = 2) ⇒ Dictionary



12
13
14
15
16
17
18
19
20
21
# File 'lib/markovite/dict.rb', line 12

def initialize(sentence_split, depth = 2)
  self.sentence_split = sentence_split
  # The following line ensures a new array is created for each new key
  # instead of using the memory address of the first array created    raise exception "First argument must contain a SplitSentence instance" if sentence_split.class != SentenceSplit
  # as the default value
  self.chain = Hash.new { |h, k| h[k] = [] }
  self.sentences = sentence_split.sentences
  @depth = depth
  construct_chain
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain.



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

def chain
  @chain
end

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#sentence_splitObject

Returns the value of attribute sentence_split.



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

def sentence_split
  @sentence_split
end

#sentencesObject

Returns the value of attribute sentences.



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

def sentences
  @sentences
end

Instance Method Details

#clear_chainObject



59
60
61
# File 'lib/markovite/dict.rb', line 59

def clear_chain
  chain.clear
end

#clear_sentencesObject



63
64
65
# File 'lib/markovite/dict.rb', line 63

def clear_sentences
  sentences.clear
end

#construct_chain(new_sentences = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/markovite/dict.rb', line 37

def construct_chain(new_sentences = nil)
  new_sentences = new_sentences || sentences
  raise "No sentences in memory" if new_sentences.empty?
  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



31
32
33
34
35
# File 'lib/markovite/dict.rb', line 31

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

#has_sentence(sentence) ⇒ Object



23
24
25
# File 'lib/markovite/dict.rb', line 23

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