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__"- DEFAULT_DEPTH =
2
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(attributes) ⇒ Dictionary
constructor
A new instance of Dictionary.
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
#chain ⇒ Object
Returns the value of attribute chain.
12 13 14 |
# File 'lib/markovite/dict.rb', line 12 def chain @chain end |
#depth ⇒ Object
Returns the value of attribute depth.
12 13 14 |
# File 'lib/markovite/dict.rb', line 12 def depth @depth end |
#sentence_split ⇒ Object
Returns the value of attribute sentence_split.
11 12 13 |
# File 'lib/markovite/dict.rb', line 11 def sentence_split @sentence_split end |
#sentences ⇒ Object
Returns the value of attribute sentences.
11 12 13 |
# File 'lib/markovite/dict.rb', line 11 def sentences @sentences end |
Instance Method Details
#clear_chain ⇒ Object
67 68 69 |
# File 'lib/markovite/dict.rb', line 67 def clear_chain chain.clear end |
#clear_sentences ⇒ Object
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 (text) new_sentences = sentence_split.split_text(text) sentence_split.(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 |