random-words 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +148 -39
  3. data/.rubocop.yml +1 -0
  4. data/CHANGELOG.md +42 -0
  5. data/Gemfile.lock +4 -1
  6. data/README.md +153 -4
  7. data/bin/randw +195 -49
  8. data/lib/random-words/array.rb +51 -0
  9. data/lib/random-words/config.rb +248 -0
  10. data/lib/random-words/generator.rb +239 -93
  11. data/lib/random-words/hash.rb +16 -0
  12. data/lib/random-words/html2markdown.rb +205 -0
  13. data/lib/random-words/lorem-markdown.rb +398 -0
  14. data/lib/random-words/number-to-word.rb +137 -0
  15. data/lib/random-words/source.rb +105 -0
  16. data/lib/random-words/string.rb +221 -47
  17. data/lib/random-words/table-cleanup.rb +210 -0
  18. data/lib/random-words/version.rb +1 -1
  19. data/lib/random-words/words/1984/adjectives.txt +103 -0
  20. data/lib/random-words/words/1984/adverbs.txt +92 -0
  21. data/lib/random-words/words/1984/articles-plural.txt +10 -0
  22. data/lib/random-words/words/1984/articles-singular.txt +10 -0
  23. data/lib/random-words/words/1984/clauses.txt +79 -0
  24. data/lib/random-words/words/1984/config.yml +4 -0
  25. data/lib/random-words/words/1984/conjunctions-coordinating.txt +20 -0
  26. data/lib/random-words/words/1984/conjunctions-subordinate.txt +29 -0
  27. data/lib/random-words/words/1984/names.txt +74 -0
  28. data/lib/random-words/words/1984/nouns-plural.txt +89 -0
  29. data/lib/random-words/words/1984/nouns-singular.txt +74 -0
  30. data/lib/random-words/words/1984/numbers.yml +5 -0
  31. data/lib/random-words/words/1984/prepositions.txt +89 -0
  32. data/lib/random-words/words/1984/terminators.txt +16 -0
  33. data/lib/random-words/words/1984/verbs-passive.txt +83 -0
  34. data/lib/random-words/words/1984/verbs-plural.txt +91 -0
  35. data/lib/random-words/words/1984/verbs-singular.txt +110 -0
  36. data/lib/random-words/words/alice/adjectives.txt +80 -0
  37. data/lib/random-words/words/alice/adverbs.txt +87 -0
  38. data/lib/random-words/words/alice/articles-plural.txt +10 -0
  39. data/lib/random-words/words/alice/articles-singular.txt +10 -0
  40. data/lib/random-words/words/alice/clauses.txt +81 -0
  41. data/lib/random-words/words/alice/config.yml +4 -0
  42. data/lib/random-words/words/alice/conjunctions-coordinating.txt +20 -0
  43. data/lib/random-words/words/alice/conjunctions-subordinate.txt +29 -0
  44. data/lib/random-words/words/alice/names.txt +74 -0
  45. data/lib/random-words/words/alice/nouns-plural.txt +95 -0
  46. data/lib/random-words/words/alice/nouns-singular.txt +82 -0
  47. data/lib/random-words/words/alice/numbers.yml +5 -0
  48. data/lib/random-words/words/alice/prepositions.txt +45 -0
  49. data/lib/random-words/words/alice/terminators.txt +16 -0
  50. data/lib/random-words/words/alice/verbs-passive.txt +495 -0
  51. data/lib/random-words/words/alice/verbs-plural.txt +115 -0
  52. data/lib/random-words/words/alice/verbs-singular.txt +97 -0
  53. data/lib/random-words/words/bacon/clauses.txt +592 -592
  54. data/lib/random-words/words/bacon/config.yml +4 -0
  55. data/lib/random-words/words/bacon/conjunctions-coordinating.txt +20 -0
  56. data/lib/random-words/words/bacon/names.txt +74 -0
  57. data/lib/random-words/words/bacon/numbers.yml +5 -0
  58. data/lib/random-words/words/bacon/prepositions.txt +45 -0
  59. data/lib/random-words/words/bacon/terminators.txt +16 -0
  60. data/lib/random-words/words/corporate/config.yml +4 -0
  61. data/lib/random-words/words/corporate/conjunctions-coordinating.txt +20 -0
  62. data/lib/random-words/words/corporate/names.txt +74 -0
  63. data/lib/random-words/words/corporate/numbers.yml +5 -0
  64. data/lib/random-words/words/corporate/prepositions.txt +45 -0
  65. data/lib/random-words/words/corporate/terminators.txt +16 -0
  66. data/lib/random-words/words/doctor/adjectives.txt +92 -0
  67. data/lib/random-words/words/doctor/adverbs.txt +92 -0
  68. data/lib/random-words/words/doctor/articles-plural.txt +10 -0
  69. data/lib/random-words/words/doctor/articles-singular.txt +10 -0
  70. data/lib/random-words/words/doctor/clauses.txt +83 -0
  71. data/lib/random-words/words/doctor/config.yml +10 -0
  72. data/lib/random-words/words/doctor/conjunctions-coordinating.txt +20 -0
  73. data/lib/random-words/words/doctor/conjunctions-subordinate.txt +29 -0
  74. data/lib/random-words/words/doctor/names.txt +74 -0
  75. data/lib/random-words/words/doctor/nouns-plural.txt +84 -0
  76. data/lib/random-words/words/doctor/nouns-singular.txt +84 -0
  77. data/lib/random-words/words/doctor/numbers.yml +5 -0
  78. data/lib/random-words/words/doctor/prepositions.txt +45 -0
  79. data/lib/random-words/words/doctor/terminators.txt +16 -0
  80. data/lib/random-words/words/doctor/verbs-passive.txt +83 -0
  81. data/lib/random-words/words/doctor/verbs-plural.txt +88 -0
  82. data/lib/random-words/words/doctor/verbs-singular.txt +83 -0
  83. data/lib/random-words/words/english/clauses.txt +592 -592
  84. data/lib/random-words/words/english/config.yml +4 -0
  85. data/lib/random-words/words/english/conjunctions-coordinating.txt +20 -0
  86. data/lib/random-words/words/english/names.txt +74 -0
  87. data/lib/random-words/words/english/numbers.yml +5 -0
  88. data/lib/random-words/words/english/prepositions.txt +45 -0
  89. data/lib/random-words/words/english/terminators.txt +16 -0
  90. data/lib/random-words/words/english/verbs-plural.txt +1 -0
  91. data/lib/random-words/words/english/verbs-singular.txt +1 -0
  92. data/lib/random-words/words/foulmouth/adjectives.txt +89 -0
  93. data/lib/random-words/words/foulmouth/adverbs.txt +97 -0
  94. data/lib/random-words/words/foulmouth/articles-plural.txt +10 -0
  95. data/lib/random-words/words/foulmouth/articles-singular.txt +10 -0
  96. data/lib/random-words/words/foulmouth/clauses.txt +74 -0
  97. data/lib/random-words/words/foulmouth/config.yml +4 -0
  98. data/lib/random-words/words/foulmouth/conjunctions-coordinating.txt +20 -0
  99. data/lib/random-words/words/foulmouth/conjunctions-subordinate.txt +29 -0
  100. data/lib/random-words/words/foulmouth/names.txt +74 -0
  101. data/lib/random-words/words/foulmouth/nouns-plural.txt +96 -0
  102. data/lib/random-words/words/foulmouth/nouns-singular.txt +99 -0
  103. data/lib/random-words/words/foulmouth/numbers.yml +5 -0
  104. data/lib/random-words/words/foulmouth/prepositions.txt +35 -0
  105. data/lib/random-words/words/foulmouth/terminators.txt +16 -0
  106. data/lib/random-words/words/foulmouth/verbs-passive.txt +143 -0
  107. data/lib/random-words/words/foulmouth/verbs-plural.txt +91 -0
  108. data/lib/random-words/words/foulmouth/verbs-singular.txt +104 -0
  109. data/lib/random-words/words/hipster/adjectives.txt +100 -0
  110. data/lib/random-words/words/hipster/adverbs.txt +89 -0
  111. data/lib/random-words/words/hipster/articles-plural.txt +10 -0
  112. data/lib/random-words/words/hipster/articles-singular.txt +10 -0
  113. data/lib/random-words/words/hipster/clauses.txt +180 -0
  114. data/lib/random-words/words/hipster/config.yml +4 -0
  115. data/lib/random-words/words/hipster/conjunctions-coordinating.txt +20 -0
  116. data/lib/random-words/words/hipster/conjunctions-subordinate.txt +29 -0
  117. data/lib/random-words/words/hipster/names.txt +74 -0
  118. data/lib/random-words/words/hipster/nouns-plural.txt +96 -0
  119. data/lib/random-words/words/hipster/nouns-singular.txt +100 -0
  120. data/lib/random-words/words/hipster/numbers.yml +5 -0
  121. data/lib/random-words/words/hipster/prepositions.txt +45 -0
  122. data/lib/random-words/words/hipster/terminators.txt +16 -0
  123. data/lib/random-words/words/hipster/verbs-passive.txt +88 -0
  124. data/lib/random-words/words/hipster/verbs-plural.txt +106 -0
  125. data/lib/random-words/words/hipster/verbs-singular.txt +114 -0
  126. data/lib/random-words/words/latin/config.yml +4 -0
  127. data/lib/random-words/words/latin/conjunctions-coordinating.txt +15 -0
  128. data/lib/random-words/words/latin/names.txt +74 -0
  129. data/lib/random-words/words/latin/numbers.yml +5 -0
  130. data/lib/random-words/words/latin/prepositions.txt +18 -0
  131. data/lib/random-words/words/latin/terminators.txt +16 -0
  132. data/lib/random-words/words/spanish/adjectives.txt +81 -0
  133. data/lib/random-words/words/spanish/adverbs.txt +87 -0
  134. data/lib/random-words/words/spanish/articles-plural.txt +4 -0
  135. data/lib/random-words/words/spanish/articles-singular.txt +4 -0
  136. data/lib/random-words/words/spanish/clauses.txt +74 -0
  137. data/lib/random-words/words/spanish/config.yml +4 -0
  138. data/lib/random-words/words/spanish/conjunctions-coordinating.txt +8 -0
  139. data/lib/random-words/words/spanish/conjunctions-subordinate.txt +16 -0
  140. data/lib/random-words/words/spanish/names.txt +74 -0
  141. data/lib/random-words/words/spanish/nouns-plural.txt +92 -0
  142. data/lib/random-words/words/spanish/nouns-singular.txt +125 -0
  143. data/lib/random-words/words/spanish/numbers.yml +5 -0
  144. data/lib/random-words/words/spanish/prepositions.txt +31 -0
  145. data/lib/random-words/words/spanish/terminators.txt +17 -0
  146. data/lib/random-words/words/spanish/verbs-passive.txt +495 -0
  147. data/lib/random-words/words/spanish/verbs-plural.txt +326 -0
  148. data/lib/random-words/words/spanish/verbs-singular.txt +351 -0
  149. data/lib/random-words/words/veggie/adjectives.txt +111 -0
  150. data/lib/random-words/words/veggie/adverbs.txt +81 -0
  151. data/lib/random-words/words/veggie/articles-plural.txt +10 -0
  152. data/lib/random-words/words/veggie/articles-singular.txt +10 -0
  153. data/lib/random-words/words/veggie/clauses.txt +57 -0
  154. data/lib/random-words/words/veggie/config.yml +4 -0
  155. data/lib/random-words/words/veggie/conjunctions-coordinating.txt +20 -0
  156. data/lib/random-words/words/veggie/conjunctions-subordinate.txt +29 -0
  157. data/lib/random-words/words/veggie/names.txt +74 -0
  158. data/lib/random-words/words/veggie/nouns-plural.txt +78 -0
  159. data/lib/random-words/words/veggie/nouns-singular.txt +105 -0
  160. data/lib/random-words/words/veggie/numbers.yml +5 -0
  161. data/lib/random-words/words/veggie/prepositions.txt +45 -0
  162. data/lib/random-words/words/veggie/terminators.txt +16 -0
  163. data/lib/random-words/words/veggie/verbs-passive.txt +56 -0
  164. data/lib/random-words/words/veggie/verbs-plural.txt +79 -0
  165. data/lib/random-words/words/veggie/verbs-singular.txt +79 -0
  166. data/lib/random-words.rb +28 -0
  167. data/random-words.gemspec +2 -0
  168. data/src/_README.md +153 -4
  169. metadata +168 -7
  170. data/lib/random-words/words/bacon/numbers.txt +0 -21
  171. data/lib/random-words/words/corporate/numbers.txt +0 -21
  172. data/lib/random-words/words/english/numbers.txt +0 -21
  173. data/lib/random-words/words/latin/numbers.txt +0 -21
@@ -8,8 +8,13 @@ module RandomWords
8
8
  # Random character, word, and sentence generator
9
9
  class Generator
10
10
  # @return [Array<String>] arrays of elements of speech
11
- attr_accessor :nouns, :verbs, :passive_verbs, :adverbs, :adjectives, :articles, :clauses, :subordinate_conjunctions,
12
- :terminators, :numbers, :plural_nouns, :plural_verbs, :plural_articles
11
+ attr_reader :nouns, :verbs, :passive_verbs, :adverbs, :adjectives, :articles, :clauses, :subordinate_conjunctions,
12
+ :terminators, :numbers, :plural_nouns, :plural_verbs, :plural_articles, :prepositions, :coordinating_conjunctions,
13
+ :all_words, :extended_punctuation
14
+
15
+ # Whether to use extended punctuation
16
+ # @return [Boolean] true if extended punctuation is used, false otherwise
17
+ attr_reader :use_extended_punctuation
13
18
 
14
19
  # @return [Integer] Number of sentences in paragraphs
15
20
  attr_reader :paragraph_length
@@ -20,8 +25,8 @@ module RandomWords
20
25
  # @return [Symbol] Dictionary in use
21
26
  attr_reader :source
22
27
 
23
- # @return [Boolean] Testing mode
24
- attr_accessor :testing
28
+ # @return [Hash<String, RandomWords::Source>] List of available sources
29
+ attr_reader :sources
25
30
 
26
31
  # Define the default sentence parts
27
32
  # These parts will be used to generate random sentences and character strings
@@ -30,7 +35,7 @@ module RandomWords
30
35
 
31
36
  # Initialize the generator with a source and options
32
37
  # @param source [Symbol] The source of the words (e.g., :english)
33
- # @param _options [Hash] Options for the generator (e.g., length, paragraph_length)
38
+ # @param options [Hash] Options for the generator (e.g., length, paragraph_length)
34
39
  # @example
35
40
  # generator = RandomWords::Generator.new(:english, sentence_length: :medium, paragraph_length: 5)
36
41
  # generator.source = :french
@@ -38,31 +43,55 @@ module RandomWords
38
43
  # generator.sentence_length = :long
39
44
  # generator.paragraph_length = 3
40
45
  def initialize(source = :english, options = {})
46
+ @tested = []
47
+ @config = RandomWords::Config.new(source)
41
48
  @source = source
42
- @nouns = from_file('nouns-singular.txt')
43
- @plural_nouns = from_file('nouns-plural.txt')
44
- @verbs = from_file('verbs-singular.txt')
45
- @plural_verbs = from_file('verbs-plural.txt')
46
- @passive_verbs = from_file('verbs-passive.txt')
47
- @adverbs = from_file('adverbs.txt')
48
- @adjectives = from_file('adjectives.txt')
49
- @articles = from_file('articles-singular.txt')
50
- @plural_articles = from_file('articles-plural.txt')
51
- @clauses = from_file('clauses.txt')
52
- @subordinate_conjunctions = from_file('conjunctions-subordinate.txt')
53
-
54
- @numbers = from_file('numbers.txt')
49
+
50
+ @nouns = @config.dictionary[:nouns]
51
+ @plural_nouns = @config.dictionary[:plural_nouns]
52
+ @verbs = @config.dictionary[:verbs]
53
+ @plural_verbs = @config.dictionary[:plural_verbs]
54
+ @passive_verbs = @config.dictionary[:passive_verbs]
55
+ @adverbs = @config.dictionary[:adverbs]
56
+ @adjectives = @config.dictionary[:adjectives]
57
+ @articles = @config.dictionary[:articles]
58
+ @plural_articles = @config.dictionary[:plural_articles]
59
+ @prepositions = @config.dictionary[:prepositions]
60
+ @clauses = @config.dictionary[:clauses]
61
+ @coordinating_conjunctions = @config.dictionary[:coordinating_conjunctions]
62
+ @subordinate_conjunctions = @config.dictionary[:subordinate_conjunctions]
63
+ @numbers = @config.dictionary[:numbers]
64
+ @sources = @config.sources
65
+ @terminators = @config.dictionary[:terminators]
66
+ @names = [@config.dictionary[:first_names], @config.dictionary[:last_names]]
67
+ @all_words = @config.dictionary[:all_words]
55
68
 
56
69
  @options = {
57
70
  sentence_length: :medium,
58
- paragraph_length: 5
71
+ paragraph_length: 5,
72
+ use_extended_punctuation: false
59
73
  }
74
+
60
75
  @options.merge!(options) if options.is_a?(Hash)
61
76
  @sentence_length = @options[:sentence_length]
62
77
  @paragraph_length = @options[:paragraph_length]
78
+ @use_extended_punctuation = @options[:use_extended_punctuation]
79
+
80
+ if @use_extended_punctuation
81
+ @terminators.concat(@config.dictionary[:extended_punctuation])
82
+ end
63
83
  lengths
64
84
  end
65
85
 
86
+ # Shortcut for RandomWords::Config.create_user_dictionary
87
+ # @param title [String] The title of the user dictionary
88
+ # @return [Symbol] The symbolized name of the dictionary
89
+ # @example
90
+ # create_dictionary('my_custom_dictionary')
91
+ def create_dictionary(title)
92
+ @config.create_user_dictionary(title)
93
+ end
94
+
66
95
  # Define number of sentences in paragraphs
67
96
  # @param length [Integer] The number of sentences in the paragraph
68
97
  def paragraph_length=(length)
@@ -89,20 +118,14 @@ module RandomWords
89
118
  @sentence_length = to_set
90
119
  end
91
120
 
92
- # Bad init method for testing purposes
93
- # @!visibility private
94
- def bad_init
95
- @nouns = from_file('nouns-noent.txt')
96
- end
97
-
98
121
  # define all lengths for testing purposes
99
122
  # @!visibility private
100
123
  def define_all_lengths
101
124
  @lengths = {
102
- short: 60,
103
- medium: 200,
104
- long: 300,
105
- very_long: 500
125
+ short: 20,
126
+ medium: 60,
127
+ long: 100,
128
+ very_long: 300
106
129
  }
107
130
  res = []
108
131
  res << define_length(:short)
@@ -122,26 +145,33 @@ module RandomWords
122
145
  # These methods are used to test the random generation of words and sentences
123
146
  # @!visibility private
124
147
  def test_random
125
- @testing = true
148
+ RandomWords.testing = true
149
+ @use_extended_punctuation = true
126
150
  res = []
127
151
  res << random_noun
128
152
  res << random_verb
129
153
  res << random_adjective
130
154
  res << random_adverb
131
155
  res << random_article
132
- res << random_article_for_noun('apple')
133
- res << random_article_for_noun('apples')
134
- res << random_article_for_noun('banana')
135
- res << random_article_for_noun('bananas')
156
+ res << random_article_for_word('apple')
157
+ res << random_article_for_word('apples')
158
+ res << random_article_for_word('banana')
159
+ res << random_article_for_word('bananas')
136
160
  res << random_plural_article
137
161
  res << random_clause
138
162
  res << random_subordinate_conjunction
163
+ res << random_coordinating_conjunction
139
164
  res << random_number_with_plural
140
165
  res << random_conjunction
141
166
  res << random_passive_verb
142
167
  res << random_plural_noun
143
168
  res << random_plural_verb
169
+ res << random_preposition
170
+ res << random_prepositional_phrase
171
+ res << random_terminator.join(' ')
144
172
  res << generate_additional_clauses.join(' ')
173
+ res << words_of_length(5).join(' ')
174
+ res << nouns_of_length(5).join(' ')
145
175
  res
146
176
  end
147
177
 
@@ -150,12 +180,19 @@ module RandomWords
150
180
  initialize(new_source)
151
181
  end
152
182
 
183
+ def use_extended_punctuation=(use_extended_punctuation)
184
+ raise ArgumentError, 'use_extended_punctuation must be a boolean' unless [true, false].include?(use_extended_punctuation)
185
+
186
+ @use_extended_punctuation = use_extended_punctuation
187
+ @terminators.concat(@config.dictionary[:extended_punctuation]) if use_extended_punctuation
188
+ end
189
+
153
190
  # Refactored lengths and lengths= methods
154
191
  # This method returns the lengths of sentences
155
192
  # The default lengths are set to the following values:
156
- # short: 60, medium: 200, long: 300, very_long: 500
193
+ # short: 20, medium: 60, long: 100, very_long: 300
157
194
  def lengths
158
- @lengths ||= { short: 60, medium: 200, long: 300, very_long: 500 }
195
+ @lengths ||= { short: 20, medium: 60, long: 100, very_long: 300 }
159
196
  end
160
197
 
161
198
  # This method allows you to set new lengths for the sentences
@@ -197,7 +234,7 @@ module RandomWords
197
234
  def characters(min, max = nil, whole_words: true, whitespace: true, dead_switch: 0)
198
235
  result = ''
199
236
  max ||= min
200
- raise ArgumentError, 'Infinite loop detected' if dead_switch > 20
237
+ raise ArgumentError, 'Infinite loop detected' if dead_switch > 40
201
238
 
202
239
  whole_words = false if dead_switch > 15
203
240
 
@@ -248,17 +285,17 @@ module RandomWords
248
285
  def generate_combined_sentence(length = nil)
249
286
  length ||= define_length(@sentence_length)
250
287
  sentence = generate_sentence
251
- return sentence.capitalize.compress.terminate if sentence.length > length
288
+ return sentence.to_sent(random_terminator).fix_caps(terminators) if sentence.length > length
252
289
 
253
290
  while sentence.length < length
254
291
  # Generate a random number of sentences to combine
255
292
  new_sentence = generate_sentence(length / 2)
256
293
 
257
294
  # Combine the sentences with random conjunctions
258
- sentence = "#{sentence.strip.no_term}, #{random_conjunction} #{new_sentence}"
295
+ sentence = "#{sentence.strip.no_term}, #{random_coordinating_conjunction} #{new_sentence}"
259
296
  end
260
297
 
261
- sentence.capitalize.compress.terminate
298
+ sentence.to_sent(random_terminator).fix_caps(terminators)
262
299
  end
263
300
 
264
301
  # Generate a random paragraph
@@ -275,18 +312,78 @@ module RandomWords
275
312
  sentences.join(' ').strip.compress
276
313
  end
277
314
 
315
+ # Generate a random name
316
+ # @return [String] A randomly generated name
317
+ def name
318
+ random_name
319
+ end
320
+
321
+ # Generate a random code language
322
+ # @return [Symbol] A randomly selected code language
323
+ def code_lang
324
+ code_langs = %i[python ruby swift javascript css rust go java]
325
+ code_langs[Random.rand(code_langs.count)]
326
+ end
327
+
328
+ # Return random code snippet
329
+ # @param lang [Symbol] The language of the code snippet
330
+ # @return [String] A randomly generated code snippet
331
+ def code_snippet(lang = nil)
332
+ code_snippets = {
333
+ python: %(def hello_world():\n print("Hello, World!")),
334
+ ruby: %(def hello_world\n puts "Hello, World!"\nend),
335
+ swift: %(func helloWorld() {\n print(\"Hello, World!\")\n}),
336
+ javascript: %(function helloWorld() {\n console.log(\"Hello, World!\");\n}),
337
+ css: %(body {\n background-color: #f0f0f0;\n font-family: Arial, sans-serif;\n}\nh1 {\n color: #333;\n}),
338
+ rust: %(fn main() {\n println!("Hello, World!");\n}),
339
+ go: %(package main\nimport \"fmt\"\nfunc main() {\n fmt.Println(\"Hello, World!\")\n}),
340
+ java: %(public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n})
341
+ }
342
+ lang ||= code_lang
343
+ code_snippets[lang.to_sym]
344
+ end
345
+
346
+ # Generate random markdown
347
+ # @param settings [Hash] Settings for generating markdown
348
+ # @return [String] A randomly generated markdown string
349
+ def markdown(settings = {})
350
+ input = RandomWords::LoremMarkdown.new(settings).output
351
+ RandomWords::HTML2Markdown.new(input)
352
+ end
353
+
354
+ # Generate random HTML
355
+ # @param settings [Hash] Settings for generating HTML
356
+ # @return [String] A randomly generated HTML string
357
+ def html(settings = {})
358
+ RandomWords::LoremMarkdown.new(settings).output
359
+ end
360
+
278
361
  private
279
362
 
280
- # Handle overflow when the new result exceeds the maximum length
363
+ # Struct for overflow configuration
364
+ # @!visibility private
281
365
  OverflowConfig = Struct.new(:new_result, :result, :min, :max, :whole_words, :whitespace, :dead_switch)
282
366
 
367
+ # Handle overflow when the new result exceeds the maximum length
368
+ # @param config [OverflowConfig] The configuration for handling overflow
369
+ # @return [String] The modified result after handling overflow
370
+ # This method checks if the new result exceeds the maximum length.
371
+ # If it does, it tries to add a random word of the required length.
372
+ # If the new result is still too long, it generates a new string with the specified length.
373
+ # If the new result is shorter than the minimum length, it generates a new string with the minimum length.
374
+ # @example
375
+ # handle_overflow(config) # Handles overflow for the given configuration
376
+ # @see OverflowConfig
377
+ # @see #characters
378
+ # @see #words_of_length
379
+ # @see #nouns_of_length
283
380
  def handle_overflow(config)
284
381
  space = config.whitespace ? ' ' : ''
285
- needed = config.max - config.result.compress.length - space.length
382
+ needed = config.max - config.result.compress.length
383
+ needed -= space.length if needed > 0 && config.result.compress.length.positive?
286
384
  config.min = config.max if config.min > config.max
287
-
288
385
  if needed > 1
289
- options = nouns_of_length(needed)
386
+ options = words_of_length(needed)
290
387
  return "#{config.result}#{space}#{options.sample}".compress unless options.empty?
291
388
  end
292
389
 
@@ -294,9 +391,9 @@ module RandomWords
294
391
  return characters(config.min, config.max, whole_words: config.whole_words, whitespace: config.whitespace,
295
392
  dead_switch: config.dead_switch + 1)
296
393
  end
297
-
394
+ puts "made it"
298
395
  truncated = config.new_result.compress[0...config.max]
299
- truncated.strip if truncated.strip.length.between?(config.min, config.max)
396
+ truncated.compress.length == config.max ? truncated.compress : nil
300
397
  end
301
398
 
302
399
  # Generate a random sentence with a specified length
@@ -313,7 +410,7 @@ module RandomWords
313
410
  sentence_components = []
314
411
 
315
412
  # Randomly decide if we include a plural noun with a number
316
- sentence_components << random_number_with_plural if roll(20) # 20% chance to include a plural noun
413
+ sentence_components << random_number_with_plural if roll(10) # 10% chance to include a plural noun
317
414
 
318
415
  # Construct main clause
319
416
  sentence_components << generate_main_clause
@@ -321,38 +418,25 @@ module RandomWords
321
418
  # Include any additional clauses
322
419
  # sentence_components.concat(generate_additional_clauses)
323
420
 
324
- # while sentence_components.join(' ').strip.length < length
325
- # # Randomly include a subordinate conjunction
326
- # additional_clauses = generate_additional_clauses
327
- # sentence_components.concat(additional_clauses)
328
- # sentence_components.map!(&:strip)
329
- # break if sentence_components.join(' ').length >= length
330
-
331
- # conjunction = random_subordinate_conjunction.strip
332
- # sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
333
- # end
421
+ while sentence_components.join(' ').strip.length < length
422
+ # Randomly include a subordinate conjunction
423
+ additional_clauses = generate_additional_clauses
424
+ sentence_components.concat(additional_clauses)
425
+ sentence_components.map!(&:strip)
426
+ break if sentence_components.join(' ').length >= length
427
+ conjunction = if roll(50) || (RandomWords.testing && !RandomWords.tested.include?('subordinate_conjunction'))
428
+ RandomWords.tested << 'subordinate_conjunction' if RandomWords.testing
429
+ random_subordinate_conjunction.strip
430
+ else
431
+ RandomWords.tested << 'coordinating_conjunction' if RandomWords.testing
432
+ random_coordinating_conjunction.strip
433
+ end
434
+ # sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
435
+ sentence_components << conjunction unless conjunction.empty?
436
+ end
334
437
 
335
438
  # Join all parts into a single sentence
336
- sentence_components.join(' ').strip.compress
337
- end
338
-
339
- # Load words from a dictionary file
340
- # Files are plain text with one word or phrase per line
341
- # The @source variable defines which dictionary to be used and should be defined before calling this method
342
- # @param filename [String] The name of the file to load
343
- # @return [Array] An array of words loaded from the file
344
- # @example
345
- # from_file('nouns.txt') # Loads words from words/[source]/nouns.txt
346
- def from_file(filename)
347
- filename = "#{filename.sub(/\.txt$/, '')}.txt"
348
- path = File.join(__dir__, 'words', @source.to_s, filename)
349
-
350
- path = File.join(__dir__, 'words', 'english', filename) unless File.exist?(path)
351
-
352
- File.read(path).split("\n").map(&:strip) # Changed from split_lines to split("\n")
353
- rescue Errno::ENOENT
354
- warn "File not found: #{filename}"
355
- []
439
+ sentence_components.join(" #{roll(50) ? random_coordinating_conjunction : random_subordinate_conjunction} ").strip.compress
356
440
  end
357
441
 
358
442
  # Convert a length symbol to a specific length value
@@ -384,12 +468,22 @@ module RandomWords
384
468
  nouns.select { |word| word.length == length }
385
469
  end
386
470
 
471
+ # Get a list of words matching a specific length
472
+ # @param length [Integer] The desired length of the words
473
+ # @return [Array] An array of words with the specified length
474
+ # @example
475
+ # words_of_length(5) # Returns an array of words with a length of 5 characters
476
+ # @see #nouns_of_length
477
+ def words_of_length(length)
478
+ all_words.select { |word| word.length == length }
479
+ end
480
+
387
481
  # Generate a random conjunction
388
482
  # @return [String] A randomly selected conjunction
389
483
  # @example
390
484
  # random_conjunction # Returns a random conjunction
391
485
  def random_conjunction
392
- %w[and or but].sample
486
+ coordinating_conjunctions.sample
393
487
  end
394
488
 
395
489
  # Generate a random number with a plural noun
@@ -398,8 +492,15 @@ module RandomWords
398
492
  # @example
399
493
  # random_number_with_plural # Returns a string like "three cats"
400
494
  def random_number_with_plural
401
- number = numbers.sample
402
- "#{number} #{random_plural_noun}"
495
+ num = rand(1000)
496
+ number = num.to_word(@numbers)
497
+ if num == 1 || (RandomWords.testing && !RandomWords.tested.include?('random_noun'))
498
+ RandomWords.tested << 'random_noun' if RandomWords.testing
499
+ "#{number} #{random_noun}"
500
+ else
501
+ RandomWords.tested << 'random_plural_noun' if RandomWords.testing
502
+ "#{number} #{random_plural_noun}"
503
+ end
403
504
  end
404
505
 
405
506
  # Generate a random noun
@@ -451,18 +552,20 @@ module RandomWords
451
552
  end
452
553
 
453
554
  # Generate a random article for a noun
454
- # @param noun [String] The noun for which to generate an article
555
+ # @param word [String] The noun for which to generate an article
455
556
  # @return [String] A randomly selected article for the given noun
456
557
  # This method checks if the noun is plural and selects an appropriate article.
457
558
  # If the noun starts with a vowel, it uses 'an' instead of 'a'.
458
559
  # @example
459
- # random_article_for_noun('apple') # Returns 'an'
460
- def random_article_for_noun(noun)
461
- article = plural_nouns.include?(noun) ? random_plural_article : random_article
462
- article = 'an' if @testing
463
- if noun.start_with?(/[aeiou]/i) && article =~ /^an?$/
464
- article = 'an' if article == 'a'
465
- elsif article == 'an'
560
+ # random_article_for_word('apple') # Returns 'an'
561
+ def random_article_for_word(word)
562
+ article = plural_nouns.include?(word) ? random_plural_article : random_article
563
+ # puts [word, article].inspect
564
+ article = 'an' if RandomWords.testing && !RandomWords.tested.include?('random_article_for_word')
565
+ RandomWords.tested << 'random_article_for_word' if RandomWords.testing
566
+ if word.start_with?(/[aeiou]/i) && article =~ /^an?$/i
567
+ article = 'an'
568
+ elsif article =~ /^an$/i
466
569
  article = 'a'
467
570
  end
468
571
  article
@@ -486,6 +589,47 @@ module RandomWords
486
589
  subordinate_conjunctions.sample
487
590
  end
488
591
 
592
+ # Generate a random coordinating conjunction
593
+ # @return [String] A randomly selected coordinating conjunction
594
+ def random_coordinating_conjunction
595
+ coordinating_conjunctions.sample
596
+ end
597
+
598
+ # Generate a random preposition
599
+ # @return [String] A randomly selected preposition
600
+ def random_preposition
601
+ prepositions.sample
602
+ end
603
+
604
+ # Generate a random prepositional phrase
605
+ # @return [String] A randomly generated prepositional phrase
606
+ # This method constructs a prepositional phrase using a random preposition and a random noun.
607
+ def random_prepositional_phrase
608
+ preposition = random_preposition
609
+ phrase = if roll(20) || (@testing && !@tested.include?('random_plural_article'))
610
+ @tested << 'random_plural_article' if @testing
611
+ "#{random_plural_article} #{random_number_with_plural}"
612
+ else
613
+ @tested << 'random_article_for_word' if @testing
614
+ noun = random_noun
615
+ "#{random_article_for_word(noun)} #{noun}"
616
+ end
617
+
618
+ "#{preposition} #{phrase}"
619
+ end
620
+
621
+ # Generate a random terminator
622
+ # @return [Array] A randomly selected terminator pair
623
+ def random_terminator
624
+ terminators.sample
625
+ end
626
+
627
+ def random_name
628
+ first_name = @names[0].sample
629
+ last_name = @names[1].sample
630
+ "#{first_name} #{last_name}"
631
+ end
632
+
489
633
  # Generate a random main clause
490
634
  # @return [String] A randomly generated main clause
491
635
  # This method constructs a main clause using a random number of words.
@@ -495,15 +639,17 @@ module RandomWords
495
639
  # @example
496
640
  # generate_main_clause # Returns a random main clause
497
641
  def generate_main_clause
498
- beginning = if roll(50)
642
+
643
+ beginning = if roll(20)
499
644
  "#{random_number_with_plural} #{random_adverb} #{random_plural_verb}"
500
645
  else
501
646
  noun = random_noun
502
- "#{random_article_for_noun(noun)} #{random_adjective} #{noun} #{random_adverb} #{random_verb}"
647
+ adjective = random_adjective
648
+ "#{random_article_for_word(adjective)} #{adjective} #{noun} #{random_adverb} #{random_verb}"
503
649
  end
504
-
505
- tail = roll(20) ? ", #{random_clause}" : ''
506
- "#{beginning.strip}#{tail.strip}"
650
+ tail = roll(50) ? " #{random_prepositional_phrase}" : ""
651
+ tail += roll(10) ? ", #{random_clause}" : ""
652
+ "#{beginning.strip.sub(/,?$/, ', ')}#{tail}"
507
653
  end
508
654
 
509
655
  # Simplified generate_additional_clauses
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RandomWords
4
+ # Hash helpers for RandomWords
5
+ class ::Hash
6
+ # Turn all keys into symbols
7
+ def symbolize_keys
8
+ each_with_object({}) { |(k, v), hsh| hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v }
9
+ end
10
+
11
+ # Turn all keys into strings
12
+ def stringify_keys
13
+ each_with_object({}) { |(k, v), hsh| hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v }
14
+ end
15
+ end
16
+ end