Class: Keybox::CharGramGenerator
- Inherits:
-
StringGenerator
- Object
- StringGenerator
- Keybox::CharGramGenerator
- Defined in:
- lib/keybox/string_generator.rb
Overview
CharGramGenerator emits a sequence that is a CharGram of length N. The ngrams can be the default ones that ship with keybox or an array passed in.
Instance Attribute Summary collapse
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
Attributes inherited from StringGenerator
#autoclear, #chunks, #max_length, #min_length
Instance Method Summary collapse
-
#generate_chunk ⇒ Object
return a random chargram from the pool indidcated by the last character of the last emitted item.
-
#initialize(chargram_list = nil) ⇒ CharGramGenerator
constructor
A new instance of CharGramGenerator.
- #size ⇒ Object
Methods inherited from StringGenerator
#clear, #generate, #to_s, #valid?
Constructor Details
#initialize(chargram_list = nil) ⇒ CharGramGenerator
Returns a new instance of CharGramGenerator.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/keybox/string_generator.rb', line 54 def initialize(chargram_list = nil) super() wordlist = chargram_list || load_default_chargram_list @pool = Hash.new wordlist.each do |word| letters = word.split('') letter_set = @pool[letters.first] ||= Array.new letter_set << word end end |
Instance Attribute Details
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
51 52 53 |
# File 'lib/keybox/string_generator.rb', line 51 def pool @pool end |
Instance Method Details
#generate_chunk ⇒ Object
return a random chargram from the pool indidcated by the last character of the last emitted item
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/keybox/string_generator.rb', line 71 def generate_chunk pool_key = "" if @chunks.size > 0 then pool_key = @chunks.pop else pool_key = @randomizer.pick_one_from(@pool.keys) end new_chunk = @randomizer.pick_one_from(@pool[pool_key]) @chunks.concat(new_chunk.split(//)) new_chunk end |
#size ⇒ Object
65 66 67 |
# File 'lib/keybox/string_generator.rb', line 65 def size @pool.inject(0) { |sum,h| sum + h[1].size } end |