Class: MarkovWords::Generator
- Inherits:
-
Object
- Object
- MarkovWords::Generator
- Defined in:
- lib/markov_words/generator.rb
Overview
This class takes care of word generation, caching, and data storage.
Instance Method Summary collapse
-
#cache ⇒ Array<String>
The current list of cached words.
-
#grams ⇒ Hash
The current database of n-gram mappings.
-
#initialize(opts = {}) ⇒ Words
constructor
Create a new "Words" object.
-
#refresh_cache ⇒ Array<String>
"Top off" the cache of stored words, and ensure that it's at
@cache_size
. -
#word ⇒ String
Generate a new word, or return one from the cache if available.
Constructor Details
#initialize(opts = {}) ⇒ Words
Create a new "Words" object
39 40 41 42 43 44 45 46 47 |
# File 'lib/markov_words/generator.rb', line 39 def initialize(opts = {}) @grams = nil @gram_size = opts.fetch :gram_size, 2 @max_length = opts.fetch :max_length, 16 @min_length = opts.fetch :min_length, 3 initialize_cache(opts) initialize_data(opts) end |
Instance Method Details
#cache ⇒ Array<String>
The current list of cached words.
8 9 10 |
# File 'lib/markov_words/generator.rb', line 8 def cache @data_store.retrieve_data(:cache) end |
#grams ⇒ Hash
The current database of n-gram mappings
14 15 16 17 18 |
# File 'lib/markov_words/generator.rb', line 14 def grams @grams = @grams || @data_store.retrieve_data(:grams) || markov_corpus(@corpus_file, @gram_size) end |
#refresh_cache ⇒ Array<String>
"Top off" the cache of stored words, and ensure that it's at
@cache_size
. If perform_caching
is set to false
, returns an empty
array.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/markov_words/generator.rb', line 53 def refresh_cache if @perform_caching words_array = @data_store.retrieve_data(:cache) || [] words_array << generate_word while words_array.length < @cache_size @data_store.store_data(:cache, words_array) words_array else [] end end |
#word ⇒ String
Generate a new word, or return one from the cache if available.
66 67 68 69 70 71 72 |
# File 'lib/markov_words/generator.rb', line 66 def word if @perform_caching load_word_from_cache else generate_word end end |