Class: MarkovWords::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/markov_words/generator.rb

Overview

This class takes care of word generation, caching, and data storage.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Words

Create a new "Words" object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :cache_size (Integer)

    How many words to pre-calculate + store in the cache for quick retrieval

  • :corpus_file (String) — default: '/usr/share/dict/words'

    Your dictionary of words.

  • :data_file (String)

    Location where calculations are persisted to disk.

  • :flush_data (String)

    Remove any previously-stored data from an existing database file.

  • :gram_size (Integer) — default: 2

    Number of n-grams to compute for your database.

  • :max_length (Integer) — default: 16

    Max generated word length.

  • :min_length (Integer) — default: 3

    Minimum generated word length. NOTE: If your corpus size is very small (<1000 words or so), it's hard to guarantee a min_length because so many n-grams will have no association, which terminates word generation.

  • :perform_caching (Boolean) — default: true

    Perform caching?



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

#cacheArray<String>

The current list of cached words.

Returns:

  • (Array<String>)

    All words in the cache.



8
9
10
# File 'lib/markov_words/generator.rb', line 8

def cache
  @data_store.retrieve_data(:cache)
end

#gramsHash

The current database of n-gram mappings

Returns:

  • (Hash)

    n-gram database



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_cacheArray<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.

Returns:

  • (Array<String>)

    All words in the cache.



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

#wordString

Generate a new word, or return one from the cache if available.

Returns:

  • (String)

    The word.



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