Class: CorrectHorseBatteryStaple::Generator

Inherits:
Object
  • Object
show all
Includes:
Common, Memoize
Defined in:
lib/correct_horse_battery_staple/generator.rb

Overview

Generate an N-word passphrase from a corpus

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memoize

included

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

#corpusObject

Returns the value of attribute corpus.



11
12
13
# File 'lib/correct_horse_battery_staple/generator.rb', line 11

def corpus
  @corpus
end

#word_lengthObject

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(options)
  candidate_count = @corpus.count_candidates(options)
  (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, options = {})
  @corpus.pick(count, options).
    map {|entry| entry.word.downcase }.
    join("-")
end

#wordsObject



32
33
34
# File 'lib/correct_horse_battery_staple/generator.rb', line 32

def words
  @words ||= @corpus.result
end