class Simplelorem::Generator

Public Class Methods

new(text) click to toggle source
# File lib/simplelorem.rb, line 5
def initialize(text)
  @text = self.sanitize text
  @last_word = ''
  @avail_punct = ['.', '.', '.', '.', '.', '.', '.', '.', '!', '?']
end

Public Instance Methods

generate(count) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/simplelorem.rb, line 11
def sanitize(text)
  text.gsub(/\n|\t/,'').split(',').map { |i| i.gsub(/\.|\?|\!/, '').split.join(' ') }
end