random-words 1.0.5 → 1.0.7

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 (186) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +166 -39
  3. data/.rubocop.yml +4 -0
  4. data/.rubocop_todo.yml +313 -0
  5. data/CHANGELOG.md +67 -0
  6. data/Gemfile +15 -12
  7. data/Gemfile.lock +46 -26
  8. data/README.md +208 -15
  9. data/bin/randw +258 -92
  10. data/lib/random-words/array.rb +71 -0
  11. data/lib/random-words/config.rb +254 -0
  12. data/lib/random-words/generator.rb +257 -94
  13. data/lib/random-words/hash.rb +16 -0
  14. data/lib/random-words/html2markdown.rb +205 -0
  15. data/lib/random-words/lorem-markdown.rb +397 -0
  16. data/lib/random-words/number-to-word.rb +139 -0
  17. data/lib/random-words/source.rb +106 -0
  18. data/lib/random-words/string.rb +224 -45
  19. data/lib/random-words/table-cleanup.rb +210 -0
  20. data/lib/random-words/version.rb +1 -1
  21. data/lib/random-words/words/1984/adjectives.txt +103 -0
  22. data/lib/random-words/words/1984/adverbs.txt +92 -0
  23. data/lib/random-words/words/1984/articles-plural.txt +10 -0
  24. data/lib/random-words/words/1984/articles-singular.txt +10 -0
  25. data/lib/random-words/words/1984/clauses.txt +79 -0
  26. data/lib/random-words/words/1984/config.yml +4 -0
  27. data/lib/random-words/words/1984/conjunctions-coordinating.txt +20 -0
  28. data/lib/random-words/words/1984/conjunctions-subordinate.txt +29 -0
  29. data/lib/random-words/words/1984/names.txt +12 -0
  30. data/lib/random-words/words/1984/nouns-plural.txt +89 -0
  31. data/lib/random-words/words/1984/nouns-singular.txt +74 -0
  32. data/lib/random-words/words/1984/numbers.yml +5 -0
  33. data/lib/random-words/words/1984/phrases.txt +16 -0
  34. data/lib/random-words/words/1984/prepositions.txt +89 -0
  35. data/lib/random-words/words/1984/terminators.txt +16 -0
  36. data/lib/random-words/words/1984/verbs-passive.txt +83 -0
  37. data/lib/random-words/words/1984/verbs-plural.txt +91 -0
  38. data/lib/random-words/words/1984/verbs-singular.txt +110 -0
  39. data/lib/random-words/words/alice/adjectives.txt +80 -0
  40. data/lib/random-words/words/alice/adverbs.txt +87 -0
  41. data/lib/random-words/words/alice/articles-plural.txt +10 -0
  42. data/lib/random-words/words/alice/articles-singular.txt +10 -0
  43. data/lib/random-words/words/alice/clauses.txt +81 -0
  44. data/lib/random-words/words/alice/config.yml +4 -0
  45. data/lib/random-words/words/alice/conjunctions-coordinating.txt +20 -0
  46. data/lib/random-words/words/alice/conjunctions-subordinate.txt +29 -0
  47. data/lib/random-words/words/alice/names.txt +10 -0
  48. data/lib/random-words/words/alice/nouns-plural.txt +95 -0
  49. data/lib/random-words/words/alice/nouns-singular.txt +82 -0
  50. data/lib/random-words/words/alice/numbers.yml +5 -0
  51. data/lib/random-words/words/alice/phrases.txt +16 -0
  52. data/lib/random-words/words/alice/prepositions.txt +45 -0
  53. data/lib/random-words/words/alice/terminators.txt +16 -0
  54. data/lib/random-words/words/alice/verbs-passive.txt +495 -0
  55. data/lib/random-words/words/alice/verbs-plural.txt +115 -0
  56. data/lib/random-words/words/alice/verbs-singular.txt +97 -0
  57. data/lib/random-words/words/bacon/clauses.txt +592 -592
  58. data/lib/random-words/words/bacon/config.yml +4 -0
  59. data/lib/random-words/words/bacon/conjunctions-coordinating.txt +20 -0
  60. data/lib/random-words/words/bacon/names.txt +54 -0
  61. data/lib/random-words/words/bacon/numbers.yml +5 -0
  62. data/lib/random-words/words/bacon/phrases.txt +20 -0
  63. data/lib/random-words/words/bacon/prepositions.txt +45 -0
  64. data/lib/random-words/words/bacon/terminators.txt +16 -0
  65. data/lib/random-words/words/corporate/config.yml +4 -0
  66. data/lib/random-words/words/corporate/conjunctions-coordinating.txt +20 -0
  67. data/lib/random-words/words/corporate/names.txt +74 -0
  68. data/lib/random-words/words/corporate/numbers.yml +5 -0
  69. data/lib/random-words/words/corporate/phrases.txt +29 -0
  70. data/lib/random-words/words/corporate/prepositions.txt +45 -0
  71. data/lib/random-words/words/corporate/terminators.txt +16 -0
  72. data/lib/random-words/words/doctor/adjectives.txt +92 -0
  73. data/lib/random-words/words/doctor/adverbs.txt +92 -0
  74. data/lib/random-words/words/doctor/articles-plural.txt +10 -0
  75. data/lib/random-words/words/doctor/articles-singular.txt +10 -0
  76. data/lib/random-words/words/doctor/clauses.txt +83 -0
  77. data/lib/random-words/words/doctor/config.yml +10 -0
  78. data/lib/random-words/words/doctor/conjunctions-coordinating.txt +20 -0
  79. data/lib/random-words/words/doctor/conjunctions-subordinate.txt +29 -0
  80. data/lib/random-words/words/doctor/names.txt +74 -0
  81. data/lib/random-words/words/doctor/nouns-plural.txt +84 -0
  82. data/lib/random-words/words/doctor/nouns-singular.txt +84 -0
  83. data/lib/random-words/words/doctor/numbers.yml +5 -0
  84. data/lib/random-words/words/doctor/phrases.txt +29 -0
  85. data/lib/random-words/words/doctor/prepositions.txt +45 -0
  86. data/lib/random-words/words/doctor/terminators.txt +16 -0
  87. data/lib/random-words/words/doctor/verbs-passive.txt +83 -0
  88. data/lib/random-words/words/doctor/verbs-plural.txt +88 -0
  89. data/lib/random-words/words/doctor/verbs-singular.txt +83 -0
  90. data/lib/random-words/words/english/clauses.txt +592 -592
  91. data/lib/random-words/words/english/config.yml +4 -0
  92. data/lib/random-words/words/english/conjunctions-coordinating.txt +20 -0
  93. data/lib/random-words/words/english/names.txt +74 -0
  94. data/lib/random-words/words/english/numbers.yml +5 -0
  95. data/lib/random-words/words/english/phrases.txt +29 -0
  96. data/lib/random-words/words/english/prepositions.txt +45 -0
  97. data/lib/random-words/words/english/terminators.txt +16 -0
  98. data/lib/random-words/words/english/verbs-plural.txt +1 -0
  99. data/lib/random-words/words/english/verbs-singular.txt +1 -0
  100. data/lib/random-words/words/foulmouth/adjectives.txt +89 -0
  101. data/lib/random-words/words/foulmouth/adverbs.txt +97 -0
  102. data/lib/random-words/words/foulmouth/articles-plural.txt +12 -0
  103. data/lib/random-words/words/foulmouth/articles-singular.txt +11 -0
  104. data/lib/random-words/words/foulmouth/clauses.txt +74 -0
  105. data/lib/random-words/words/foulmouth/config.yml +4 -0
  106. data/lib/random-words/words/foulmouth/conjunctions-coordinating.txt +20 -0
  107. data/lib/random-words/words/foulmouth/conjunctions-subordinate.txt +29 -0
  108. data/lib/random-words/words/foulmouth/names.txt +81 -0
  109. data/lib/random-words/words/foulmouth/nouns-plural.txt +96 -0
  110. data/lib/random-words/words/foulmouth/nouns-singular.txt +99 -0
  111. data/lib/random-words/words/foulmouth/numbers.yml +5 -0
  112. data/lib/random-words/words/foulmouth/phrases.txt +30 -0
  113. data/lib/random-words/words/foulmouth/prepositions.txt +35 -0
  114. data/lib/random-words/words/foulmouth/terminators.txt +16 -0
  115. data/lib/random-words/words/foulmouth/verbs-passive.txt +143 -0
  116. data/lib/random-words/words/foulmouth/verbs-plural.txt +91 -0
  117. data/lib/random-words/words/foulmouth/verbs-singular.txt +104 -0
  118. data/lib/random-words/words/hipster/adjectives.txt +100 -0
  119. data/lib/random-words/words/hipster/adverbs.txt +89 -0
  120. data/lib/random-words/words/hipster/articles-plural.txt +10 -0
  121. data/lib/random-words/words/hipster/articles-singular.txt +10 -0
  122. data/lib/random-words/words/hipster/clauses.txt +180 -0
  123. data/lib/random-words/words/hipster/config.yml +4 -0
  124. data/lib/random-words/words/hipster/conjunctions-coordinating.txt +20 -0
  125. data/lib/random-words/words/hipster/conjunctions-subordinate.txt +29 -0
  126. data/lib/random-words/words/hipster/names.txt +73 -0
  127. data/lib/random-words/words/hipster/nouns-plural.txt +96 -0
  128. data/lib/random-words/words/hipster/nouns-singular.txt +100 -0
  129. data/lib/random-words/words/hipster/numbers.yml +5 -0
  130. data/lib/random-words/words/hipster/phrases.txt +21 -0
  131. data/lib/random-words/words/hipster/prepositions.txt +45 -0
  132. data/lib/random-words/words/hipster/terminators.txt +16 -0
  133. data/lib/random-words/words/hipster/verbs-passive.txt +88 -0
  134. data/lib/random-words/words/hipster/verbs-plural.txt +106 -0
  135. data/lib/random-words/words/hipster/verbs-singular.txt +114 -0
  136. data/lib/random-words/words/latin/config.yml +4 -0
  137. data/lib/random-words/words/latin/conjunctions-coordinating.txt +15 -0
  138. data/lib/random-words/words/latin/names.txt +93 -0
  139. data/lib/random-words/words/latin/numbers.yml +5 -0
  140. data/lib/random-words/words/latin/phrases.txt +16 -0
  141. data/lib/random-words/words/latin/prepositions.txt +18 -0
  142. data/lib/random-words/words/latin/terminators.txt +16 -0
  143. data/lib/random-words/words/spanish/adjectives.txt +81 -0
  144. data/lib/random-words/words/spanish/adverbs.txt +87 -0
  145. data/lib/random-words/words/spanish/articles-plural.txt +4 -0
  146. data/lib/random-words/words/spanish/articles-singular.txt +4 -0
  147. data/lib/random-words/words/spanish/clauses.txt +74 -0
  148. data/lib/random-words/words/spanish/config.yml +4 -0
  149. data/lib/random-words/words/spanish/conjunctions-coordinating.txt +8 -0
  150. data/lib/random-words/words/spanish/conjunctions-subordinate.txt +16 -0
  151. data/lib/random-words/words/spanish/names.txt +61 -0
  152. data/lib/random-words/words/spanish/nouns-plural.txt +92 -0
  153. data/lib/random-words/words/spanish/nouns-singular.txt +125 -0
  154. data/lib/random-words/words/spanish/numbers.yml +5 -0
  155. data/lib/random-words/words/spanish/phrases.txt +31 -0
  156. data/lib/random-words/words/spanish/prepositions.txt +31 -0
  157. data/lib/random-words/words/spanish/terminators.txt +17 -0
  158. data/lib/random-words/words/spanish/verbs-passive.txt +495 -0
  159. data/lib/random-words/words/spanish/verbs-plural.txt +326 -0
  160. data/lib/random-words/words/spanish/verbs-singular.txt +351 -0
  161. data/lib/random-words/words/veggie/adjectives.txt +111 -0
  162. data/lib/random-words/words/veggie/adverbs.txt +81 -0
  163. data/lib/random-words/words/veggie/articles-plural.txt +10 -0
  164. data/lib/random-words/words/veggie/articles-singular.txt +10 -0
  165. data/lib/random-words/words/veggie/clauses.txt +57 -0
  166. data/lib/random-words/words/veggie/config.yml +4 -0
  167. data/lib/random-words/words/veggie/conjunctions-coordinating.txt +20 -0
  168. data/lib/random-words/words/veggie/conjunctions-subordinate.txt +29 -0
  169. data/lib/random-words/words/veggie/names.txt +93 -0
  170. data/lib/random-words/words/veggie/nouns-plural.txt +78 -0
  171. data/lib/random-words/words/veggie/nouns-singular.txt +105 -0
  172. data/lib/random-words/words/veggie/numbers.yml +5 -0
  173. data/lib/random-words/words/veggie/phrases.txt +20 -0
  174. data/lib/random-words/words/veggie/prepositions.txt +45 -0
  175. data/lib/random-words/words/veggie/terminators.txt +16 -0
  176. data/lib/random-words/words/veggie/verbs-passive.txt +56 -0
  177. data/lib/random-words/words/veggie/verbs-plural.txt +79 -0
  178. data/lib/random-words/words/veggie/verbs-singular.txt +79 -0
  179. data/lib/random-words.rb +28 -0
  180. data/random-words.gemspec +3 -1
  181. data/src/_README.md +208 -15
  182. metadata +181 -9
  183. data/lib/random-words/words/bacon/numbers.txt +0 -21
  184. data/lib/random-words/words/corporate/numbers.txt +0 -21
  185. data/lib/random-words/words/english/numbers.txt +0 -21
  186. 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, :phrases, :names
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,17 +25,17 @@ 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
28
33
  SENTENCE_PARTS = %w[random_article random_adjective random_noun random_adverb random_verb random_adjective
29
- random_verb random_adverb].freeze
34
+ random_verb random_adverb random_phrase].freeze
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,54 @@ 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], @config.dictionary[:full_names]]
67
+ @phrases = @config.dictionary[:phrases]
68
+ @all_words = @config.dictionary[:all_words]
55
69
 
56
70
  @options = {
57
71
  sentence_length: :medium,
58
- paragraph_length: 5
72
+ paragraph_length: 5,
73
+ use_extended_punctuation: false
59
74
  }
75
+
60
76
  @options.merge!(options) if options.is_a?(Hash)
61
77
  @sentence_length = @options[:sentence_length]
62
78
  @paragraph_length = @options[:paragraph_length]
79
+ @use_extended_punctuation = @options[:use_extended_punctuation]
80
+
81
+ @terminators.concat(@config.dictionary[:extended_punctuation]) if @use_extended_punctuation
63
82
  lengths
64
83
  end
65
84
 
85
+ # Shortcut for RandomWords::Config.create_user_dictionary
86
+ # @param title [String] The title of the user dictionary
87
+ # @return [Symbol] The symbolized name of the dictionary
88
+ # @example
89
+ # create_dictionary('my_custom_dictionary')
90
+ def create_dictionary(title)
91
+ @config.create_user_dictionary(title)
92
+ end
93
+
66
94
  # Define number of sentences in paragraphs
67
95
  # @param length [Integer] The number of sentences in the paragraph
68
96
  def paragraph_length=(length)
@@ -89,20 +117,14 @@ module RandomWords
89
117
  @sentence_length = to_set
90
118
  end
91
119
 
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
120
  # define all lengths for testing purposes
99
121
  # @!visibility private
100
122
  def define_all_lengths
101
123
  @lengths = {
102
- short: 60,
103
- medium: 200,
104
- long: 300,
105
- very_long: 500
124
+ short: 20,
125
+ medium: 60,
126
+ long: 100,
127
+ very_long: 300
106
128
  }
107
129
  res = []
108
130
  res << define_length(:short)
@@ -122,26 +144,34 @@ module RandomWords
122
144
  # These methods are used to test the random generation of words and sentences
123
145
  # @!visibility private
124
146
  def test_random
125
- @testing = true
147
+ RandomWords.testing = true
148
+ @use_extended_punctuation = true
126
149
  res = []
127
150
  res << random_noun
128
151
  res << random_verb
129
152
  res << random_adjective
130
153
  res << random_adverb
131
154
  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')
155
+ res << random_article_for_word('apple')
156
+ res << random_article_for_word('apples')
157
+ res << random_article_for_word('banana')
158
+ res << random_article_for_word('bananas')
136
159
  res << random_plural_article
137
160
  res << random_clause
138
161
  res << random_subordinate_conjunction
162
+ res << random_coordinating_conjunction
139
163
  res << random_number_with_plural
164
+ res << random_phrase
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(terminators)}, #{random_coordinating_conjunction} #{new_sentence.no_term(terminators)}"
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,80 @@ 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
+ # rubocop:disable Layout/LineLength
329
+ # Return random code snippet
330
+ # @param lang [Symbol] The language of the code snippet
331
+ # @return [String] A randomly generated code snippet
332
+ def code_snippet(lang = nil)
333
+ code_snippets = {
334
+ python: %(def hello_world():\n print("Hello, World!")),
335
+ ruby: %(def hello_world\n puts "Hello, World!"\nend),
336
+ swift: %(func helloWorld() {\n print("Hello, World!")\n}),
337
+ javascript: %(function helloWorld() {\n console.log("Hello, World!");\n}),
338
+ css: %(body {\n background-color: #f0f0f0;\n font-family: Arial, sans-serif;\n}\nh1 {\n color: #333;\n}),
339
+ rust: %(fn main() {\n println!("Hello, World!");\n}),
340
+ go: %(package main\nimport "fmt"\nfunc main() {\n fmt.Println("Hello, World!")\n}),
341
+ java: %(public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n})
342
+ }
343
+ lang ||= code_lang
344
+ code_snippets[lang.to_sym]
345
+ end
346
+ # rubocop:enable Layout/LineLength
347
+
348
+ # Generate random markdown
349
+ # @param settings [Hash] Settings for generating markdown
350
+ # @return [String] A randomly generated markdown string
351
+ def markdown(settings = {})
352
+ input = RandomWords::LoremMarkdown.new(settings).output
353
+ RandomWords::HTML2Markdown.new(input).markdown
354
+ end
355
+
356
+ # Generate random HTML
357
+ # @param settings [Hash] Settings for generating HTML
358
+ # @return [String] A randomly generated HTML string
359
+ def html(settings = {})
360
+ RandomWords::LoremMarkdown.new(settings).output
361
+ end
362
+
278
363
  private
279
364
 
280
- # Handle overflow when the new result exceeds the maximum length
365
+ # Struct for overflow configuration
366
+ # @!visibility private
281
367
  OverflowConfig = Struct.new(:new_result, :result, :min, :max, :whole_words, :whitespace, :dead_switch)
282
368
 
369
+ # Handle overflow when the new result exceeds the maximum length
370
+ # @param config [OverflowConfig] The configuration for handling overflow
371
+ # @return [String] The modified result after handling overflow
372
+ # This method checks if the new result exceeds the maximum length.
373
+ # If it does, it tries to add a random word of the required length.
374
+ # If the new result is still too long, it generates a new string with the specified length.
375
+ # If the new result is shorter than the minimum length, it generates a new string with the minimum length.
376
+ # @example
377
+ # handle_overflow(config) # Handles overflow for the given configuration
378
+ # @see OverflowConfig
379
+ # @see #characters
380
+ # @see #words_of_length
381
+ # @see #nouns_of_length
283
382
  def handle_overflow(config)
284
383
  space = config.whitespace ? ' ' : ''
285
- needed = config.max - config.result.compress.length - space.length
384
+ needed = config.max - config.result.compress.length
385
+ needed -= space.length if needed > 0 && config.result.compress.length.positive?
286
386
  config.min = config.max if config.min > config.max
287
-
288
387
  if needed > 1
289
- options = nouns_of_length(needed)
388
+ options = words_of_length(needed)
290
389
  return "#{config.result}#{space}#{options.sample}".compress unless options.empty?
291
390
  end
292
391
 
@@ -294,9 +393,9 @@ module RandomWords
294
393
  return characters(config.min, config.max, whole_words: config.whole_words, whitespace: config.whitespace,
295
394
  dead_switch: config.dead_switch + 1)
296
395
  end
297
-
396
+ puts 'made it'
298
397
  truncated = config.new_result.compress[0...config.max]
299
- truncated.strip if truncated.strip.length.between?(config.min, config.max)
398
+ truncated.compress.length == config.max ? truncated.compress : nil
300
399
  end
301
400
 
302
401
  # Generate a random sentence with a specified length
@@ -313,7 +412,7 @@ module RandomWords
313
412
  sentence_components = []
314
413
 
315
414
  # 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
415
+ # sentence_components << random_number_with_plural if roll(10) # 10% chance to include a plural noun
317
416
 
318
417
  # Construct main clause
319
418
  sentence_components << generate_main_clause
@@ -321,38 +420,26 @@ module RandomWords
321
420
  # Include any additional clauses
322
421
  # sentence_components.concat(generate_additional_clauses)
323
422
 
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
423
+ while sentence_components.join(' ').strip.length < length
424
+ # Randomly include a subordinate conjunction
425
+ additional_clauses = generate_additional_clauses
426
+ sentence_components.concat(additional_clauses)
427
+ sentence_components.map!(&:strip)
428
+ break if sentence_components.join(' ').length >= length
429
+
430
+ conjunction = if roll(50) || (RandomWords.testing && !RandomWords.tested.include?('subordinate_conjunction'))
431
+ RandomWords.tested << 'subordinate_conjunction' if RandomWords.testing
432
+ random_subordinate_conjunction.strip
433
+ else
434
+ RandomWords.tested << 'coordinating_conjunction' if RandomWords.testing
435
+ random_coordinating_conjunction.strip
436
+ end
437
+ # sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
438
+ sentence_components << conjunction unless conjunction.empty?
439
+ end
334
440
 
335
441
  # 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
- []
442
+ sentence_components.join(" #{roll(50) ? random_coordinating_conjunction : random_subordinate_conjunction} ").strip.compress
356
443
  end
357
444
 
358
445
  # Convert a length symbol to a specific length value
@@ -384,12 +471,22 @@ module RandomWords
384
471
  nouns.select { |word| word.length == length }
385
472
  end
386
473
 
474
+ # Get a list of words matching a specific length
475
+ # @param length [Integer] The desired length of the words
476
+ # @return [Array] An array of words with the specified length
477
+ # @example
478
+ # words_of_length(5) # Returns an array of words with a length of 5 characters
479
+ # @see #nouns_of_length
480
+ def words_of_length(length)
481
+ all_words.select { |word| word.length == length }
482
+ end
483
+
387
484
  # Generate a random conjunction
388
485
  # @return [String] A randomly selected conjunction
389
486
  # @example
390
487
  # random_conjunction # Returns a random conjunction
391
488
  def random_conjunction
392
- %w[and or but].sample
489
+ coordinating_conjunctions.sample
393
490
  end
394
491
 
395
492
  # Generate a random number with a plural noun
@@ -398,8 +495,21 @@ module RandomWords
398
495
  # @example
399
496
  # random_number_with_plural # Returns a string like "three cats"
400
497
  def random_number_with_plural
401
- number = numbers.sample
402
- "#{number} #{random_plural_noun}"
498
+ num = rand(1000)
499
+ number = num.to_word(@numbers)
500
+ if num == 1 || (RandomWords.testing && !RandomWords.tested.include?('random_noun'))
501
+ RandomWords.tested << 'random_noun' if RandomWords.testing
502
+ "#{number} #{random_noun}"
503
+ else
504
+ RandomWords.tested << 'random_plural_noun' if RandomWords.testing
505
+ "#{number} #{random_plural_noun}"
506
+ end
507
+ end
508
+
509
+ # Generate a random phrase
510
+ # @return [String] A randomly selected phrase
511
+ def random_phrase
512
+ phrases.sample
403
513
  end
404
514
 
405
515
  # Generate a random noun
@@ -451,18 +561,20 @@ module RandomWords
451
561
  end
452
562
 
453
563
  # Generate a random article for a noun
454
- # @param noun [String] The noun for which to generate an article
564
+ # @param word [String] The noun for which to generate an article
455
565
  # @return [String] A randomly selected article for the given noun
456
566
  # This method checks if the noun is plural and selects an appropriate article.
457
567
  # If the noun starts with a vowel, it uses 'an' instead of 'a'.
458
568
  # @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'
569
+ # random_article_for_word('apple') # Returns 'an'
570
+ def random_article_for_word(word)
571
+ article = plural_nouns.include?(word) ? random_plural_article : random_article
572
+ # puts [word, article].inspect
573
+ article = 'an' if RandomWords.testing && !RandomWords.tested.include?('random_article_for_word')
574
+ RandomWords.tested << 'random_article_for_word' if RandomWords.testing
575
+ if word.start_with?(/[aeiou]/i) && article =~ /^an?$/i
576
+ article = 'an'
577
+ elsif article =~ /^an$/i
466
578
  article = 'a'
467
579
  end
468
580
  article
@@ -486,6 +598,52 @@ module RandomWords
486
598
  subordinate_conjunctions.sample
487
599
  end
488
600
 
601
+ # Generate a random coordinating conjunction
602
+ # @return [String] A randomly selected coordinating conjunction
603
+ def random_coordinating_conjunction
604
+ coordinating_conjunctions.sample
605
+ end
606
+
607
+ # Generate a random preposition
608
+ # @return [String] A randomly selected preposition
609
+ def random_preposition
610
+ prepositions.sample
611
+ end
612
+
613
+ # Generate a random prepositional phrase
614
+ # @return [String] A randomly generated prepositional phrase
615
+ # This method constructs a prepositional phrase using a random preposition and a random noun.
616
+ def random_prepositional_phrase
617
+ preposition = random_preposition
618
+ phrase = if roll(20) || (@testing && !@tested.include?('random_plural_article'))
619
+ @tested << 'random_plural_article' if @testing
620
+ "#{random_plural_article} #{random_number_with_plural}"
621
+ else
622
+ @tested << 'random_article_for_word' if @testing
623
+ noun = random_noun
624
+ "#{random_article_for_word(noun)} #{noun}"
625
+ end
626
+
627
+ "#{preposition} #{phrase}"
628
+ end
629
+
630
+ # Generate a random terminator
631
+ # @return [Array] A randomly selected terminator pair
632
+ def random_terminator
633
+ terminators.sample
634
+ end
635
+
636
+ def random_name
637
+ return @names[2].sample if (@names[0].empty? || @names[1].empty?) && !@names[2].empty?
638
+
639
+ return @names[2].sample if !@names[2].empty? && roll(60)
640
+
641
+ first_name = @names[0].sample
642
+ middle_initial = roll(20) ? " #{('A'..'Z').to_a.sample}" : ''
643
+ last_name = @names[1].sample
644
+ "#{first_name}#{middle_initial} #{last_name}"
645
+ end
646
+
489
647
  # Generate a random main clause
490
648
  # @return [String] A randomly generated main clause
491
649
  # This method constructs a main clause using a random number of words.
@@ -495,15 +653,20 @@ module RandomWords
495
653
  # @example
496
654
  # generate_main_clause # Returns a random main clause
497
655
  def generate_main_clause
498
- beginning = if roll(50)
656
+ start_with_phrase = roll(20)
657
+ start_with_plural = roll(20)
658
+ beginning = if start_with_phrase
659
+ random_phrase
660
+ elsif start_with_plural
499
661
  "#{random_number_with_plural} #{random_adverb} #{random_plural_verb}"
500
662
  else
501
663
  noun = random_noun
502
- "#{random_article_for_noun(noun)} #{random_adjective} #{noun} #{random_adverb} #{random_verb}"
664
+ adjective = random_adjective
665
+ "#{random_article_for_word(adjective)} #{adjective} #{noun} #{random_adverb} #{random_verb}"
503
666
  end
504
-
505
- tail = roll(20) ? ", #{random_clause}" : ''
506
- "#{beginning.strip}#{tail.strip}"
667
+ tail = roll(50) ? " #{random_prepositional_phrase}" : ''
668
+ tail += roll(10) ? ", #{random_clause}" : ''
669
+ "#{beginning.strip.sub(/,?$/, ', ')}#{tail}"
507
670
  end
508
671
 
509
672
  # 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