Class: RandomWords::Generator
- Inherits:
-
Object
- Object
- RandomWords::Generator
- Defined in:
- lib/random-words/generator.rb
Overview
Random character, word, and sentence generator
Constant Summary collapse
- SENTENCE_PARTS =
Define the default sentence parts These parts will be used to generate random sentences and character strings
%w[random_article random_adjective random_noun random_adverb random_verb random_adjective random_verb random_adverb random_phrase].freeze
- TEMPLATES =
[ # Simple declarative i[random_article random_adjective random_noun random_verb], i[random_plural_article random_adjective random_plural_noun random_plural_verb], # With adverb i[random_article random_adjective random_noun random_adverb random_verb], i[random_name random_adverb random_verb], # Passive voice i[random_article random_noun random_passive_verb random_preposition random_article random_noun], # With prepositional phrase i[random_article random_adjective random_noun random_verb random_preposition random_article random_noun], # Compound sentence i[random_article random_noun random_verb random_coordinating_conjunction random_article random_noun random_verb], # Subordinate clause i[random_clause random_article random_noun random_verb], # With phrase i[random_phrase], # Name as subject i[random_name random_verb random_article random_noun], # Plural with adverb and preposition i[random_plural_article random_plural_noun random_adverb random_plural_verb random_preposition random_article random_noun], # Coordinating conjunction joining two clauses i[random_article random_noun random_verb random_coordinating_conjunction random_plural_article random_plural_noun random_plural_verb random_phrase], # Subordinate conjunction i[random_subordinate_conjunction random_article random_noun random_verb random_article random_noun random_verb], # With random_adjective and random_adverb in various places i[random_adverb random_article random_adjective random_noun random_verb], i[random_article random_noun random_verb random_adverb], # Name with phrase i[random_name random_verb random_phrase], # Plural passive i[random_plural_article random_plural_noun random_passive_verb random_preposition random_article random_noun], # Name with subordinate clause i[random_name random_verb random_subordinate_conjunction random_article random_noun random_verb] ].freeze
- OPTIONAL_PARTS =
i[adjectives adverbs].freeze
Instance Attribute Summary collapse
-
#adjectives ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#adverbs ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#all_words ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#articles ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#clauses ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#config ⇒ Object
readonly
return [Hash] configuration.
-
#coordinating_conjunctions ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#debug ⇒ Boolean
Debug mode.
-
#extended_punctuation ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#names ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#nouns ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#numbers ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#paragraph_length ⇒ Integer
Number of sentences in paragraphs.
-
#passive_verbs ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#phrases ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#plural_articles ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#plural_nouns ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#plural_verbs ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#prepositions ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#sentence_length ⇒ Symbol
Sentence length (:short, :medium, :long, :very_long).
-
#source ⇒ Symbol
Dictionary in use.
-
#sources ⇒ Hash<String, RandomWords::Source>
readonly
List of available sources.
-
#subordinate_conjunctions ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#terminators ⇒ Array<String>
readonly
Arrays of elements of speech.
-
#use_extended_punctuation ⇒ Boolean
Whether to use extended punctuation.
-
#verbs ⇒ Array<String>
readonly
Arrays of elements of speech.
Instance Method Summary collapse
-
#characters(min, max = nil, whole_words: true, whitespace: true, article: true, dead_switch: 0) ⇒ String
Generate a series of random words up to a specified length.
-
#code_lang ⇒ Symbol
Generate a random code language.
-
#code_snippet(lang = nil) ⇒ String
Return random code snippet.
-
#create_dictionary(title) ⇒ Symbol
Shortcut for RandomWords::Config.create_user_dictionary.
-
#dbg(msg) ⇒ Object
Display debug message.
-
#generate_combined_sentence(length = nil) ⇒ String
Generate a random sentence, combining multiple sentences if necessary This method generates a random sentence and checks its length.
-
#html(settings = {}) ⇒ String
Generate random HTML.
-
#initialize(source = :english, options = {}) ⇒ Generator
constructor
Initialize the generator with a source and options.
-
#lengths ⇒ Object
Refactored lengths and lengths= methods This method returns the lengths of sentences The default lengths are set to the following values: short: 20, medium: 60, long: 100, very_long: 300.
-
#lengths=(new_lengths) ⇒ Hash
This method allows you to set new lengths for the sentences It merges the new lengths with the existing ones Example: lengths = { short: 50, medium: 150 }.
-
#markdown(settings = {}) ⇒ String
Generate random markdown.
-
#name ⇒ String
Generate a random name.
-
#paragraph(length = @paragraph_length) ⇒ String
Generate a random paragraph This method generates a random paragraph by combining multiple sentences.
-
#sentence(length = nil) ⇒ String
Generate a random sentence.
-
#sentences(number) ⇒ Array
Generate a specified number of random sentences.
-
#word ⇒ String
Generate a random word.
-
#words(number) ⇒ Object
Generate a set number of random words.
Constructor Details
#initialize(source = :english, options = {}) ⇒ Generator
Initialize the generator with a source and options
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/random-words/generator.rb', line 104 def initialize(source = :english, = {}) @debug = [:debug] || false @tested = [] @config = RandomWords::Config.new(source) @source = source @nouns = @config.dictionary[:nouns] @plural_nouns = @config.dictionary[:plural_nouns] @verbs = @config.dictionary[:verbs] @plural_verbs = @config.dictionary[:plural_verbs] @passive_verbs = @config.dictionary[:passive_verbs] @adverbs = @config.dictionary[:adverbs] @adjectives = @config.dictionary[:adjectives] @articles = @config.dictionary[:articles] @plural_articles = @config.dictionary[:plural_articles] @prepositions = @config.dictionary[:prepositions] @clauses = @config.dictionary[:clauses] @coordinating_conjunctions = @config.dictionary[:coordinating_conjunctions] @subordinate_conjunctions = @config.dictionary[:subordinate_conjunctions] @numbers = @config.dictionary[:numbers] @sources = @config.sources @terminators = @config.dictionary[:terminators] @names = [@config.dictionary[:first_names], @config.dictionary[:last_names], @config.dictionary[:full_names]] @phrases = @config.dictionary[:phrases] @all_words = @config.dictionary[:all_words] = { sentence_length: :medium, paragraph_length: 5, use_extended_punctuation: false } .merge!() if .is_a?(Hash) @sentence_length = [:sentence_length] @paragraph_length = [:paragraph_length] @use_extended_punctuation = [:use_extended_punctuation] @terminators.concat(@config.dictionary[:extended_punctuation]) if @use_extended_punctuation lengths end |
Instance Attribute Details
#adjectives ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def adjectives @adjectives end |
#adverbs ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def adverbs @adverbs end |
#all_words ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def all_words @all_words end |
#articles ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def articles @articles end |
#clauses ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def clauses @clauses end |
#config ⇒ Object (readonly)
return [Hash] configuration
16 17 18 |
# File 'lib/random-words/generator.rb', line 16 def config @config end |
#coordinating_conjunctions ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def coordinating_conjunctions @coordinating_conjunctions end |
#debug ⇒ Boolean
Debug mode
36 37 38 |
# File 'lib/random-words/generator.rb', line 36 def debug @debug end |
#extended_punctuation ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def extended_punctuation @extended_punctuation end |
#names ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def names @names end |
#nouns ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def nouns @nouns end |
#numbers ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def numbers @numbers end |
#paragraph_length ⇒ Integer
Returns Number of sentences in paragraphs.
23 24 25 |
# File 'lib/random-words/generator.rb', line 23 def paragraph_length @paragraph_length end |
#passive_verbs ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def passive_verbs @passive_verbs end |
#phrases ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def phrases @phrases end |
#plural_articles ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def plural_articles @plural_articles end |
#plural_nouns ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def plural_nouns @plural_nouns end |
#plural_verbs ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def plural_verbs @plural_verbs end |
#prepositions ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def prepositions @prepositions end |
#sentence_length ⇒ Symbol
Returns Sentence length (:short, :medium, :long, :very_long).
26 27 28 |
# File 'lib/random-words/generator.rb', line 26 def sentence_length @sentence_length end |
#source ⇒ Symbol
Returns Dictionary in use.
29 30 31 |
# File 'lib/random-words/generator.rb', line 29 def source @source end |
#sources ⇒ Hash<String, RandomWords::Source> (readonly)
Returns List of available sources.
32 33 34 |
# File 'lib/random-words/generator.rb', line 32 def sources @sources end |
#subordinate_conjunctions ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def subordinate_conjunctions @subordinate_conjunctions end |
#terminators ⇒ Array<String> (readonly)
Returns arrays of elements of speech.
11 12 13 |
# File 'lib/random-words/generator.rb', line 11 def terminators @terminators end |
#use_extended_punctuation ⇒ Boolean
Whether to use extended punctuation
20 21 22 |
# File 'lib/random-words/generator.rb', line 20 def use_extended_punctuation @use_extended_punctuation end |
Instance Method Details
#characters(min, max = nil, whole_words: true, whitespace: true, article: true, dead_switch: 0) ⇒ String
Generate a series of random words up to a specified length
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/random-words/generator.rb', line 309 def characters(min, max = nil, whole_words: true, whitespace: true, article: true, dead_switch: 0) result = '' max ||= min raise ArgumentError, 'Infinite loop detected' if dead_switch > 40 whole_words = false if dead_switch > 15 space = whitespace ? ' ' : '' current_part = article ? 0 : 1 while result.length < max && result.length < min word = send(SENTENCE_PARTS[current_part].to_sym) word = word.gsub(/ +/, '') unless whitespace current_part = (current_part + 1) % SENTENCE_PARTS.length new_result = "#{result}#{space}#{word}".compress if new_result.length > max return handle_overflow(OverflowConfig.new(new_result, result, min, max, whole_words, whitespace, dead_switch)) end return new_result if new_result.length == max result = new_result end result.strip end |
#code_lang ⇒ Symbol
Generate a random code language
392 393 394 395 |
# File 'lib/random-words/generator.rb', line 392 def code_lang code_langs = i[python ruby swift javascript css rust go java] code_langs[Random.rand(code_langs.count)] end |
#code_snippet(lang = nil) ⇒ String
Return random code snippet
400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/random-words/generator.rb', line 400 def code_snippet(lang = nil) code_snippets = { python: %(def hello_world():\n print("Hello, World!")), ruby: %(def hello_world\n puts "Hello, World!"\nend), swift: %(func helloWorld() {\n print("Hello, World!")\n}), javascript: %(function helloWorld() {\n console.log("Hello, World!");\n}), css: %(body {\n background-color: #f0f0f0;\n font-family: Arial, sans-serif;\n}\nh1 {\n color: #333;\n}), rust: %(fn main() {\n println!("Hello, World!");\n}), go: %(package main\nimport "fmt"\nfunc main() {\n fmt.Println("Hello, World!")\n}), java: %(public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n}) } lang ||= code_lang code_snippets[lang.to_sym] end |
#create_dictionary(title) ⇒ Symbol
Shortcut for RandomWords::Config.create_user_dictionary
158 159 160 |
# File 'lib/random-words/generator.rb', line 158 def create_dictionary(title) @config.create_user_dictionary(title) end |
#dbg(msg) ⇒ Object
Display debug message
147 148 149 150 151 |
# File 'lib/random-words/generator.rb', line 147 def dbg(msg) return unless @debug "%#{msg}%" end |
#generate_combined_sentence(length = nil) ⇒ String
Generate a random sentence, combining multiple sentences if necessary This method generates a random sentence and checks its length. If the length is less than the defined length, it combines it with another sentence. The final sentence is returned with proper capitalization and termination.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/random-words/generator.rb', line 360 def generate_combined_sentence(length = nil) length ||= define_length(@sentence_length) sentence = generate_sentence return sentence.to_sent(random_terminator).fix_caps(terminators). if sentence.length > length while sentence.length < length # Generate a random number of sentences to combine new_sentence = generate_sentence(length / 2) # Combine the sentences with random conjunctions sentence = "#{sentence.strip.no_term(terminators)}, #{random_coordinating_conjunction} #{new_sentence.no_term(terminators)}" end sentence.to_sent(random_terminator).fix_caps(terminators). end |
#html(settings = {}) ⇒ String
Generate random HTML
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/random-words/generator.rb', line 433 def html(settings = {}) html = RandomWords::LoremHTML.new(settings) if settings[:complete] style = settings[:style] || 'style.css' " <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n \\t<meta charset=\"UTF-8\">\n \\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n \\t<title>\#{html.title}</title>\n \\t<link rel=\"stylesheet\" href=\"\#{style}\">\n </head>\n <body>\n \#{html.output.indent(\"\\t\")}\n </body>\n </html>\n EOOUTPUT\n else\n html.output\n end\nend\n" |
#lengths ⇒ Object
Refactored lengths and lengths= methods This method returns the lengths of sentences The default lengths are set to the following values: short: 20, medium: 60, long: 100, very_long: 300
269 270 271 |
# File 'lib/random-words/generator.rb', line 269 def lengths @lengths ||= { short: 20, medium: 60, long: 100, very_long: 300 } end |
#lengths=(new_lengths) ⇒ Hash
This method allows you to set new lengths for the sentences It merges the new lengths with the existing ones Example: lengths = { short: 50, medium: 150 }
280 281 282 |
# File 'lib/random-words/generator.rb', line 280 def lengths=(new_lengths) @lengths = lengths.merge(new_lengths) end |
#markdown(settings = {}) ⇒ String
Generate random markdown
418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/random-words/generator.rb', line 418 def markdown(settings = {}) input = RandomWords::LoremHTML.new(settings) = {} if settings[:meta_type] [:type] = settings[:meta_type] [:title] = input.title if input.title [:style] = settings[:style] || 'style.css' [:date] = Time.now.strftime('%Y-%m-%d %H:%M:%S') end RandomWords::HTML2Markdown.new(input, nil, ).to_s end |
#name ⇒ String
Generate a random name
458 459 460 |
# File 'lib/random-words/generator.rb', line 458 def name "#{dbg('NAM')}#{random_name}" end |
#paragraph(length = @paragraph_length) ⇒ String
Generate a random paragraph This method generates a random paragraph by combining multiple sentences. It uses the generate_combined_sentence method to create each sentence.
382 383 384 385 386 387 388 |
# File 'lib/random-words/generator.rb', line 382 def paragraph(length = @paragraph_length) sentences = [] length.times do sentences << generate_combined_sentence end sentences.join(' ').strip.compress end |
#sentence(length = nil) ⇒ String
Generate a random sentence
339 340 341 |
# File 'lib/random-words/generator.rb', line 339 def sentence(length = nil) generate_combined_sentence(length) end |
#sentences(number) ⇒ Array
Generate a specified number of random sentences
348 349 350 |
# File 'lib/random-words/generator.rb', line 348 def sentences(number) Array.new(number) { generate_combined_sentence } end |
#word ⇒ String
Generate a random word
286 287 288 |
# File 'lib/random-words/generator.rb', line 286 def word generate_word end |
#words(number) ⇒ Object
Generate a set number of random words
292 293 294 295 296 297 |
# File 'lib/random-words/generator.rb', line 292 def words(number) result = SENTENCE_PARTS.cycle.take(number).map { |part| send(part.to_sym) }.take(number) result.map do |word| word.split(/ /).last end.join(' ').article_agree.compress end |