Class: Crazipsum::Generator

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

Overview

Generator generates lorem ipsum sentences, paragraphs and text based on a Dictionnary.

Constant Summary collapse

DEFAULT_WORD_COUNT_RANGE =
(7..15).freeze
DEFAULT_SENTENCE_COUNT_RANGE =
(4..7).freeze
DEFAULT_PARAGRAPH_COUNT_RANGE =
(3..5).freeze
DEFAULT_PARAGRAPH_SEPARATOR =
"\n\n"

Instance Method Summary collapse

Constructor Details

#initialize(dictionnary) ⇒ Generator

Returns a new instance of Generator that will used words and fillers from the given Dictionnary to generate the lorem ipsum texts.



14
15
16
# File 'lib/crazipsum/generator.rb', line 14

def initialize(dictionnary)
  @dictionnary = dictionnary
end

Instance Method Details

#paragraph(word_count: rand(DEFAULT_WORD_COUNT_RANGE), sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE), fillers: dictionnary.fillers) ⇒ String Also known as: sentences

Generates a lorem ipsum paragraph based on the generator’s dictionnary.

Parameters:

  • sentence_count (integer) (defaults to: rand(DEFAULT_SENTENCE_COUNT_RANGE))

    the number of sentences expected in the paragraph.

  • word_count (integer) (defaults to: rand(DEFAULT_WORD_COUNT_RANGE))

    the number of words expected in each sentence.

  • fillers (Array<String>) (defaults to: dictionnary.fillers)

    a list of words used to fill in the lorem ipsum.

Returns:

  • (String)

    a lorem ipsum paragraph.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/crazipsum/generator.rb', line 44

def paragraph(
  word_count: rand(DEFAULT_WORD_COUNT_RANGE),
  sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE),
  fillers: dictionnary.fillers
)
  sentence_count = 0 if sentence_count.negative?
  paragraph = []
  sentence_count.times do
    s = sentence(word_count: word_count, fillers: fillers)
    paragraph << s unless s.empty?
  end
  paragraph.join(' ')
end

#paragraphs(word_count: rand(DEFAULT_WORD_COUNT_RANGE), sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE), paragraph_count: rand(DEFAULT_PARAGRAPH_COUNT_RANGE), fillers: dictionnary.fillers, seperator: DEFAULT_PARAGRAPH_SEPARATOR) ⇒ String Also known as: text

Generates a lorem ipsum text with multiples paragraphs based on the generator’s dictionnary.

Parameters:

  • sentence_count (integer) (defaults to: rand(DEFAULT_SENTENCE_COUNT_RANGE))

    the number of sentences expected in the paragraph.

  • sentence_count (integer) (defaults to: rand(DEFAULT_SENTENCE_COUNT_RANGE))

    the number of sentences expected in the paragraphs.

  • word_count (integer) (defaults to: rand(DEFAULT_WORD_COUNT_RANGE))

    the number of words expected in each sentence.

  • fillers (Array<String>) (defaults to: dictionnary.fillers)

    a list of words used to fill in the lorem ipsum.

Returns:

  • (String)

    a lorem ipsum text.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/crazipsum/generator.rb', line 67

def paragraphs(
  word_count: rand(DEFAULT_WORD_COUNT_RANGE),
  sentence_count: rand(DEFAULT_SENTENCE_COUNT_RANGE),
  paragraph_count: rand(DEFAULT_PARAGRAPH_COUNT_RANGE),
  fillers: dictionnary.fillers,
  seperator: DEFAULT_PARAGRAPH_SEPARATOR
)
  paragraph_count = 0 if paragraph_count.negative?
  paragraphs = []
  paragraph_count.times do
    p = paragraph(word_count: word_count, sentence_count: sentence_count, fillers: fillers)
    paragraphs << p unless p.empty?
  end
  paragraphs.join(seperator)
end

#sentence(word_count: rand(DEFAULT_WORD_COUNT_RANGE), fillers: dictionnary.fillers) ⇒ String

Generates a lorem ipsum sentence based on the generator’s dictionnary.

Parameters:

  • word_count (integer) (defaults to: rand(DEFAULT_WORD_COUNT_RANGE))

    the number of words expected in the sentence.

  • fillers (Array<String>) (defaults to: dictionnary.fillers)

    a list of words used to fill in the lorem ipsum.

Returns:

  • (String)

    a lorem ipsum sentence.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/crazipsum/generator.rb', line 23

def sentence(word_count: rand(DEFAULT_WORD_COUNT_RANGE), fillers: dictionnary.fillers)
  word_count = 0 if word_count.negative?
  dictionnary_words = dictionnary.words
  words = (0...word_count).map do
    next dictionnary_words.sample if fillers.nil? || fillers == false || fillers.empty?

    rand(3).zero? ? dictionnary_words.sample : fillers.sample
  end
  return '' if words.empty?

  words[0] = words[0].capitalize
  words = words.join(' ')
  "#{words}."
end