Class: CorrectHorseBatteryStaple::Generator
- Inherits:
-
Object
- Object
- CorrectHorseBatteryStaple::Generator
- Defined in:
- lib/correct_horse_battery_staple/generator.rb
Overview
Generate an N-word passphrase from a corpus
Instance Attribute Summary collapse
-
#corpus ⇒ Object
Returns the value of attribute corpus.
-
#word_length ⇒ Object
Returns the value of attribute word_length.
Instance Method Summary collapse
- #estimate_entropy(options) ⇒ Object
-
#initialize(corpus, word_length = nil) ⇒ Generator
constructor
A new instance of Generator.
- #make(count = 4, options = {}) ⇒ Object
- #words ⇒ Object
Methods included from Memoize
Methods included from Common
#array_sample, #logger, #random_in_range, #random_number, #set_sample
Constructor Details
#initialize(corpus, word_length = nil) ⇒ Generator
Returns a new instance of Generator.
13 14 15 16 17 18 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 13 def initialize(corpus, word_length = nil) @corpus = corpus if word_length @corpus.filter {|entry| word_length.include?(entry.word.to_s.length) } end end |
Instance Attribute Details
#corpus ⇒ Object
Returns the value of attribute corpus.
11 12 13 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 11 def corpus @corpus end |
#word_length ⇒ Object
Returns the value of attribute word_length.
11 12 13 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 11 def word_length @word_length end |
Instance Method Details
#estimate_entropy(options) ⇒ Object
26 27 28 29 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 26 def estimate_entropy() candidate_count = @corpus.count_candidates() (log(candidate_count) / log(2)).floor end |
#make(count = 4, options = {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 20 def make(count = 4, = {}) @corpus.pick(count, ). map {|entry| entry.word.downcase }. join("-") end |
#words ⇒ Object
32 33 34 |
# File 'lib/correct_horse_battery_staple/generator.rb', line 32 def words @words ||= @corpus.result end |