random-words 1.0.6 → 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.
- checksums.yaml +4 -4
- data/.rspec_status +118 -100
- data/.rubocop.yml +4 -1
- data/.rubocop_todo.yml +313 -0
- data/CHANGELOG.md +26 -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 +22 -17
- 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 +15 -4
@@ -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
|
-
puts
|
396
|
+
puts 'made it'
|
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
|
data/lib/random-words/string.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
|
+
|
3
4
|
module RandomWords
|
4
5
|
# String extensions for RandomWords
|
5
6
|
# This module extends the String class with additional methods for cleaning,
|
@@ -42,12 +43,10 @@ module RandomWords
|
|
42
43
|
|
43
44
|
letters = split('')
|
44
45
|
string = []
|
45
|
-
while letters[0] !~ /[[:word:]]/
|
46
|
-
string << letters.shift
|
47
|
-
end
|
46
|
+
string << letters.shift while letters[0] !~ /[[:word:]]/
|
48
47
|
string << letters.shift.upcase
|
49
48
|
string.concat(letters)
|
50
|
-
string.join(
|
49
|
+
string.join('')
|
51
50
|
end
|
52
51
|
|
53
52
|
# Downcase the first letter of a string, respecting punctuation.
|
@@ -57,12 +56,10 @@ module RandomWords
|
|
57
56
|
|
58
57
|
letters = split('')
|
59
58
|
string = []
|
60
|
-
while letters[0] !~ /[[:word:]]/
|
61
|
-
string << letters.shift
|
62
|
-
end
|
59
|
+
string << letters.shift while letters[0] !~ /[[:word:]]/
|
63
60
|
string << letters.shift.downcase
|
64
61
|
string.concat(letters)
|
65
|
-
string.join(
|
62
|
+
string.join('')
|
66
63
|
end
|
67
64
|
|
68
65
|
# Capitalize the first letter of each sentence in a string.
|
@@ -75,7 +72,7 @@ module RandomWords
|
|
75
72
|
def fix_caps(terminators)
|
76
73
|
return self if empty?
|
77
74
|
|
78
|
-
terminator_ends = terminators.map { |t| t[1].split(
|
75
|
+
terminator_ends = terminators.map { |t| t[1].split('').last }.delete_if(&:empty?)
|
79
76
|
return capitalize if terminator_ends.empty?
|
80
77
|
|
81
78
|
terminator_regex = Regexp.new("[#{Regexp.escape(terminator_ends.join)}]")
|
@@ -83,7 +80,7 @@ module RandomWords
|
|
83
80
|
|
84
81
|
split(/(?<=#{terminator_regex}) /).map do |sentence|
|
85
82
|
sentence.capitalize
|
86
|
-
end.join(
|
83
|
+
end.join(' ')
|
87
84
|
end
|
88
85
|
|
89
86
|
# Remove duplicate commas
|
@@ -91,7 +88,7 @@ module RandomWords
|
|
91
88
|
# @example
|
92
89
|
# "Hello, , World!".remove_duplicate_commas # => "Hello, World!"
|
93
90
|
def dedup_commas
|
94
|
-
gsub(/(, *)+/, ',').gsub(
|
91
|
+
gsub(/(, *)+/, ',').gsub(',', ', ')
|
95
92
|
end
|
96
93
|
|
97
94
|
# Generate a sentence with capitalization and terminator
|
@@ -128,8 +125,17 @@ module RandomWords
|
|
128
125
|
|
129
126
|
# Remove any punctuation mark from the end of a string.
|
130
127
|
# @return [String] The string with the last punctuation mark removed.
|
131
|
-
def no_term
|
132
|
-
|
128
|
+
def no_term(terminators)
|
129
|
+
str = dup
|
130
|
+
leading = terminators.map { |t| t[0] }.delete_if(&:empty?).join
|
131
|
+
trailing = terminators.map { |t| t[1] }.delete_if(&:empty?).join
|
132
|
+
return self if leading.empty? && trailing.empty?
|
133
|
+
|
134
|
+
str.sub!(/^[#{Regexp.escape(leading)}]/, '') unless leading.empty?
|
135
|
+
|
136
|
+
str.sub!(/[#{Regexp.escape(trailing)}]+$/, '') unless trailing.empty?
|
137
|
+
|
138
|
+
str
|
133
139
|
end
|
134
140
|
|
135
141
|
# Indent every line in a string with a specified string.
|
@@ -181,7 +187,7 @@ module RandomWords
|
|
181
187
|
# @return [String] The string with spaces restored.
|
182
188
|
def restore_spaces
|
183
189
|
# Restores spaces in the output.
|
184
|
-
gsub(
|
190
|
+
gsub('%%', ' ')
|
185
191
|
end
|
186
192
|
|
187
193
|
# Check if the string is trueish (starts with 't', 'y', or '1').
|
@@ -196,7 +202,7 @@ module RandomWords
|
|
196
202
|
new_source = nil
|
197
203
|
sources = RandomWords::Generator.new.sources
|
198
204
|
|
199
|
-
sources.each do |
|
205
|
+
sources.each do |_k, v|
|
200
206
|
v.names.each do |name|
|
201
207
|
next unless name.to_s =~ /^#{self}/i
|
202
208
|
|
@@ -224,7 +230,6 @@ module RandomWords
|
|
224
230
|
else
|
225
231
|
:medium
|
226
232
|
end
|
227
|
-
|
228
233
|
end
|
229
234
|
end
|
230
|
-
end
|
235
|
+
end
|
@@ -125,7 +125,7 @@ module RandomWords
|
|
125
125
|
@alignment.zip(@widths).each do |align, width|
|
126
126
|
@string << ':' if align == :left
|
127
127
|
width = @max_cell_width - 2 if width >= @max_cell_width
|
128
|
-
@string << '-' * (width + (align == :center ? 2 : 1))
|
128
|
+
@string << ('-' * (width + (align == :center ? 2 : 1)))
|
129
129
|
@string << ':' if align == :right
|
130
130
|
@string << '|'
|
131
131
|
end
|
@@ -189,7 +189,7 @@ module RandomWords
|
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
192
|
-
lines = t['table'].split(
|
192
|
+
lines = t['table'].split("\n")
|
193
193
|
lines.delete_if(&:alignment?)
|
194
194
|
|
195
195
|
lines.each do |row|
|
@@ -207,4 +207,4 @@ module RandomWords
|
|
207
207
|
@content
|
208
208
|
end
|
209
209
|
end
|
210
|
-
end
|
210
|
+
end
|
data/lib/random-words/version.rb
CHANGED