Class: Typify

Inherits:
Object
  • Object
show all
Defined in:
lib/typify.rb

Constant Summary collapse

VOWELS =
"aeiou"
CONSONANTS =
("a".."z").reject {|l| VOWELS.include? l }.join
SAMPLE_TEXT =
"  Randomness means lack of pattern or predictability in events.[1] Randomness suggests a non-order or non-coherence in a sequence of symbols or steps, such that there is no intelligible pattern or combination.\n  Random events are individually unpredictable, but the frequency of different outcomes over a large number of events (or \"trials\") are frequently predictable. For example, when throwing two dice and counting the total, a sum of 7 will randomly occur twice as often as 4, but the outcome of any particular roll of the dice is unpredictable. This view, where randomness simply refers to situations where the certainty of the outcome is at issue, applies to concepts of chance, probability, and information entropy. In these situations, randomness implies a measure of uncertainty, and notions of haphazardness are irrelevant.\n  The fields of mathematics, probability, and statistics use formal definitions of randomness. In statistics, a random variable is an assignment of a numerical value to each possible outcome of an event space. This association facilitates the identification and the calculation of probabilities of the events. A random process is a sequence of random variables describing a process whose outcomes do not follow a deterministic pattern, but follow an evolution described by probability distributions. These and other constructs are extremely useful in probability theory.\n  Randomness is often used in statistics to signify well-defined statistical properties. Monte Carlo methods, which rely on random input, are important techniques in science, as, for instance, in computational science.[2]\n  Random selection is a method of selecting items (often called units) from a population where the probability of choosing a specific item is the proportion of those items in the population. For example, if we have a bowl of 100 marbles with 10 red (and any red marble is indistinguishable from any other red marble) and 90 blue (and any blue marble is indistinguishable from any other blue marble), a random selection mechanism would choose a red marble with probability 1/10. Note that a random selection mechanism that selected 10 marbles from this bowl would not necessarily result in 1 red and 9 blue. In situations where a population consists of items that are distinguishable, a random selection mechanism requires equal probabilities for any item to be chosen. That is, if the selection process is such that each member of a population, of say research subjects, has the same probability of being chosen then we can say the selection process is random.\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words = SAMPLE_TEXT) ⇒ Typify

Returns a new instance of Typify.



13
14
15
# File 'lib/typify.rb', line 13

def initialize(words = SAMPLE_TEXT)
  @mapping = sample_words words
end

Instance Attribute Details

#mappingObject

Returns the value of attribute mapping.



17
18
19
# File 'lib/typify.rb', line 17

def mapping
  @mapping
end

Instance Method Details

#make(length = rand(7..50)) ⇒ Object



53
54
55
56
57
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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