random-words 1.0.8 → 1.0.10
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 +0 -1
- data/CHANGELOG.md +30 -0
- data/LICENSE +19 -0
- data/README.md +21 -13
- data/bin/randw +20 -7
- data/lib/random-words/array.rb +9 -0
- data/lib/random-words/boolean.rb +60 -0
- data/lib/random-words/config.rb +8 -2
- data/lib/random-words/generator.rb +89 -46
- data/lib/random-words/lorem-markdown.rb +20 -3
- data/lib/random-words/number-to-word.rb +4 -4
- data/lib/random-words/numeric.rb +13 -0
- data/lib/random-words/string.rb +112 -13
- data/lib/random-words/version.rb +1 -1
- data/lib/random-words/words/1984/clauses.txt +79 -79
- data/lib/random-words/words/1984/terminators.txt +1 -0
- data/lib/random-words/words/alice/clauses.txt +81 -81
- data/lib/random-words/words/alice/terminators.txt +1 -0
- data/lib/random-words/words/bacon/clauses.txt +639 -639
- data/lib/random-words/words/bacon/terminators.txt +1 -0
- data/lib/random-words/words/corporate/clauses.txt +100 -100
- data/lib/random-words/words/doctor/clauses.txt +83 -83
- data/lib/random-words/words/english/clauses.txt +639 -639
- data/lib/random-words/words/english/conjunctions-subordinate.txt +0 -1
- data/lib/random-words/words/english/terminators.txt +1 -0
- data/lib/random-words/words/foulmouth/clauses.txt +58 -58
- data/lib/random-words/words/hipster/clauses.txt +180 -180
- data/lib/random-words/words/hipster/conjunctions-coordinating.txt +1 -1
- data/lib/random-words/words/hipster/phrases.txt +3 -3
- data/lib/random-words/words/spanish/clauses.txt +74 -74
- data/lib/random-words/words/spanish/config.yml +1 -1
- data/lib/random-words/words/spanish/terminators.txt +0 -3
- data/lib/random-words/words/veggie/clauses.txt +57 -57
- data/lib/random-words.rb +2 -0
- data/src/_README.md +25 -13
- metadata +5 -3
- data/Gemfile.lock +0 -131
@@ -12,6 +12,9 @@ module RandomWords
|
|
12
12
|
:terminators, :numbers, :plural_nouns, :plural_verbs, :plural_articles, :prepositions, :coordinating_conjunctions,
|
13
13
|
:all_words, :extended_punctuation, :phrases, :names
|
14
14
|
|
15
|
+
# return [Hash] configuration
|
16
|
+
attr_reader :config
|
17
|
+
|
15
18
|
# Whether to use extended punctuation
|
16
19
|
# @return [Boolean] true if extended punctuation is used, false otherwise
|
17
20
|
attr_reader :use_extended_punctuation
|
@@ -28,6 +31,10 @@ module RandomWords
|
|
28
31
|
# @return [Hash<String, RandomWords::Source>] List of available sources
|
29
32
|
attr_reader :sources
|
30
33
|
|
34
|
+
# Debug mode
|
35
|
+
# @return [Boolean] true if debug mode is enabled, false otherwise
|
36
|
+
attr_accessor :debug
|
37
|
+
|
31
38
|
# Define the default sentence parts
|
32
39
|
# These parts will be used to generate random sentences and character strings
|
33
40
|
SENTENCE_PARTS = %w[random_article random_adjective random_noun random_adverb random_verb random_adjective
|
@@ -43,6 +50,7 @@ module RandomWords
|
|
43
50
|
# generator.sentence_length = :long
|
44
51
|
# generator.paragraph_length = 3
|
45
52
|
def initialize(source = :english, options = {})
|
53
|
+
@debug = options[:debug] || false
|
46
54
|
@tested = []
|
47
55
|
@config = RandomWords::Config.new(source)
|
48
56
|
@source = source
|
@@ -79,9 +87,17 @@ module RandomWords
|
|
79
87
|
@use_extended_punctuation = @options[:use_extended_punctuation]
|
80
88
|
|
81
89
|
@terminators.concat(@config.dictionary[:extended_punctuation]) if @use_extended_punctuation
|
90
|
+
|
82
91
|
lengths
|
83
92
|
end
|
84
93
|
|
94
|
+
# Display debug message
|
95
|
+
def debug(msg)
|
96
|
+
return unless @debug
|
97
|
+
|
98
|
+
"%#{msg}%"
|
99
|
+
end
|
100
|
+
|
85
101
|
# Shortcut for RandomWords::Config.create_user_dictionary
|
86
102
|
# @param title [String] The title of the user dictionary
|
87
103
|
# @return [Symbol] The symbolized name of the dictionary
|
@@ -145,6 +161,7 @@ module RandomWords
|
|
145
161
|
# @!visibility private
|
146
162
|
def test_random
|
147
163
|
RandomWords.testing = true
|
164
|
+
@debug = true
|
148
165
|
@use_extended_punctuation = true
|
149
166
|
res = []
|
150
167
|
res << random_noun
|
@@ -158,11 +175,13 @@ module RandomWords
|
|
158
175
|
res << random_article_for_word('bananas')
|
159
176
|
res << random_plural_article
|
160
177
|
res << random_clause
|
178
|
+
res << random_separator
|
161
179
|
res << random_subordinate_conjunction
|
162
180
|
res << random_coordinating_conjunction
|
163
181
|
res << random_number_with_plural
|
164
182
|
res << random_phrase
|
165
183
|
res << random_conjunction
|
184
|
+
res << random_name
|
166
185
|
res << random_passive_verb
|
167
186
|
res << random_plural_noun
|
168
187
|
res << random_plural_verb
|
@@ -285,7 +304,7 @@ module RandomWords
|
|
285
304
|
def generate_combined_sentence(length = nil)
|
286
305
|
length ||= define_length(@sentence_length)
|
287
306
|
sentence = generate_sentence
|
288
|
-
return sentence.to_sent(random_terminator).fix_caps(terminators) if sentence.length > length
|
307
|
+
return sentence.to_sent(random_terminator).fix_caps(terminators).expand_debug if sentence.length > length
|
289
308
|
|
290
309
|
while sentence.length < length
|
291
310
|
# Generate a random number of sentences to combine
|
@@ -295,7 +314,7 @@ module RandomWords
|
|
295
314
|
sentence = "#{sentence.strip.no_term(terminators)}, #{random_coordinating_conjunction} #{new_sentence.no_term(terminators)}"
|
296
315
|
end
|
297
316
|
|
298
|
-
sentence.to_sent(random_terminator).fix_caps(terminators)
|
317
|
+
sentence.to_sent(random_terminator).fix_caps(terminators).expand_debug
|
299
318
|
end
|
300
319
|
|
301
320
|
# Generate a random paragraph
|
@@ -312,12 +331,6 @@ module RandomWords
|
|
312
331
|
sentences.join(' ').strip.compress
|
313
332
|
end
|
314
333
|
|
315
|
-
# Generate a random name
|
316
|
-
# @return [String] A randomly generated name
|
317
|
-
def name
|
318
|
-
random_name
|
319
|
-
end
|
320
|
-
|
321
334
|
# Generate a random code language
|
322
335
|
# @return [Symbol] A randomly selected code language
|
323
336
|
def code_lang
|
@@ -360,6 +373,12 @@ module RandomWords
|
|
360
373
|
RandomWords::LoremMarkdown.new(settings).output
|
361
374
|
end
|
362
375
|
|
376
|
+
# Generate a random name
|
377
|
+
# @return [String] A randomly generated name
|
378
|
+
def name
|
379
|
+
"#{debug('NAM')}#{random_name}"
|
380
|
+
end
|
381
|
+
|
363
382
|
private
|
364
383
|
|
365
384
|
# Struct for overflow configuration
|
@@ -425,21 +444,25 @@ module RandomWords
|
|
425
444
|
additional_clauses = generate_additional_clauses
|
426
445
|
sentence_components.concat(additional_clauses)
|
427
446
|
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
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
# sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
|
438
|
-
sentence_components << conjunction unless conjunction.empty?
|
447
|
+
# break if sentence_components.join(' ').length >= length
|
448
|
+
|
449
|
+
# conjunction = if roll(50) || (RandomWords.testing && !RandomWords.tested.include?('subordinate_conjunction'))
|
450
|
+
# RandomWords.tested << 'subordinate_conjunction' if RandomWords.testing
|
451
|
+
# random_subordinate_conjunction.strip
|
452
|
+
# else
|
453
|
+
# RandomWords.tested << 'coordinating_conjunction' if RandomWords.testing
|
454
|
+
# random_coordinating_conjunction.strip
|
455
|
+
# end
|
456
|
+
# # sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
|
457
|
+
# sentence_components << conjunction unless conjunction.empty?
|
439
458
|
end
|
440
459
|
|
441
460
|
# Join all parts into a single sentence
|
442
|
-
|
461
|
+
output = sentence_components.shift
|
462
|
+
sentence_components.each do |part|
|
463
|
+
output << " #{roll(50) ? random_coordinating_conjunction : random_subordinate_conjunction} #{part}"
|
464
|
+
end
|
465
|
+
output.strip.compress
|
443
466
|
end
|
444
467
|
|
445
468
|
# Convert a length symbol to a specific length value
|
@@ -486,7 +509,7 @@ module RandomWords
|
|
486
509
|
# @example
|
487
510
|
# random_conjunction # Returns a random conjunction
|
488
511
|
def random_conjunction
|
489
|
-
coordinating_conjunctions.sample
|
512
|
+
"#{debug('COC')}#{coordinating_conjunctions.sample}"
|
490
513
|
end
|
491
514
|
|
492
515
|
# Generate a random number with a plural noun
|
@@ -496,68 +519,72 @@ module RandomWords
|
|
496
519
|
# random_number_with_plural # Returns a string like "three cats"
|
497
520
|
def random_number_with_plural
|
498
521
|
num = rand(1000)
|
499
|
-
number =
|
522
|
+
number = if roll(50)
|
523
|
+
num.to_word(@numbers)
|
524
|
+
else
|
525
|
+
num.to_commas
|
526
|
+
end
|
500
527
|
if num == 1 || (RandomWords.testing && !RandomWords.tested.include?('random_noun'))
|
501
528
|
RandomWords.tested << 'random_noun' if RandomWords.testing
|
502
|
-
"#{number} #{random_noun}"
|
529
|
+
"#{debug('NUM')}#{number} #{random_noun}"
|
503
530
|
else
|
504
531
|
RandomWords.tested << 'random_plural_noun' if RandomWords.testing
|
505
|
-
"#{number} #{random_plural_noun}"
|
532
|
+
"#{debug('NUM')}#{number} #{random_plural_noun}"
|
506
533
|
end
|
507
534
|
end
|
508
535
|
|
509
536
|
# Generate a random phrase
|
510
537
|
# @return [String] A randomly selected phrase
|
511
538
|
def random_phrase
|
512
|
-
phrases.sample
|
539
|
+
"#{debug('PHR')}#{phrases.sample}"
|
513
540
|
end
|
514
541
|
|
515
542
|
# Generate a random noun
|
516
543
|
# @return [String] A randomly selected noun
|
517
544
|
def random_noun
|
518
|
-
nouns.sample
|
545
|
+
"#{debug('NOU')}#{nouns.sample}"
|
519
546
|
end
|
520
547
|
|
521
548
|
# Generate a random plural noun
|
522
549
|
# @return [String] A randomly selected plural noun
|
523
550
|
def random_plural_noun
|
524
|
-
plural_nouns.sample
|
551
|
+
"#{debug('PLN')}#{plural_nouns.sample}"
|
525
552
|
end
|
526
553
|
|
527
554
|
# Generate a random verb
|
528
555
|
# @return [String] A randomly selected verb
|
529
556
|
def random_verb
|
530
|
-
verbs.sample
|
557
|
+
"#{debug('VER')}#{verbs.sample}"
|
531
558
|
end
|
532
559
|
|
533
560
|
# Generate a random plural verb
|
534
561
|
# @return [String] A randomly selected plural verb
|
535
562
|
def random_plural_verb
|
536
|
-
plural_verbs.sample
|
563
|
+
"#{debug('PLV')}#{plural_verbs.sample}"
|
537
564
|
end
|
538
565
|
|
539
566
|
# Generate a random passive verb
|
540
567
|
# @return [String] A randomly selected passive verb
|
541
568
|
def random_passive_verb
|
542
|
-
passive_verbs.sample
|
569
|
+
"#{debug('PAV')}#{passive_verbs.sample}"
|
543
570
|
end
|
544
571
|
|
545
572
|
# Generate a random adverb
|
546
573
|
# @return [String] A randomly selected adverb
|
547
574
|
def random_adverb
|
548
|
-
adverbs.sample
|
575
|
+
"#{debug('ADV')}#{adverbs.sample}"
|
549
576
|
end
|
550
577
|
|
551
578
|
# Generate a random adjective
|
552
579
|
# @return [String] A randomly selected adjective
|
553
580
|
def random_adjective
|
554
|
-
adjectives.sample
|
581
|
+
"#{debug('ADJ')}#{adjectives.sample}"
|
555
582
|
end
|
556
583
|
|
557
584
|
# Generate a random article
|
558
585
|
# @return [String] A randomly selected article
|
559
586
|
def random_article
|
560
|
-
articles.
|
587
|
+
"#{debug('ART')}#{articles.rotate[0]}"
|
561
588
|
end
|
562
589
|
|
563
590
|
# Generate a random article for a noun
|
@@ -574,7 +601,7 @@ module RandomWords
|
|
574
601
|
RandomWords.tested << 'random_article_for_word' if RandomWords.testing
|
575
602
|
if word.start_with?(/[aeiou]/i) && article =~ /^an?$/i
|
576
603
|
article = 'an'
|
577
|
-
elsif
|
604
|
+
elsif /^an$/i.match?(article)
|
578
605
|
article = 'a'
|
579
606
|
end
|
580
607
|
article
|
@@ -583,31 +610,42 @@ module RandomWords
|
|
583
610
|
# Generate a random plural article
|
584
611
|
# @return [String] A randomly selected plural article
|
585
612
|
def random_plural_article
|
586
|
-
plural_articles.
|
613
|
+
"#{debug('PLA')}#{plural_articles.rotate[0]}"
|
587
614
|
end
|
588
615
|
|
589
616
|
# Generate a random clause
|
590
617
|
# @return [String] A randomly selected clause
|
591
618
|
def random_clause
|
592
|
-
clauses.sample
|
619
|
+
"#{debug('CLA')}#{clauses.sample}"
|
620
|
+
end
|
621
|
+
|
622
|
+
# Generate a random set of separators
|
623
|
+
def random_separators
|
624
|
+
[',', ',', ',', ';', ':', ' —']
|
625
|
+
end
|
626
|
+
|
627
|
+
# Generate a random separator
|
628
|
+
# @return [String] A randomly selected separator
|
629
|
+
def random_separator
|
630
|
+
"#{debug('SEP')}#{random_separators.sample}"
|
593
631
|
end
|
594
632
|
|
595
633
|
# Generate a random subordinate conjunction
|
596
634
|
# @return [String] A randomly selected subordinate conjunction
|
597
635
|
def random_subordinate_conjunction
|
598
|
-
subordinate_conjunctions.
|
636
|
+
"#{debug('SUC')}#{subordinate_conjunctions.rotate[0]}"
|
599
637
|
end
|
600
638
|
|
601
639
|
# Generate a random coordinating conjunction
|
602
640
|
# @return [String] A randomly selected coordinating conjunction
|
603
641
|
def random_coordinating_conjunction
|
604
|
-
coordinating_conjunctions.
|
642
|
+
"#{debug('COC')}#{coordinating_conjunctions.rotate[0]}"
|
605
643
|
end
|
606
644
|
|
607
645
|
# Generate a random preposition
|
608
646
|
# @return [String] A randomly selected preposition
|
609
647
|
def random_preposition
|
610
|
-
prepositions.
|
648
|
+
"#{debug('PRE')}#{prepositions.rotate[0]}"
|
611
649
|
end
|
612
650
|
|
613
651
|
# Generate a random prepositional phrase
|
@@ -624,7 +662,7 @@ module RandomWords
|
|
624
662
|
"#{random_article_for_word(noun)} #{noun}"
|
625
663
|
end
|
626
664
|
|
627
|
-
"#{preposition} #{phrase}"
|
665
|
+
"#{debug('PRP')}#{preposition} #{phrase}"
|
628
666
|
end
|
629
667
|
|
630
668
|
# Generate a random terminator
|
@@ -653,20 +691,25 @@ module RandomWords
|
|
653
691
|
# @example
|
654
692
|
# generate_main_clause # Returns a random main clause
|
655
693
|
def generate_main_clause
|
656
|
-
|
657
|
-
|
658
|
-
beginning = if start_with_phrase
|
694
|
+
beginning = case rand(10)
|
695
|
+
when 0..1
|
659
696
|
random_phrase
|
660
|
-
|
697
|
+
when 2..3
|
661
698
|
"#{random_number_with_plural} #{random_adverb} #{random_plural_verb}"
|
699
|
+
when 4..5
|
700
|
+
random_name
|
701
|
+
when 6..7
|
702
|
+
noun = random_noun
|
703
|
+
"#{random_adverb}, #{random_article_for_word(noun)} #{noun} #{random_verb}"
|
662
704
|
else
|
663
705
|
noun = random_noun
|
664
706
|
adjective = random_adjective
|
665
707
|
"#{random_article_for_word(adjective)} #{adjective} #{noun} #{random_adverb} #{random_verb}"
|
666
708
|
end
|
667
709
|
tail = roll(50) ? " #{random_prepositional_phrase}" : ''
|
668
|
-
|
669
|
-
"#{
|
710
|
+
separator = random_separator
|
711
|
+
tail += roll(10) ? "#{separator} #{random_clause}" : ''
|
712
|
+
"#{beginning.strip.sub(/[#{Regexp.escape(separator)}]*$/, separator)}#{tail}"
|
670
713
|
end
|
671
714
|
|
672
715
|
# Simplified generate_additional_clauses
|
@@ -35,14 +35,16 @@ module RandomWords
|
|
35
35
|
code: false,
|
36
36
|
mark: false,
|
37
37
|
headers: false,
|
38
|
-
table: false
|
38
|
+
table: false,
|
39
|
+
extended: false
|
39
40
|
}
|
40
41
|
|
41
42
|
@options = defaults.merge(options)
|
42
43
|
|
43
44
|
@generator = RandomWords::Generator.new(@options[:source], {
|
44
45
|
sentence_length: @options[:length],
|
45
|
-
paragraph_length: @options[:sentences]
|
46
|
+
paragraph_length: @options[:sentences],
|
47
|
+
use_extended_punctuation: @options[:extended]
|
46
48
|
})
|
47
49
|
|
48
50
|
@output = ''
|
@@ -107,10 +109,26 @@ module RandomWords
|
|
107
109
|
|
108
110
|
ensure_block_newlines
|
109
111
|
compress_newlines
|
112
|
+
convert_punctuation
|
110
113
|
end
|
111
114
|
|
112
115
|
private
|
113
116
|
|
117
|
+
# Convert non-ascii punctuation to Markdown equivalents.
|
118
|
+
# @param [String] text The text to convert.
|
119
|
+
# @return [String] The converted text.
|
120
|
+
def convert_punctuation
|
121
|
+
text = @output
|
122
|
+
text.gsub!(/“|”/, '"')
|
123
|
+
text.gsub!(/‘|’/, "'")
|
124
|
+
text.gsub!(/–/, '-')
|
125
|
+
text.gsub!(/—/, '--')
|
126
|
+
text.gsub!(/—/, '---')
|
127
|
+
text.gsub!(/•/, '*')
|
128
|
+
text.gsub!(/…/, '...')
|
129
|
+
@output = text
|
130
|
+
end
|
131
|
+
|
114
132
|
# Rolls for zero with the specified odds.
|
115
133
|
def roll(odds)
|
116
134
|
Random.rand(0..odds).zero?
|
@@ -349,7 +367,6 @@ module RandomWords
|
|
349
367
|
s = { short: 2, medium: 4, long: 6, very_long: 8 }[@options[:length]]
|
350
368
|
count.times do
|
351
369
|
p = @generator.sentences(s).join(' ')
|
352
|
-
|
353
370
|
should_em = force.include?(:em) || (em && roll(1))
|
354
371
|
should_strong = force.include?(:strong) || (strong && roll(1))
|
355
372
|
should_code = force.include?(:code) || (code && roll(6))
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Original code by Mike Burns
|
3
|
+
# Original code by Mike Burns 2007, updated by Brett Terpstra 2025
|
4
4
|
|
5
5
|
# Copyright 2007 Mike Burns
|
6
6
|
|
@@ -40,7 +40,7 @@ module RandomWords
|
|
40
40
|
place += 1
|
41
41
|
tmp /= 1000
|
42
42
|
end
|
43
|
-
final == '' ? 'zero' : final.sub(/\s+$/, '')
|
43
|
+
final == '' ? 'zero' : final.sub(/ and *$/, '').sub(/\s+$/, '')
|
44
44
|
end
|
45
45
|
|
46
46
|
# For testing edge cases
|
@@ -67,9 +67,9 @@ module RandomWords
|
|
67
67
|
hundreds = self / 100
|
68
68
|
tens = self % 100
|
69
69
|
if tens.zero?
|
70
|
-
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]}", place, numbers)
|
70
|
+
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]} and ", place, numbers)
|
71
71
|
else
|
72
|
-
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]} " + tens.tens_place_to_word(numbers), place,
|
72
|
+
append_place(hundreds.digit_to_word(numbers) + " #{numbers[:places][2]} and " + tens.tens_place_to_word(numbers), place,
|
73
73
|
numbers)
|
74
74
|
end
|
75
75
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RandomWords
|
4
|
+
# Number helpers
|
5
|
+
class ::Numeric
|
6
|
+
def to_commas
|
7
|
+
num = to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
|
8
|
+
dec = num.split('.')
|
9
|
+
num = dec[0].to_s + '.' + dec[1].to_s.ljust(2, '0')[0,2] if dec[1]
|
10
|
+
num
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/random-words/string.rb
CHANGED
@@ -23,7 +23,7 @@ module RandomWords
|
|
23
23
|
gsub(/[^-a-zA-Z0-9\s]/, '').strip
|
24
24
|
end
|
25
25
|
|
26
|
-
#
|
26
|
+
# Capitalize instances of "i" in a string.
|
27
27
|
# @return [String] The string with "i" capitalized.
|
28
28
|
# @example
|
29
29
|
# "this is an i".capitalize_i # => "this is an I"
|
@@ -41,12 +41,20 @@ module RandomWords
|
|
41
41
|
def capitalize
|
42
42
|
return self if empty?
|
43
43
|
|
44
|
-
|
44
|
+
str = dup
|
45
|
+
|
46
|
+
debug = ''
|
47
|
+
str.sub!(/^%[A-Z]+%/) do |match|
|
48
|
+
debug = match
|
49
|
+
''
|
50
|
+
end
|
51
|
+
|
52
|
+
letters = str.split('')
|
45
53
|
string = []
|
46
54
|
string << letters.shift while letters[0] !~ /[[:word:]]/
|
47
55
|
string << letters.shift.upcase
|
48
56
|
string.concat(letters)
|
49
|
-
string.join('')
|
57
|
+
debug + string.join('')
|
50
58
|
end
|
51
59
|
|
52
60
|
# Downcase the first letter of a string, respecting punctuation.
|
@@ -76,26 +84,26 @@ module RandomWords
|
|
76
84
|
return capitalize if terminator_ends.empty?
|
77
85
|
|
78
86
|
terminator_regex = Regexp.new("[#{Regexp.escape(terminator_ends.join)}]")
|
79
|
-
return capitalize unless
|
87
|
+
return capitalize unless match?(terminator_regex)
|
80
88
|
|
81
89
|
split(/(?<=#{terminator_regex}) /).map do |sentence|
|
82
90
|
sentence.capitalize
|
83
91
|
end.join(' ')
|
84
92
|
end
|
85
93
|
|
86
|
-
# Remove duplicate
|
87
|
-
# @return [String] The string with duplicate
|
94
|
+
# Remove duplicate punctuation
|
95
|
+
# @return [String] The string with duplicate punctuation removed.
|
88
96
|
# @example
|
89
|
-
# "Hello, , World!".
|
90
|
-
def
|
91
|
-
gsub(/(
|
97
|
+
# "Hello, , World!".dedup_punctuation # => "Hello, World!"
|
98
|
+
def dedup_punctuation
|
99
|
+
gsub(/((%[A-Z]+%)?[,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1')
|
92
100
|
end
|
93
101
|
|
94
102
|
# Generate a sentence with capitalization and terminator
|
95
103
|
# @param terminator [Array<String>] An array of beginning and ending punctuation marks.
|
96
104
|
# @return [String] The string with a random punctuation mark at the end.
|
97
105
|
def to_sent(terminator)
|
98
|
-
capitalize.compress.capitalize_i.
|
106
|
+
capitalize.compress.capitalize_i.dedup_punctuation.terminate(terminator)
|
99
107
|
end
|
100
108
|
|
101
109
|
# Split a string by newlines, clean each line, and remove empty lines.
|
@@ -104,7 +112,9 @@ module RandomWords
|
|
104
112
|
# "Hello, World!\n\nThis is a test.".split_lines # => ["Hello World", "This is a test"]
|
105
113
|
#
|
106
114
|
def split_lines
|
107
|
-
strip.split("\n").map(&:clean)
|
115
|
+
arr = strip.split("\n").map(&:clean)
|
116
|
+
arr.reject!(&:empty?)
|
117
|
+
arr
|
108
118
|
end
|
109
119
|
|
110
120
|
# Compress multiple spaces into a single space and remove leading/trailing spaces.
|
@@ -120,7 +130,13 @@ module RandomWords
|
|
120
130
|
# "Hello World".terminate(["", "."]) # => "Hello World."
|
121
131
|
#
|
122
132
|
def terminate(terminator)
|
123
|
-
|
133
|
+
debug = ''
|
134
|
+
str = dup
|
135
|
+
str.sub!(/^%[A-Z]+%/) do |match|
|
136
|
+
debug = match
|
137
|
+
''
|
138
|
+
end
|
139
|
+
debug + str.sub(/^/, terminator[0]).sub(/[^a-zA-Z0-9]*$/, terminator[1])
|
124
140
|
end
|
125
141
|
|
126
142
|
# Remove any punctuation mark from the end of a string.
|
@@ -204,7 +220,7 @@ module RandomWords
|
|
204
220
|
|
205
221
|
sources.each do |_k, v|
|
206
222
|
v.names.each do |name|
|
207
|
-
next unless
|
223
|
+
next unless /^#{self}/i.match?(name.to_s)
|
208
224
|
|
209
225
|
new_source = v.name
|
210
226
|
break
|
@@ -231,5 +247,88 @@ module RandomWords
|
|
231
247
|
:medium
|
232
248
|
end
|
233
249
|
end
|
250
|
+
|
251
|
+
# Terminal output colors
|
252
|
+
def colors
|
253
|
+
{
|
254
|
+
black: 30,
|
255
|
+
red: 31,
|
256
|
+
green: 32,
|
257
|
+
yellow: 33,
|
258
|
+
blue: 34,
|
259
|
+
magenta: 35,
|
260
|
+
cyan: 36,
|
261
|
+
white: 37,
|
262
|
+
boldblack: '1;30',
|
263
|
+
boldred: '1;31',
|
264
|
+
boldgreen: '1;32',
|
265
|
+
boldyellow: '1;33',
|
266
|
+
boldblue: '1;34',
|
267
|
+
boldmagenta: '1;35',
|
268
|
+
boldcyan: '1;36',
|
269
|
+
boldwhite: '1;37',
|
270
|
+
reset: 0
|
271
|
+
}
|
272
|
+
end
|
273
|
+
|
274
|
+
# Colorize the text for terminal output.
|
275
|
+
# @param text [String] The text to colorize.
|
276
|
+
# @param color [Symbol] The color to use (e.g., :red, :green).
|
277
|
+
#
|
278
|
+
# If the output is not a TTY, the text is returned without colorization.
|
279
|
+
#
|
280
|
+
# @return [String] The colorized text.
|
281
|
+
# @example
|
282
|
+
# colorize_text("Hello, World!", :red) # => "\e[31mHello, World!\e[0m"
|
283
|
+
def colorize_text(text, color, testing = false)
|
284
|
+
return text if !$stdout.isatty && !testing
|
285
|
+
|
286
|
+
return text unless colors.key?(color)
|
287
|
+
|
288
|
+
return text if text.empty?
|
289
|
+
|
290
|
+
color_code = colors[color]
|
291
|
+
|
292
|
+
"\e[#{color_code}m#{text}\e[0m"
|
293
|
+
end
|
294
|
+
|
295
|
+
# Dictionary of expansions
|
296
|
+
def expansions
|
297
|
+
{
|
298
|
+
'ADJ' => ['Adjective', :yellow],
|
299
|
+
'ADV' => ['Adverb', :boldyellow],
|
300
|
+
'ART' => ['Article', :cyan],
|
301
|
+
'CLA' => ['Clause', :magenta],
|
302
|
+
'COC' => ['Coordinating Conjunction', :magenta],
|
303
|
+
'CON' => ['Conjunction', :magenta],
|
304
|
+
'NAM' => ['Name', :boldblue],
|
305
|
+
'NOU' => ['Noun', :green],
|
306
|
+
'NUM' => ['Number', :red],
|
307
|
+
'PAV' => ['Passive Verb', :boldred],
|
308
|
+
'PHR' => ['Phrase', :boldcyan],
|
309
|
+
'PLA' => ['Plural Article', :yellow],
|
310
|
+
'PLN' => ['Plural Noun', :green],
|
311
|
+
'PLV' => ['Plural Verb', :boldred],
|
312
|
+
'PNO' => ['Proper Noun', :boldblack],
|
313
|
+
'PRE' => ['Preposition', :boldwhite],
|
314
|
+
'PRP' => ['Prepositional Phrase', :boldwhite],
|
315
|
+
'SEP' => ['Separator', :red],
|
316
|
+
'SUC' => ['Subordinate Conjunction', :magenta],
|
317
|
+
'TER' => ['Terminator', :boldcyan],
|
318
|
+
'VER' => ['Verb', :boldred]
|
319
|
+
}
|
320
|
+
end
|
321
|
+
|
322
|
+
# Expand abbreviated debug statements in the string.
|
323
|
+
# @return [String] The expanded debug string.
|
324
|
+
def expand_debug(testing = false)
|
325
|
+
gsub(/%(#{Regexp.union(expansions.keys)})%?/) do
|
326
|
+
match = Regexp.last_match
|
327
|
+
|
328
|
+
return match unless expansions.key?(match[1])
|
329
|
+
|
330
|
+
colorize_text("[#{expansions[match[1]][0] || match}]", expansions[match[1]][1] || :white, testing)
|
331
|
+
end
|
332
|
+
end
|
234
333
|
end
|
235
334
|
end
|
data/lib/random-words/version.rb
CHANGED