class Dictionary

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

Constants

BEGINNING

make this a module???

DEFAULT_DEPTH
ENDING

Attributes

chain[R]
depth[R]
sentence_split[RW]
sentences[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/markovite/dict.rb, line 14
def initialize(attributes)
  attributes.each {|attribute, value| self.send("#{attribute}=", value)}
  set_default
end

Public Instance Methods

chain=(arg) click to toggle source
# File lib/markovite/dict.rb, line 23
def chain=(arg)
  # The following line ensures a new array is created for each new key
  # instead of using the memory address of the first array created
  # as the default value
  @chain = Hash.new { |h, k| h[k] = [] } if self.chain.nil?
  arg.each {|key, value|chain[key] = value}
  chain
end
clear_chain() click to toggle source
# File lib/markovite/dict.rb, line 67
def clear_chain
  chain.clear
end
clear_sentences() click to toggle source
# File lib/markovite/dict.rb, line 71
def clear_sentences
  sentences.clear
end
construct_chain(new_sentences = nil) click to toggle source
# 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
depth=(arg) click to toggle source
# File lib/markovite/dict.rb, line 32
def depth=(arg)
  raise "Depth cannot be changed" if depth
  raise "Depth must be integer" if arg.class != Integer
  @depth = arg
end
expand_chain(text) click to toggle source
# 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) click to toggle source
# File lib/markovite/dict.rb, line 19
def has_sentence(sentence)
  sentences.include?(sentence)
end

Private Instance Methods

set_default() click to toggle source
# File lib/markovite/dict.rb, line 77
def set_default
  self.sentence_split = sentence_split || SentenceSplit.new
  self.chain = chain || {}
  self.sentences = sentences || sentence_split.split_text
  construct_chain if chain.empty?
end