class ChainPunk::Generator

Public Class Methods

new(frequency_table) click to toggle source
# File lib/chain_punk/generator.rb, line 5
def initialize(frequency_table)
  @frequency_table = frequency_table
end

Public Instance Methods

generate(grapheme_count, options = {}) click to toggle source
# File lib/chain_punk/generator.rb, line 9
def generate(grapheme_count, options = {})
  boundary = options[:boundary] || ''
  index_size = options[:index_size] || 1
  seeds = options[:seeds]
  create_phrase(grapheme_count, seeds, index_size, boundary, options[:closure])
end

Private Instance Methods

create_phrase(grapheme_count, seeds, index_size, boundary, closure) click to toggle source
# File lib/chain_punk/generator.rb, line 18
def create_phrase(grapheme_count, seeds, index_size, boundary, closure)
  graphemes = starting_grapheme(seeds)
  phrase = []

  (1..grapheme_count / index_size).each do
    phrase << graphemes
    break if @frequency_table[graphemes].nil?

    @frequency_table[graphemes]
    graphemes = @frequency_table[graphemes].sample
  end

  "#{phrase.flatten.join(boundary)}#{closure}"
end
starting_grapheme(seeds = nil) click to toggle source
# File lib/chain_punk/generator.rb, line 33
def starting_grapheme(seeds = nil)
  return @frequency_table.keys.sample if seeds.nil?

  seeds.sample
end