class Typify

Constants

CONSONANTS
SAMPLE_TEXT
VOWELS

Attributes

mapping[RW]

Public Class Methods

new(words = SAMPLE_TEXT) click to toggle source
# File lib/typify.rb, line 13
def initialize(words = SAMPLE_TEXT)
  @mapping = sample_words words
end

Public Instance Methods

make(length = rand(7..50)) click to toggle source
# File lib/typify.rb, line 53
def make(length = rand(7..50))
  words = Array.new
  length.times { words << random }
  words
end
random(length = @mapping[:lengths].sample) click to toggle source
# File lib/typify.rb, line 40
def random(length = @mapping[:lengths].sample)
  word = String.new
  word << @mapping[:starts][length].split('').sample
  length.times do |l|
    begin
      word << @mapping[:chars][word[l]].split('').sample
    rescue
      # there was no mapping here
    end
  end
  word
end
sample_words(words = SAMPLE_TEXT) click to toggle source
# File lib/typify.rb, line 19
def sample_words(words = SAMPLE_TEXT)
  @mapping           ||= Hash.new
  @mapping[:chars]   ||= Hash.new
  @mapping[:starts]  ||= Hash.new
  @mapping[:lengths] ||= Array.new
  ("a".."z").each { |l| mapping[:chars][l] = "" }
  words = words.downcase.split(/[^a-z]+/)
  
  words.each do |w|
    next if w.length == 0
    @mapping[:lengths] << w.length
    @mapping[:starts][w.length] ||= ""
    @mapping[:starts][w.length] << w[0].to_s if w
    w.length.times do |i|
      @mapping[:chars][w[i]] << w[i+1] if w[i+1]
    end
  end
  
  return @mapping
end