class RandomWords::Generator
def generate_sentence(length = nil)
-
(String)
- A randomly generated sentence
Parameters:
-
length
(Integer
) -- The desired length of the sentence
def generate_sentence(length = nil) length ||= define_length(@sentence_length) sentence_components = [] # Randomly decide if we include a plural noun with a number sentence_components << random_number_with_plural if roll(20) # 20% chance to include a plural noun # Construct main clause sentence_components << generate_main_clause # Include any additional clauses # sentence_components.concat(generate_additional_clauses) # while sentence_components.join(' ').strip.length < length # # Randomly include a subordinate conjunction # additional_clauses = generate_additional_clauses # sentence_components.concat(additional_clauses) # sentence_components.map!(&:strip) # break if sentence_components.join(' ').length >= length # conjunction = random_subordinate_conjunction.strip # sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start # end # Join all parts into a single sentence sentence_components.join(' ').strip.compress end