Class: Simplelorem::Generator

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

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Generator

Returns a new instance of Generator.



5
6
7
# File 'lib/simplelorem.rb', line 5

def initialize(text)
  @text = self.sanitize text
end

Instance Method Details

#generate(count) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/simplelorem.rb', line 30

def generate(count)
  sentences = []

  while count > 0
    sentences.push self.generate_sentence
    count -= 1
  end

  sentences.join ' '
end

#generate_sentenceObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simplelorem.rb', line 13

def generate_sentence()
  sentence = []
  sentence_length = rand 6..22

  while sentence_length > 0
    word = @text.sample
    word += [',', ';', ' --'].sample if rand < 0.1 && sentence_length > 2
    sentence.push word
    sentence_length -= 1
  end

  sentence[0] = sentence[0].capitalize
  sentence[sentence.length-1] = sentence[sentence.length-1] += ['.', '!', '?'].sample

  sentence.join ' '
end

#sanitize(text) ⇒ Object



9
10
11
# File 'lib/simplelorem.rb', line 9

def sanitize(text)
  text.split(',').map { |i| i.gsub(/\.|\?|\!/, '').split.join(' ') }
end