Class: MarkyMarkov::TemporaryDictionary
- Inherits:
-
Object
- Object
- MarkyMarkov::TemporaryDictionary
- Defined in:
- lib/marky_markov.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#clear! ⇒ Object
Clears the temporary dictionary’s hash, useful for keeping the same dictionary object but removing the words it has learned.
-
#dictionary ⇒ Hash
Returns the MarkovDictionary objects dictionary hash.
-
#generate_n_sentences(sentencecount) ⇒ String
Generates n sentences using the dictionary generated via parse_string or parse_file.
-
#generate_n_words(wordcount) ⇒ String
Generates a sentence/sentences of n words using the dictionary generated via parse_string or parse_file.
-
#initialize(depth = 2) ⇒ Object
constructor
Create a new Temporary Markov Chain Dictionary and sentence generator for use.
-
#method_missing(method_sym, *args, &block) ⇒ String
Dynamically call generate_n_words or generate_n_sentences if an Int is substituted for the n in the method call.
-
#parse_file(location) ⇒ Object
Parses a given file and adds the sentences it contains to the current dictionary.
-
#parse_string(string) ⇒ Object
Parses a given string and adds them to the current dictionary.
-
#respond_to_missing?(method_sym, include_private) ⇒ Boolean
Modify respond_to_missing? to include generate_n_words and generate_n_sentences method_missing implementation.
Constructor Details
#initialize(depth = 2) ⇒ Object
Create a new Temporary Markov Chain Dictionary and sentence generator for use. Depth defaults to two words but can be set to any number between 1 and 9.
24 25 26 27 |
# File 'lib/marky_markov.rb', line 24 def initialize(depth=2) @dictionary = MarkovDictionary.new(depth) @sentence = MarkovSentenceGenerator.new(@dictionary) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ String
Dynamically call generate_n_words or generate_n_sentences if an Int is substituted for the n in the method call.
91 92 93 94 95 96 97 98 99 |
# File 'lib/marky_markov.rb', line 91 def method_missing(method_sym, *args, &block) if method_sym.to_s =~ /^generate_(\d*)_word[s]*$/ generate_n_words($1.to_i) elsif method_sym.to_s =~ /^generate_(\d*)_sentence[s]*$/ generate_n_sentences($1.to_i) else super end end |
Instance Method Details
#clear! ⇒ Object
Clears the temporary dictionary’s hash, useful for keeping the same dictionary object but removing the words it has learned.
119 120 121 |
# File 'lib/marky_markov.rb', line 119 def clear! @dictionary.dictionary.clear end |
#dictionary ⇒ Hash
Returns the MarkovDictionary objects dictionary hash.
31 32 33 |
# File 'lib/marky_markov.rb', line 31 def dictionary @dictionary.dictionary end |
#generate_n_sentences(sentencecount) ⇒ String
Generates n sentences using the dictionary generated via parse_string or parse_file. A sentence is defined as beginning with a capitalized word and ending with either a . ! or ?
77 78 79 |
# File 'lib/marky_markov.rb', line 77 def generate_n_sentences(sentencecount) @sentence.generate_sentence(sentencecount) end |
#generate_n_words(wordcount) ⇒ String
Generates a sentence/sentences of n words using the dictionary generated via parse_string or parse_file.
62 63 64 |
# File 'lib/marky_markov.rb', line 62 def generate_n_words(wordcount) @sentence.generate(wordcount) end |
#parse_file(location) ⇒ Object
Parses a given file and adds the sentences it contains to the current dictionary.
40 41 42 |
# File 'lib/marky_markov.rb', line 40 def parse_file(location) @dictionary.parse_source(location, true) end |
#parse_string(string) ⇒ Object
Parses a given string and adds them to the current dictionary.
49 50 51 |
# File 'lib/marky_markov.rb', line 49 def parse_string(string) @dictionary.parse_source(string, false) end |
#respond_to_missing?(method_sym, include_private) ⇒ Boolean
Modify respond_to_missing? to include generate_n_words and generate_n_sentences method_missing implementation.
104 105 106 107 108 109 110 111 112 |
# File 'lib/marky_markov.rb', line 104 def respond_to_missing?(method_sym, include_private) if method_sym.to_s =~ /^generate_(\d*)_word[s]*$/ true elsif method_sym.to_s =~ /^generate_(\d*)_sentence[s]*$/ true else super end end |