random-words 1.0.6 → 1.0.8
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.
- checksums.yaml +4 -4
- data/.rspec +3 -3
- data/.rubocop.yml +4 -1
- data/.rubocop_todo.yml +313 -0
- data/.vscode/launch.json +26 -0
- data/CHANGELOG.md +34 -1
- data/Gemfile +15 -12
- data/Gemfile.lock +44 -27
- data/README.md +81 -37
- data/bin/randw +109 -89
- data/lib/random-words/array.rb +27 -7
- data/lib/random-words/config.rb +29 -23
- data/lib/random-words/generator.rb +42 -25
- data/lib/random-words/hash.rb +1 -1
- data/lib/random-words/html2markdown.rb +7 -7
- data/lib/random-words/lorem-markdown.rb +19 -20
- data/lib/random-words/number-to-word.rb +23 -21
- data/lib/random-words/source.rb +10 -9
- data/lib/random-words/string.rb +26 -21
- data/lib/random-words/table-cleanup.rb +3 -3
- data/lib/random-words/version.rb +1 -1
- data/lib/random-words/words/1984/names.txt +10 -72
- data/lib/random-words/words/1984/phrases.txt +16 -0
- data/lib/random-words/words/alice/names.txt +10 -74
- data/lib/random-words/words/alice/phrases.txt +16 -0
- data/lib/random-words/words/bacon/names.txt +53 -73
- data/lib/random-words/words/bacon/phrases.txt +20 -0
- data/lib/random-words/words/corporate/phrases.txt +29 -0
- data/lib/random-words/words/doctor/names.txt +18 -18
- data/lib/random-words/words/doctor/phrases.txt +29 -0
- data/lib/random-words/words/english/phrases.txt +29 -0
- data/lib/random-words/words/foulmouth/articles-plural.txt +3 -1
- data/lib/random-words/words/foulmouth/articles-singular.txt +2 -1
- data/lib/random-words/words/foulmouth/config.yml +1 -1
- data/lib/random-words/words/foulmouth/names.txt +80 -73
- data/lib/random-words/words/foulmouth/phrases.txt +30 -0
- data/lib/random-words/words/hipster/names.txt +72 -73
- data/lib/random-words/words/hipster/phrases.txt +21 -0
- data/lib/random-words/words/latin/names.txt +92 -73
- data/lib/random-words/words/latin/phrases.txt +16 -0
- data/lib/random-words/words/spanish/names.txt +55 -68
- data/lib/random-words/words/spanish/phrases.txt +31 -0
- data/lib/random-words/words/veggie/names.txt +92 -73
- data/lib/random-words/words/veggie/phrases.txt +20 -0
- data/lib/random-words.rb +3 -3
- data/random-words.gemspec +1 -1
- data/src/_README.md +81 -37
- metadata +16 -5
- data/.rspec_status +0 -148
data/lib/random-words/config.rb
CHANGED
@@ -48,7 +48,6 @@ module RandomWords
|
|
48
48
|
rw_source = RandomWords::Source.new(@lang, @source_dir)
|
49
49
|
|
50
50
|
@dictionary = rw_source.dictionary
|
51
|
-
|
52
51
|
end
|
53
52
|
|
54
53
|
# Tests if a uer dictionary exists
|
@@ -56,7 +55,9 @@ module RandomWords
|
|
56
55
|
# @raise [RuntimeError] if the user dictionary is incomplete
|
57
56
|
def user_dictionary_exist?
|
58
57
|
if user_lang_dir
|
59
|
-
raise "User dictionary for #{@lang} is incomplete. Please run create_user_dictionary." unless all_parts_of_speech?(
|
58
|
+
raise "User dictionary for #{@lang} is incomplete. Please run create_user_dictionary." unless all_parts_of_speech?(
|
59
|
+
user_lang_dir, @lang
|
60
|
+
)
|
60
61
|
|
61
62
|
true
|
62
63
|
else
|
@@ -93,11 +94,11 @@ module RandomWords
|
|
93
94
|
dir ||= @source_dir
|
94
95
|
exists = true
|
95
96
|
SPEECH_PARTS.each do |part|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
101
102
|
end
|
102
103
|
CONFIG_FILES.each do |file|
|
103
104
|
unless File.exist?(File.join(dir, "#{file}.yml"))
|
@@ -112,7 +113,8 @@ module RandomWords
|
|
112
113
|
# @param lang [String] The language to create the dictionary for
|
113
114
|
# @return [Symbol, nil] The language symbol if successful, nil otherwise
|
114
115
|
def create_user_dictionary(lang = nil)
|
115
|
-
return lang.to_sym if File.directory?(File.join(config_dir, 'words',
|
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)
|
116
118
|
|
117
119
|
lang_dir = File.join(config_dir, 'words', lang)
|
118
120
|
|
@@ -132,27 +134,29 @@ module RandomWords
|
|
132
134
|
target_file = File.join(lang_dir, 'numbers.yml')
|
133
135
|
unless File.exist?(target_file)
|
134
136
|
FileUtils.cp(source_file, target_file)
|
135
|
-
warn
|
137
|
+
warn 'Created numbers.yml'
|
136
138
|
end
|
137
139
|
|
138
140
|
# Create the config.yml file if it doesn't exist
|
139
|
-
target_file = File.join(lang_dir,
|
141
|
+
target_file = File.join(lang_dir, 'config.yml')
|
140
142
|
|
141
143
|
unless File.exist?(target_file)
|
142
144
|
config = {
|
143
|
-
|
144
|
-
|
145
|
-
|
145
|
+
'name' => lang,
|
146
|
+
'triggers' => [lang],
|
147
|
+
'description' => "User dictionary for #{lang}"
|
146
148
|
}
|
147
149
|
File.write(target_file, config.to_yaml)
|
148
150
|
warn "Created #{target_file}"
|
149
151
|
end
|
150
152
|
|
151
|
-
|
152
|
-
|
153
|
-
warn "Created #{lang} in #{lang_dir}"
|
154
|
-
lang.to_sym
|
153
|
+
unless all_parts_of_speech?(lang_dir, lang) || (RandomWords.testing && !RandomWords.tested.include?('create_user_dictionary'))
|
154
|
+
return
|
155
155
|
end
|
156
|
+
|
157
|
+
RandomWords.tested << 'create_user_dictionary'
|
158
|
+
warn "Created #{lang} in #{lang_dir}"
|
159
|
+
lang.to_sym
|
156
160
|
end
|
157
161
|
|
158
162
|
# List all sources available, builtin and custom
|
@@ -161,13 +165,15 @@ module RandomWords
|
|
161
165
|
return @sources if @sources
|
162
166
|
|
163
167
|
@sources = {}
|
164
|
-
Dir[File.join(__dir__,
|
168
|
+
Dir[File.join(__dir__, 'words', '*')].each do |dir|
|
165
169
|
next unless File.directory?(dir)
|
170
|
+
|
166
171
|
name = File.basename(dir)
|
167
172
|
@sources[name] = RandomWords::Source.new(name, dir)
|
168
173
|
end
|
169
|
-
Dir[File.join(config_dir,
|
174
|
+
Dir[File.join(config_dir, 'words', '*')].each do |dir|
|
170
175
|
next unless File.directory?(dir)
|
176
|
+
|
171
177
|
name = File.basename(dir)
|
172
178
|
@sources[name] = RandomWords::Source.new(name, dir)
|
173
179
|
end
|
@@ -201,7 +207,7 @@ module RandomWords
|
|
201
207
|
{
|
202
208
|
source: configuration[:source].to_source || :latin,
|
203
209
|
sentence_length: configuration[:length].to_length || :medium,
|
204
|
-
paragraph_length: configuration[:paragraph_length].to_i || 5
|
210
|
+
paragraph_length: configuration[:paragraph_length].to_i || 5
|
205
211
|
}
|
206
212
|
end
|
207
213
|
|
@@ -236,9 +242,9 @@ module RandomWords
|
|
236
242
|
def create_base_config(config_file)
|
237
243
|
FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
|
238
244
|
config = {
|
239
|
-
|
240
|
-
|
241
|
-
|
245
|
+
'source' => 'latin',
|
246
|
+
'length' => 'medium',
|
247
|
+
'paragraph_length' => 5
|
242
248
|
}
|
243
249
|
File.write(config_file, config.to_yaml)
|
244
250
|
warn "Created #{config_file}"
|
@@ -9,8 +9,8 @@ module RandomWords
|
|
9
9
|
class Generator
|
10
10
|
# @return [Array<String>] arrays of elements of speech
|
11
11
|
attr_reader :nouns, :verbs, :passive_verbs, :adverbs, :adjectives, :articles, :clauses, :subordinate_conjunctions,
|
12
|
-
|
13
|
-
|
12
|
+
:terminators, :numbers, :plural_nouns, :plural_verbs, :plural_articles, :prepositions, :coordinating_conjunctions,
|
13
|
+
:all_words, :extended_punctuation, :phrases, :names
|
14
14
|
|
15
15
|
# Whether to use extended punctuation
|
16
16
|
# @return [Boolean] true if extended punctuation is used, false otherwise
|
@@ -31,7 +31,7 @@ module RandomWords
|
|
31
31
|
# Define the default sentence parts
|
32
32
|
# These parts will be used to generate random sentences and character strings
|
33
33
|
SENTENCE_PARTS = %w[random_article random_adjective random_noun random_adverb random_verb random_adjective
|
34
|
-
random_verb random_adverb].freeze
|
34
|
+
random_verb random_adverb random_phrase].freeze
|
35
35
|
|
36
36
|
# Initialize the generator with a source and options
|
37
37
|
# @param source [Symbol] The source of the words (e.g., :english)
|
@@ -63,7 +63,8 @@ module RandomWords
|
|
63
63
|
@numbers = @config.dictionary[:numbers]
|
64
64
|
@sources = @config.sources
|
65
65
|
@terminators = @config.dictionary[:terminators]
|
66
|
-
@names = [@config.dictionary[:first_names], @config.dictionary[:last_names]]
|
66
|
+
@names = [@config.dictionary[:first_names], @config.dictionary[:last_names], @config.dictionary[:full_names]]
|
67
|
+
@phrases = @config.dictionary[:phrases]
|
67
68
|
@all_words = @config.dictionary[:all_words]
|
68
69
|
|
69
70
|
@options = {
|
@@ -77,9 +78,7 @@ module RandomWords
|
|
77
78
|
@paragraph_length = @options[:paragraph_length]
|
78
79
|
@use_extended_punctuation = @options[:use_extended_punctuation]
|
79
80
|
|
80
|
-
if @use_extended_punctuation
|
81
|
-
@terminators.concat(@config.dictionary[:extended_punctuation])
|
82
|
-
end
|
81
|
+
@terminators.concat(@config.dictionary[:extended_punctuation]) if @use_extended_punctuation
|
83
82
|
lengths
|
84
83
|
end
|
85
84
|
|
@@ -162,6 +161,7 @@ module RandomWords
|
|
162
161
|
res << random_subordinate_conjunction
|
163
162
|
res << random_coordinating_conjunction
|
164
163
|
res << random_number_with_plural
|
164
|
+
res << random_phrase
|
165
165
|
res << random_conjunction
|
166
166
|
res << random_passive_verb
|
167
167
|
res << random_plural_noun
|
@@ -292,7 +292,7 @@ module RandomWords
|
|
292
292
|
new_sentence = generate_sentence(length / 2)
|
293
293
|
|
294
294
|
# Combine the sentences with random conjunctions
|
295
|
-
sentence = "#{sentence.strip.no_term}, #{random_coordinating_conjunction} #{new_sentence}"
|
295
|
+
sentence = "#{sentence.strip.no_term(terminators)}, #{random_coordinating_conjunction} #{new_sentence.no_term(terminators)}"
|
296
296
|
end
|
297
297
|
|
298
298
|
sentence.to_sent(random_terminator).fix_caps(terminators)
|
@@ -325,6 +325,7 @@ module RandomWords
|
|
325
325
|
code_langs[Random.rand(code_langs.count)]
|
326
326
|
end
|
327
327
|
|
328
|
+
# rubocop:disable Layout/LineLength
|
328
329
|
# Return random code snippet
|
329
330
|
# @param lang [Symbol] The language of the code snippet
|
330
331
|
# @return [String] A randomly generated code snippet
|
@@ -332,23 +333,24 @@ module RandomWords
|
|
332
333
|
code_snippets = {
|
333
334
|
python: %(def hello_world():\n print("Hello, World!")),
|
334
335
|
ruby: %(def hello_world\n puts "Hello, World!"\nend),
|
335
|
-
swift: %(func helloWorld() {\n print(
|
336
|
-
javascript: %(function helloWorld() {\n console.log(
|
336
|
+
swift: %(func helloWorld() {\n print("Hello, World!")\n}),
|
337
|
+
javascript: %(function helloWorld() {\n console.log("Hello, World!");\n}),
|
337
338
|
css: %(body {\n background-color: #f0f0f0;\n font-family: Arial, sans-serif;\n}\nh1 {\n color: #333;\n}),
|
338
339
|
rust: %(fn main() {\n println!("Hello, World!");\n}),
|
339
|
-
go: %(package main\nimport
|
340
|
+
go: %(package main\nimport "fmt"\nfunc main() {\n fmt.Println("Hello, World!")\n}),
|
340
341
|
java: %(public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n})
|
341
342
|
}
|
342
343
|
lang ||= code_lang
|
343
344
|
code_snippets[lang.to_sym]
|
344
345
|
end
|
346
|
+
# rubocop:enable Layout/LineLength
|
345
347
|
|
346
348
|
# Generate random markdown
|
347
349
|
# @param settings [Hash] Settings for generating markdown
|
348
350
|
# @return [String] A randomly generated markdown string
|
349
351
|
def markdown(settings = {})
|
350
352
|
input = RandomWords::LoremMarkdown.new(settings).output
|
351
|
-
RandomWords::HTML2Markdown.new(input)
|
353
|
+
RandomWords::HTML2Markdown.new(input).markdown
|
352
354
|
end
|
353
355
|
|
354
356
|
# Generate random HTML
|
@@ -391,7 +393,7 @@ module RandomWords
|
|
391
393
|
return characters(config.min, config.max, whole_words: config.whole_words, whitespace: config.whitespace,
|
392
394
|
dead_switch: config.dead_switch + 1)
|
393
395
|
end
|
394
|
-
|
396
|
+
|
395
397
|
truncated = config.new_result.compress[0...config.max]
|
396
398
|
truncated.compress.length == config.max ? truncated.compress : nil
|
397
399
|
end
|
@@ -410,7 +412,7 @@ module RandomWords
|
|
410
412
|
sentence_components = []
|
411
413
|
|
412
414
|
# Randomly decide if we include a plural noun with a number
|
413
|
-
sentence_components << random_number_with_plural if roll(10) # 10% chance to include a plural noun
|
415
|
+
# sentence_components << random_number_with_plural if roll(10) # 10% chance to include a plural noun
|
414
416
|
|
415
417
|
# Construct main clause
|
416
418
|
sentence_components << generate_main_clause
|
@@ -424,13 +426,14 @@ module RandomWords
|
|
424
426
|
sentence_components.concat(additional_clauses)
|
425
427
|
sentence_components.map!(&:strip)
|
426
428
|
break if sentence_components.join(' ').length >= length
|
429
|
+
|
427
430
|
conjunction = if roll(50) || (RandomWords.testing && !RandomWords.tested.include?('subordinate_conjunction'))
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
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
|
434
437
|
# sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
|
435
438
|
sentence_components << conjunction unless conjunction.empty?
|
436
439
|
end
|
@@ -503,6 +506,12 @@ module RandomWords
|
|
503
506
|
end
|
504
507
|
end
|
505
508
|
|
509
|
+
# Generate a random phrase
|
510
|
+
# @return [String] A randomly selected phrase
|
511
|
+
def random_phrase
|
512
|
+
phrases.sample
|
513
|
+
end
|
514
|
+
|
506
515
|
# Generate a random noun
|
507
516
|
# @return [String] A randomly selected noun
|
508
517
|
def random_noun
|
@@ -625,9 +634,14 @@ module RandomWords
|
|
625
634
|
end
|
626
635
|
|
627
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
|
+
|
628
641
|
first_name = @names[0].sample
|
642
|
+
middle_initial = roll(20) ? " #{('A'..'Z').to_a.sample}" : ''
|
629
643
|
last_name = @names[1].sample
|
630
|
-
"#{first_name} #{last_name}"
|
644
|
+
"#{first_name}#{middle_initial} #{last_name}"
|
631
645
|
end
|
632
646
|
|
633
647
|
# Generate a random main clause
|
@@ -639,16 +653,19 @@ module RandomWords
|
|
639
653
|
# @example
|
640
654
|
# generate_main_clause # Returns a random main clause
|
641
655
|
def generate_main_clause
|
642
|
-
|
643
|
-
|
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
|
644
661
|
"#{random_number_with_plural} #{random_adverb} #{random_plural_verb}"
|
645
662
|
else
|
646
663
|
noun = random_noun
|
647
664
|
adjective = random_adjective
|
648
665
|
"#{random_article_for_word(adjective)} #{adjective} #{noun} #{random_adverb} #{random_verb}"
|
649
666
|
end
|
650
|
-
tail = roll(50) ? " #{random_prepositional_phrase}" :
|
651
|
-
tail += roll(10) ? ", #{random_clause}" :
|
667
|
+
tail = roll(50) ? " #{random_prepositional_phrase}" : ''
|
668
|
+
tail += roll(10) ? ", #{random_clause}" : ''
|
652
669
|
"#{beginning.strip.sub(/,?$/, ', ')}#{tail}"
|
653
670
|
end
|
654
671
|
|
data/lib/random-words/hash.rb
CHANGED
@@ -80,7 +80,7 @@ module RandomWords
|
|
80
80
|
line = []
|
81
81
|
end
|
82
82
|
end
|
83
|
-
out << line.join(' ') + (str[-1..-1] =~ /[ \t\n]/ ? str[-1..-1] : '')
|
83
|
+
out << (line.join(' ') + (str[-1..-1] =~ /[ \t\n]/ ? str[-1..-1] : ''))
|
84
84
|
out.join("\n")
|
85
85
|
end
|
86
86
|
|
@@ -101,12 +101,12 @@ module RandomWords
|
|
101
101
|
@section_level -= 1
|
102
102
|
o
|
103
103
|
when /h(\d+)/
|
104
|
-
"\n\n" + ('#' * (::Regexp.last_match(1).to_i + @section_level) + ' ' + output_for_children(node)) + "\n\n"
|
104
|
+
"\n\n" + (('#' * (::Regexp.last_match(1).to_i + @section_level)) + ' ' + output_for_children(node)) + "\n\n"
|
105
105
|
when /mark/
|
106
106
|
"==#{output_for_children(node)}=="
|
107
107
|
when 'blockquote'
|
108
108
|
@section_level += 1
|
109
|
-
o = "\n\n> #{output_for_children(node).lstrip.gsub(
|
109
|
+
o = "\n\n> #{output_for_children(node).lstrip.gsub("\n", "\n> ")}\n\n".gsub(/> \n(> \n)+/, "> \n")
|
110
110
|
@section_level -= 1
|
111
111
|
o
|
112
112
|
when 'cite'
|
@@ -130,7 +130,7 @@ module RandomWords
|
|
130
130
|
@in_pre = false
|
131
131
|
node.text.strip
|
132
132
|
else
|
133
|
-
"`#{output_for_children(node).gsub(
|
133
|
+
"`#{output_for_children(node).gsub("\n", ' ')}`"
|
134
134
|
end
|
135
135
|
when 'pre'
|
136
136
|
@in_pre = true
|
@@ -185,7 +185,7 @@ module RandomWords
|
|
185
185
|
%w[th td].include?(c.name)
|
186
186
|
end.map do |c|
|
187
187
|
output_for(c)
|
188
|
-
end.join.gsub(
|
188
|
+
end.join.gsub('||', '|') + header
|
189
189
|
when 'th', 'td'
|
190
190
|
"| #{output_for_children(node)} |"
|
191
191
|
when 'text'
|
@@ -193,7 +193,7 @@ module RandomWords
|
|
193
193
|
if (c = node.content.force_encoding(@encoding)) =~ /\S/
|
194
194
|
c.gsub!(/\n\n+/, '<$PreserveDouble$>')
|
195
195
|
c.gsub!(/\s+/, ' ')
|
196
|
-
c.gsub(
|
196
|
+
c.gsub('<$PreserveDouble$>', "\n\n")
|
197
197
|
else
|
198
198
|
c
|
199
199
|
end
|
@@ -202,4 +202,4 @@ module RandomWords
|
|
202
202
|
end
|
203
203
|
end
|
204
204
|
end
|
205
|
-
end
|
205
|
+
end
|
@@ -41,9 +41,9 @@ module RandomWords
|
|
41
41
|
@options = defaults.merge(options)
|
42
42
|
|
43
43
|
@generator = RandomWords::Generator.new(@options[:source], {
|
44
|
-
|
45
|
-
|
46
|
-
|
44
|
+
sentence_length: @options[:length],
|
45
|
+
paragraph_length: @options[:sentences]
|
46
|
+
})
|
47
47
|
|
48
48
|
@output = ''
|
49
49
|
strong = @options[:decorate]
|
@@ -163,13 +163,13 @@ module RandomWords
|
|
163
163
|
newline = nested ? '' : "\n\n"
|
164
164
|
quote = "#{newline}<blockquote>\n"
|
165
165
|
quote += paragraph(level1,
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
166
|
+
em: @options[:decorate],
|
167
|
+
strong: @options[:decorate],
|
168
|
+
links: @options[:link],
|
169
|
+
code: @options[:code],
|
170
|
+
mark: @options[:mark]).gsub(
|
171
|
+
/\n+/, "\n"
|
172
|
+
)
|
173
173
|
quote += blockquote(level2, true).strip if level2.positive?
|
174
174
|
"#{quote}#{cite}</blockquote>#{newline}"
|
175
175
|
end
|
@@ -200,7 +200,7 @@ module RandomWords
|
|
200
200
|
else
|
201
201
|
6
|
202
202
|
end
|
203
|
-
grafs = @output.split(
|
203
|
+
grafs = @output.split("\n\n").reverse
|
204
204
|
|
205
205
|
@output = header(1)
|
206
206
|
@output += "#{grafs.slice!(-1)}\n\n"
|
@@ -284,13 +284,13 @@ module RandomWords
|
|
284
284
|
# @param [Integer] count The number of blocks to inject.
|
285
285
|
# @param [Proc] block The block whose result to inject.
|
286
286
|
def inject_block(count, block)
|
287
|
-
grafs = @output.split(
|
287
|
+
grafs = @output.split("\n\n")
|
288
288
|
|
289
|
-
if grafs.length < 2
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
289
|
+
len = if grafs.length < 2
|
290
|
+
1
|
291
|
+
else
|
292
|
+
Random.rand(1..grafs.length / 2).to_i
|
293
|
+
end
|
294
294
|
|
295
295
|
@output = "#{grafs.slice!(0, len).join("\n\n")}\n\n"
|
296
296
|
|
@@ -345,7 +345,6 @@ module RandomWords
|
|
345
345
|
# @param [Array] force An array of tags to force.
|
346
346
|
# @return [String] The generated paragraph.
|
347
347
|
def paragraph(count, em: false, strong: false, links: false, code: false, mark: false, force: [])
|
348
|
-
|
349
348
|
output = ''
|
350
349
|
s = { short: 2, medium: 4, long: 6, very_long: 8 }[@options[:length]]
|
351
350
|
count.times do
|
@@ -386,7 +385,7 @@ module RandomWords
|
|
386
385
|
# @param [Integer] max The maximum number of words to generate.
|
387
386
|
# @return [String] The generated fragment.
|
388
387
|
def fragment(min, max)
|
389
|
-
@generator.words(Random.rand(min..max)).no_term.downcase_first
|
388
|
+
@generator.words(Random.rand(min..max)).no_term(generator.terminators).downcase_first
|
390
389
|
end
|
391
390
|
|
392
391
|
# Generates a random string of characters.
|
@@ -395,4 +394,4 @@ module RandomWords
|
|
395
394
|
@generator.characters(min, max, whole_words: false, whitespace: false).downcase
|
396
395
|
end
|
397
396
|
end
|
398
|
-
end
|
397
|
+
end
|
@@ -35,15 +35,14 @@ module RandomWords
|
|
35
35
|
tmp = self / 1000
|
36
36
|
final = (self % 1000).hundred_to_word(2, numbers)
|
37
37
|
place = 3 # special-case the tens and below
|
38
|
-
until tmp.zero?
|
39
|
-
final = (tmp%1000).hundred_to_word(place, numbers) + ' ' + final
|
38
|
+
until tmp.zero?
|
39
|
+
final = (tmp % 1000).hundred_to_word(place, numbers) + ' ' + final
|
40
40
|
place += 1
|
41
|
-
tmp
|
41
|
+
tmp /= 1000
|
42
42
|
end
|
43
|
-
final == '' ? 'zero' : final.sub(/\s+$/,'')
|
43
|
+
final == '' ? 'zero' : final.sub(/\s+$/, '')
|
44
44
|
end
|
45
45
|
|
46
|
-
|
47
46
|
# For testing edge cases
|
48
47
|
def additional_tests(numbers)
|
49
48
|
teen_to_word(numbers)
|
@@ -56,20 +55,22 @@ module RandomWords
|
|
56
55
|
|
57
56
|
# Convert a number to its word representation for the hundreds place.
|
58
57
|
def hundred_to_word(place = 0, numbers)
|
59
|
-
if
|
58
|
+
if zero?
|
60
59
|
''
|
61
60
|
elsif self < 10
|
62
|
-
|
61
|
+
append_place(digit_to_word(numbers), place, numbers)
|
63
62
|
elsif self < 20
|
64
|
-
|
63
|
+
append_place(teen_to_word(numbers), place, numbers)
|
65
64
|
elsif self < 100
|
66
|
-
|
65
|
+
append_place(tens_place_to_word(numbers), place, numbers)
|
67
66
|
else
|
68
|
-
|
67
|
+
hundreds = self / 100
|
68
|
+
tens = self % 100
|
69
69
|
if tens.zero?
|
70
|
-
|
70
|
+
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]}", place, numbers)
|
71
71
|
else
|
72
|
-
|
72
|
+
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]} " + tens.tens_place_to_word(numbers), place,
|
73
|
+
numbers)
|
73
74
|
end
|
74
75
|
end
|
75
76
|
end
|
@@ -96,11 +97,12 @@ module RandomWords
|
|
96
97
|
# 45.tens_place_to_word(numbers) # => "forty five"
|
97
98
|
def tens_place_to_word(numbers)
|
98
99
|
if self > 19
|
99
|
-
|
100
|
-
|
101
|
-
ten
|
100
|
+
tens = self / 10
|
101
|
+
ones = self % 10
|
102
|
+
ten = numbers[:tens][tens - 2]
|
103
|
+
ten + (ones.zero? ? '' : ' ' + ones.digit_to_word(numbers))
|
102
104
|
else
|
103
|
-
|
105
|
+
teen_to_word(numbers)
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
@@ -112,11 +114,11 @@ module RandomWords
|
|
112
114
|
# 12.teen_to_word(numbers) # => "twelve"
|
113
115
|
def teen_to_word(numbers)
|
114
116
|
if self < 10
|
115
|
-
|
117
|
+
digit_to_word(numbers)
|
116
118
|
elsif self < 20
|
117
|
-
numbers[:teens][self-10]
|
119
|
+
numbers[:teens][self - 10]
|
118
120
|
else
|
119
|
-
|
121
|
+
tens_place_to_word(numbers)
|
120
122
|
end
|
121
123
|
end
|
122
124
|
|
@@ -127,11 +129,11 @@ module RandomWords
|
|
127
129
|
# @example
|
128
130
|
# 5.digit_to_word(numbers) # => "five"
|
129
131
|
def digit_to_word(numbers)
|
130
|
-
if
|
132
|
+
if zero?
|
131
133
|
''
|
132
134
|
else
|
133
135
|
numbers[:digits][self]
|
134
136
|
end
|
135
137
|
end
|
136
138
|
end
|
137
|
-
end
|
139
|
+
end
|
data/lib/random-words/source.rb
CHANGED
@@ -18,8 +18,8 @@ module RandomWords
|
|
18
18
|
@description = @config['description'] || 'No description available'
|
19
19
|
@dictionary = from_files
|
20
20
|
@dictionary[:all_words] = @dictionary.values.flatten.uniq
|
21
|
-
@dictionary[:terminators], @dictionary[:extended_punctuation] = from_file(
|
22
|
-
@dictionary[:first_names], @dictionary[:last_names] = from_file(
|
21
|
+
@dictionary[:terminators], @dictionary[:extended_punctuation] = from_file('terminators').split_terminators
|
22
|
+
@dictionary[:first_names], @dictionary[:last_names], @dictionary[:full_names] = from_file('names').split_names
|
23
23
|
rescue StandardError
|
24
24
|
@name = name.to_sym
|
25
25
|
@config = {}
|
@@ -27,8 +27,8 @@ module RandomWords
|
|
27
27
|
@description = 'No description available'
|
28
28
|
@dictionary = from_files
|
29
29
|
@dictionary[:all_words] = @dictionary.values.flatten.uniq
|
30
|
-
@dictionary[:terminators], @dictionary[:extended_punctuation] = from_file(
|
31
|
-
@dictionary[:first_names], @dictionary[:last_names] = from_file(
|
30
|
+
@dictionary[:terminators], @dictionary[:extended_punctuation] = from_file('terminators').split_terminators
|
31
|
+
@dictionary[:first_names], @dictionary[:last_names], @dictionary[:full_names] = from_file('names').split_names
|
32
32
|
end
|
33
33
|
|
34
34
|
# def to_sym
|
@@ -62,7 +62,8 @@ module RandomWords
|
|
62
62
|
clauses: from_file('clauses'),
|
63
63
|
coordinating_conjunctions: from_file('conjunctions-coordinating'),
|
64
64
|
subordinate_conjunctions: from_file('conjunctions-subordinate'),
|
65
|
-
numbers: from_yaml('numbers')
|
65
|
+
numbers: from_yaml('numbers'),
|
66
|
+
phrases: from_file('phrases')
|
66
67
|
}
|
67
68
|
end
|
68
69
|
|
@@ -77,7 +78,7 @@ module RandomWords
|
|
77
78
|
filename = "#{filename.sub(/\.txt$/, '')}.txt"
|
78
79
|
path = File.join(@path, filename)
|
79
80
|
|
80
|
-
File.read(path).split("\n").map(&:strip) # Changed from split_lines to split("\n")
|
81
|
+
File.read(path).strip.split("\n").map(&:strip) # Changed from split_lines to split("\n")
|
81
82
|
rescue Errno::ENOENT
|
82
83
|
warn "File not found: #{path}"
|
83
84
|
[]
|
@@ -89,17 +90,17 @@ module RandomWords
|
|
89
90
|
def from_yaml(filename)
|
90
91
|
path = File.join(@path, "#{filename}.yml")
|
91
92
|
return {} unless File.exist?(path)
|
93
|
+
|
92
94
|
begin
|
93
95
|
nums = YAML.safe_load(File.read(path), aliases: true).symbolize_keys
|
94
96
|
nums.keys.each do |key|
|
95
|
-
nums[key] = nums[key].split(
|
97
|
+
nums[key] = nums[key].split(' ').map(&:strip) if nums[key].is_a?(String)
|
96
98
|
end
|
97
99
|
nums
|
98
100
|
rescue Psych::SyntaxError => e
|
99
101
|
warn "YAML syntax error in #{path}: #{e.message}"
|
100
102
|
{}
|
101
103
|
end
|
102
|
-
|
103
104
|
end
|
104
105
|
end
|
105
|
-
end
|
106
|
+
end
|