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
@@ -0,0 +1,254 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ module RandomWords
5
+ # Configuration
6
+ class Config
7
+ # Language config files
8
+ CONFIG_FILES = %w[
9
+ config
10
+ numbers
11
+ ].freeze
12
+
13
+ # Parts of speech
14
+ SPEECH_PARTS = %w[
15
+ adjectives
16
+ adverbs
17
+ articles-plural
18
+ articles-singular
19
+ clauses
20
+ conjunctions-coordinating
21
+ conjunctions-subordinate
22
+ nouns-plural
23
+ nouns-singular
24
+ prepositions
25
+ terminators
26
+ verbs-passive
27
+ verbs-plural
28
+ verbs-singular
29
+ ].freeze
30
+
31
+ # Dictionary for source
32
+ attr_reader :dictionary
33
+
34
+ # Source directory for languages
35
+ attr_reader :source_dir
36
+
37
+ # Initialize the config with the given language
38
+ # @param lang [Symbol] The language to use
39
+ # @raise [RuntimeError] if no dictionary is found for the given language
40
+ def initialize(lang)
41
+ @lang = lang.to_s
42
+ FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
43
+
44
+ @source_dir = user_dictionary_exist? ? user_lang_dir : builtin_lang_dir
45
+
46
+ raise "No dictionary found for #{@lang}" unless @source_dir
47
+
48
+ rw_source = RandomWords::Source.new(@lang, @source_dir)
49
+
50
+ @dictionary = rw_source.dictionary
51
+ end
52
+
53
+ # Tests if a uer dictionary exists
54
+ # @return [Boolean] true if the user dictionary exists, false otherwise
55
+ # @raise [RuntimeError] if the user dictionary is incomplete
56
+ def user_dictionary_exist?
57
+ if user_lang_dir
58
+ raise "User dictionary for #{@lang} is incomplete. Please run create_user_dictionary." unless all_parts_of_speech?(
59
+ user_lang_dir, @lang
60
+ )
61
+
62
+ true
63
+ else
64
+ false
65
+ end
66
+ end
67
+
68
+ # The user language directory path
69
+ # @return [String] The path to the user language directory
70
+ def user_lang_dir
71
+ File.join(config_dir, 'words', @lang) if File.exist?(File.join(config_dir, 'words', @lang))
72
+ end
73
+
74
+ # The builtin language directory path
75
+ # @param lang [String] The language to use
76
+ # @param basedir [String] The base directory to use
77
+ # @return [String, nil] The path to the builtin language directory or nil if not found
78
+ def builtin_lang_dir(lang = nil, basedir: nil)
79
+ lang ||= @lang
80
+ basedir ||= __dir__
81
+ dir = File.join(basedir, 'words', lang)
82
+ return dir if File.directory?(dir)
83
+
84
+ warn "No dictionary found for #{lang}"
85
+ nil
86
+ end
87
+
88
+ # Check if all parts of speech files exist in the given directory
89
+ # @param dir [String] The directory to check
90
+ # @param lang [String] The language to check
91
+ # @return [Boolean] true if all parts of speech files exist, false otherwise
92
+ def all_parts_of_speech?(dir, lang = nil)
93
+ lang ||= @lang
94
+ dir ||= @source_dir
95
+ exists = true
96
+ SPEECH_PARTS.each do |part|
97
+ next if File.exist?(File.join(dir, "#{part}.txt"))
98
+
99
+ warn "Missing #{File.join(dir, "#{part}.txt")} for #{lang}"
100
+ exists = false
101
+ break
102
+ end
103
+ CONFIG_FILES.each do |file|
104
+ unless File.exist?(File.join(dir, "#{file}.yml"))
105
+ warn "Missing #{File.join(dir, "#{file}.yml")} for #{lang}"
106
+ exists = false
107
+ end
108
+ end
109
+ exists
110
+ end
111
+
112
+ # Create a user dictionary for the given language
113
+ # @param lang [String] The language to create the dictionary for
114
+ # @return [Symbol, nil] The language symbol if successful, nil otherwise
115
+ def create_user_dictionary(lang = nil)
116
+ return lang.to_sym if File.directory?(File.join(config_dir, 'words',
117
+ lang)) && all_parts_of_speech?(File.join(config_dir, 'words', lang), lang)
118
+
119
+ lang_dir = File.join(config_dir, 'words', lang)
120
+
121
+ FileUtils.mkdir_p(lang_dir) unless File.directory?(lang_dir)
122
+ SPEECH_PARTS.each do |part|
123
+ source_file = File.join(builtin_lang_dir('english'), "#{part}.txt")
124
+
125
+ target_file = File.join(lang_dir, "#{part}.txt")
126
+ unless File.exist?(target_file)
127
+ FileUtils.cp(source_file, target_file)
128
+ warn "Created #{part}.txt"
129
+ end
130
+ end
131
+
132
+ # Copy numbers.yml file from the builtin directory
133
+ source_file = File.join(builtin_lang_dir('english'), 'numbers.yml')
134
+ target_file = File.join(lang_dir, 'numbers.yml')
135
+ unless File.exist?(target_file)
136
+ FileUtils.cp(source_file, target_file)
137
+ warn 'Created numbers.yml'
138
+ end
139
+
140
+ # Create the config.yml file if it doesn't exist
141
+ target_file = File.join(lang_dir, 'config.yml')
142
+
143
+ unless File.exist?(target_file)
144
+ config = {
145
+ 'name' => lang,
146
+ 'triggers' => [lang],
147
+ 'description' => "User dictionary for #{lang}"
148
+ }
149
+ File.write(target_file, config.to_yaml)
150
+ warn "Created #{target_file}"
151
+ end
152
+
153
+ unless all_parts_of_speech?(lang_dir, lang) || (RandomWords.testing && !RandomWords.tested.include?('create_user_dictionary'))
154
+ return
155
+ end
156
+
157
+ RandomWords.tested << 'create_user_dictionary'
158
+ warn "Created #{lang} in #{lang_dir}"
159
+ lang.to_sym
160
+ end
161
+
162
+ # List all sources available, builtin and custom
163
+ # @return [Hash] A hash of source names and their corresponding RandomWords::Source objects
164
+ def sources
165
+ return @sources if @sources
166
+
167
+ @sources = {}
168
+ Dir[File.join(__dir__, 'words', '*')].each do |dir|
169
+ next unless File.directory?(dir)
170
+
171
+ name = File.basename(dir)
172
+ @sources[name] = RandomWords::Source.new(name, dir)
173
+ end
174
+ Dir[File.join(config_dir, 'words', '*')].each do |dir|
175
+ next unless File.directory?(dir)
176
+
177
+ name = File.basename(dir)
178
+ @sources[name] = RandomWords::Source.new(name, dir)
179
+ end
180
+ @sources
181
+ end
182
+
183
+ # The base configuration
184
+ def config
185
+ @config ||= base_config
186
+ end
187
+
188
+ # private
189
+
190
+ # The user configuration directory path
191
+ # @return [String] The path to the config directory
192
+ def config_dir
193
+ @config_dir ||= File.expand_path(File.join(Dir.home, '.config', 'random-words'))
194
+ end
195
+
196
+ # Set the user configuration directory path
197
+ # @param dir [String] The path to the config directory
198
+ # @return [String] The path to the config directory
199
+ def config_dir=(dir)
200
+ @config_dir = File.expand_path(dir)
201
+ end
202
+
203
+ # Convert a config file's options to regular config
204
+ # @param configuration [Hash] The configuration hash
205
+ # @return [Hash] The converted configuration hash
206
+ def handle_config(configuration)
207
+ {
208
+ source: configuration[:source].to_source || :latin,
209
+ sentence_length: configuration[:length].to_length || :medium,
210
+ paragraph_length: configuration[:paragraph_length].to_i || 5
211
+ }
212
+ end
213
+
214
+ # Return base configuration
215
+ # @return [Hash] The base configuration
216
+ def base_config
217
+ config_file = File.join(config_dir, 'config.yml')
218
+ if File.exist?(config_file)
219
+ config = YAML.load_file(config_file).symbolize_keys
220
+ return handle_config(config)
221
+ end
222
+ # If the config file doesn't exist, create it
223
+ # and return the default configuration
224
+ create_base_config(config_file)
225
+ config = YAML.load_file(config_file).symbolize_keys
226
+ handle_config(config)
227
+ end
228
+
229
+ # Look for a config.yml file in the config directory
230
+ # @return [String, nil] The path to the config.yml file or nil if not found
231
+ def config_file
232
+ config_file = File.join(config_dir, 'config.yml')
233
+ return config_file if File.exist?(config_file)
234
+
235
+ create_base_config(config_file)
236
+ config_file
237
+ end
238
+
239
+ # Create a base config.yml file if it doesn't exist
240
+ # @param config_file [String] The path to the config.yml file
241
+ # @return [String] The path to the config.yml file
242
+ def create_base_config(config_file)
243
+ FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
244
+ config = {
245
+ 'source' => 'latin',
246
+ 'length' => 'medium',
247
+ 'paragraph_length' => 5
248
+ }
249
+ File.write(config_file, config.to_yaml)
250
+ warn "Created #{config_file}"
251
+ config_file
252
+ end
253
+ end
254
+ end