Class: Simplelorem::Generator
- Inherits:
-
Object
- Object
- Simplelorem::Generator
- Defined in:
- lib/simplelorem.rb
Instance Method Summary collapse
- #generate(count) ⇒ Object
- #generate_sentence ⇒ Object
- #get_unique_word ⇒ Object
-
#initialize(text) ⇒ Generator
constructor
A new instance of Generator.
- #sanitize(text) ⇒ Object
Constructor Details
#initialize(text) ⇒ Generator
Returns a new instance of Generator.
5 6 7 8 9 |
# File 'lib/simplelorem.rb', line 5 def initialize(text) @text = self.sanitize text @last_word = '' @avail_punct = ['.', '.', '.', '.', '.', '.', '.', '.', '!', '?'] end |
Instance Method Details
#generate(count) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/simplelorem.rb', line 46 def generate(count) sentences = [] while count > 0 sentences.push self.generate_sentence count -= 1 end sentences.join ' ' end |
#generate_sentence ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/simplelorem.rb', line 25 def generate_sentence() sentence = "" length = rand(8..25) while (sentence.split.size <= length) do word = self.get_unique_word word += [',', ',', ',', ';', ' --'].sample if rand < 0.005 word += ' ' sentence += word end sentence[0] = sentence[0].capitalize sentence = sentence.chomp(' ') sentence += @avail_punct.sample puts sentence puts length puts return sentence end |
#get_unique_word ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/simplelorem.rb', line 15 def get_unique_word() word = @text.sample while word == @last_word word = @text.sample end @last_word = word end |
#sanitize(text) ⇒ Object
11 12 13 |
# File 'lib/simplelorem.rb', line 11 def sanitize(text) text.gsub(/\n|\t/,'').split(',').map { |i| i.gsub(/\.|\?|\!/, '').split.join(' ') } end |