Class: Dictionary
- Inherits:
-
Object
- Object
- Dictionary
- 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
-
#chain ⇒ Object
Returns the value of attribute chain.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#sentence_split ⇒ Object
Returns the value of attribute sentence_split.
-
#sentences ⇒ Object
Returns the value of attribute sentences.
Instance Method Summary collapse
- #clear_chain ⇒ Object
- #clear_sentences ⇒ Object
- #construct_chain(new_sentences = nil) ⇒ Object
- #expand_chain(text) ⇒ Object
- #has_sentence(sentence) ⇒ Object
-
#initialize(sentence_split, depth = 2) ⇒ Dictionary
constructor
A new instance of Dictionary.
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
#chain ⇒ Object
Returns the value of attribute chain.
10 11 12 |
# File 'lib/markovite/dict.rb', line 10 def chain @chain end |
#depth ⇒ Object
Returns the value of attribute depth.
11 12 13 |
# File 'lib/markovite/dict.rb', line 11 def depth @depth end |
#sentence_split ⇒ Object
Returns the value of attribute sentence_split.
10 11 12 |
# File 'lib/markovite/dict.rb', line 10 def sentence_split @sentence_split end |
#sentences ⇒ Object
Returns the value of attribute sentences.
10 11 12 |
# File 'lib/markovite/dict.rb', line 10 def sentences @sentences end |
Instance Method Details
#clear_chain ⇒ Object
59 60 61 |
# File 'lib/markovite/dict.rb', line 59 def clear_chain chain.clear end |
#clear_sentences ⇒ Object
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 (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 |