random-words 1.0.9 → 1.0.11
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/CHANGELOG.md +23 -0
- data/README.md +3 -2
- data/bin/randw +9 -4
- data/lib/random-words/array.rb +9 -0
- data/lib/random-words/config.rb +1 -1
- data/lib/random-words/generator.rb +69 -47
- data/lib/random-words/number-to-word.rb +4 -4
- data/lib/random-words/numeric.rb +13 -0
- data/lib/random-words/string.rb +109 -10
- data/lib/random-words/version.rb +1 -1
- data/lib/random-words/words/1984/clauses.txt +79 -79
- data/lib/random-words/words/alice/clauses.txt +81 -81
- data/lib/random-words/words/bacon/clauses.txt +639 -639
- 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/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/latin/clauses.txt +87 -89
- data/lib/random-words/words/veggie/clauses.txt +57 -57
- data/lib/random-words.rb +1 -0
- data/src/_README.md +4 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57abe07bfca026af5e60d98956fc1f1acea891691d0b9e26fd7ea10e567bc573
|
4
|
+
data.tar.gz: '028696c284724b1e3d8abd9048365035d48e7e50f84239fa3244430ff1fe0b33'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ccead8e4431c2fb4b3e4574a5947ae7aef581b85ceaae4c541fd68efb0599973545f09981b269f0e7220b0eb74effa6a0a77ef61b599096b9dd8871b7175f16
|
7
|
+
data.tar.gz: 81c3fc2a730c475b3999646a128487a1cf4904005c1507f0745a63845615b136d45f70c6464d7ab6c2d5a931d37625f0ee28493f1bdacf9f2d89331b0490f936
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
### 1.0.11
|
2
|
+
|
3
|
+
2025-04-22 07:11
|
4
|
+
|
5
|
+
#### FIXED
|
6
|
+
|
7
|
+
- Latin was outputting too many "quod est" clauses
|
8
|
+
- Fix for nil debug setting
|
9
|
+
|
10
|
+
### 1.0.10
|
11
|
+
|
12
|
+
2025-04-22 06:30
|
13
|
+
|
14
|
+
#### NEW
|
15
|
+
|
16
|
+
- Debug mode that shows parts of speech in output
|
17
|
+
|
18
|
+
#### IMPROVED
|
19
|
+
|
20
|
+
- Fix clauses so sentences make more sense
|
21
|
+
- Number to word phrasing
|
22
|
+
- Fixes for spec tests
|
23
|
+
|
1
24
|
### 1.0.9
|
2
25
|
|
3
26
|
2025-04-20 06:34
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
|
3
|
-
[](https://rubygems.org/gems/random-words)[](https://rubygems.org/gems/random-words)[](https://opensource.org/licenses/MIT)
|
4
4
|
|
5
5
|
A random text (Lorem Ipsum) generator.
|
6
6
|
|
@@ -67,7 +67,7 @@ elements to include.
|
|
67
67
|
|
68
68
|
First, the source language (defaults to latin), then the
|
69
69
|
length of paragraphs and tables: e.g. `english/medium`. You
|
70
|
-
can add any digits to determine how many
|
70
|
+
can add any digits to determine how many paragraphs are
|
71
71
|
generated (default 5), e.g. `corporate/medium/10`.
|
72
72
|
|
73
73
|
Then you can add individual elements, or use `/all` to
|
@@ -86,6 +86,7 @@ trigger all elements. The elements available are:
|
|
86
86
|
| headers | add headlines |
|
87
87
|
| image | add images |
|
88
88
|
| table | add tables |
|
89
|
+
| x | add extended punctuation |
|
89
90
|
|
90
91
|
The number of elements inserted depends on the length you specify.
|
91
92
|
|
data/bin/randw
CHANGED
@@ -44,6 +44,10 @@ module RandomWords
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def colorize_text(text, color)
|
47
|
+
return text unless $stdout.isatty
|
48
|
+
|
49
|
+
return text unless colors.key?(color)
|
50
|
+
|
47
51
|
color_code = colors[color]
|
48
52
|
|
49
53
|
"\e[#{color_code}m#{text}\e[0m"
|
@@ -190,7 +194,7 @@ end
|
|
190
194
|
paragraph_length: 3,
|
191
195
|
count: 1,
|
192
196
|
method: nil,
|
193
|
-
debug:
|
197
|
+
debug: 0,
|
194
198
|
whitespace: true,
|
195
199
|
separator: " "
|
196
200
|
}
|
@@ -307,8 +311,9 @@ OptionParser.new do |opts|
|
|
307
311
|
|
308
312
|
opts.separator "OTHER OPTIONS:"
|
309
313
|
|
310
|
-
opts.on("-d", "--debug", "Enable debug mode, displays sentence/word/character counts") do
|
314
|
+
opts.on("-d", "--debug [LEVEL]", "Enable debug mode, 1 displays sentence/word/character counts, 2 displays parts of speech (default 1)") do |level|
|
311
315
|
@options[:debug] = true
|
316
|
+
@options[:debug_level] = level.to_i || 1
|
312
317
|
end
|
313
318
|
|
314
319
|
opts.on("-h", "--help", "Display this help message") do
|
@@ -418,7 +423,7 @@ def markdown_settings(settings)
|
|
418
423
|
markdown_options
|
419
424
|
end
|
420
425
|
|
421
|
-
@rw = RandomWords::Generator.new(@options[:source])
|
426
|
+
@rw = RandomWords::Generator.new(@options[:source], { debug: @options[:debug_level] && @options[:debug_level] > 1 })
|
422
427
|
@rw.sentence_length = @options[:length] || :medium
|
423
428
|
@rw.paragraph_length = @options[:paragraph_length] || 3
|
424
429
|
@options[:use_extended_punctuation] = @options[:use_extended_punctuation] ? true : false
|
@@ -496,7 +501,7 @@ when :password
|
|
496
501
|
cap_char = rand(20) while p[cap_char] !~ /[a-z]/
|
497
502
|
p[cap_char] = p[cap_char].upcase
|
498
503
|
|
499
|
-
tail_char =
|
504
|
+
tail_char = /[^a-z0-9]/i.match?(p) ? "" : %w[! @ # $ % ^ & * ( ) _ + - = ; : < > , . ? /].sample
|
500
505
|
|
501
506
|
print p.gsub(/ /, @options[:separator]) + tail_char
|
502
507
|
debug "#{p.split(@options[:separator]).count}w, #{p.length}c"
|
data/lib/random-words/array.rb
CHANGED
@@ -67,5 +67,14 @@ module RandomWords
|
|
67
67
|
full_names.delete_if(&:empty?)
|
68
68
|
[first_names, last_names, full_names]
|
69
69
|
end
|
70
|
+
|
71
|
+
def rotate
|
72
|
+
return self if empty?
|
73
|
+
|
74
|
+
# Rotate the array by moving the first element to the end
|
75
|
+
first_element = shift
|
76
|
+
push(first_element)
|
77
|
+
self
|
78
|
+
end
|
70
79
|
end
|
71
80
|
end
|
data/lib/random-words/config.rb
CHANGED
@@ -212,7 +212,7 @@ module RandomWords
|
|
212
212
|
source: configuration[:source].to_source || :latin,
|
213
213
|
sentence_length: configuration[:length].to_length || :medium,
|
214
214
|
paragraph_length: configuration[:paragraph_length].to_i || 5,
|
215
|
-
use_extended_punctuation: ext_punc && ext_punc.trueish? || false
|
215
|
+
use_extended_punctuation: (ext_punc && ext_punc.trueish?) || false
|
216
216
|
}
|
217
217
|
end
|
218
218
|
|
@@ -31,6 +31,10 @@ module RandomWords
|
|
31
31
|
# @return [Hash<String, RandomWords::Source>] List of available sources
|
32
32
|
attr_reader :sources
|
33
33
|
|
34
|
+
# Debug mode
|
35
|
+
# @return [Boolean] true if debug mode is enabled, false otherwise
|
36
|
+
attr_accessor :debug
|
37
|
+
|
34
38
|
# Define the default sentence parts
|
35
39
|
# These parts will be used to generate random sentences and character strings
|
36
40
|
SENTENCE_PARTS = %w[random_article random_adjective random_noun random_adverb random_verb random_adjective
|
@@ -46,6 +50,7 @@ module RandomWords
|
|
46
50
|
# generator.sentence_length = :long
|
47
51
|
# generator.paragraph_length = 3
|
48
52
|
def initialize(source = :english, options = {})
|
53
|
+
@debug = options[:debug] || false
|
49
54
|
@tested = []
|
50
55
|
@config = RandomWords::Config.new(source)
|
51
56
|
@source = source
|
@@ -86,6 +91,13 @@ module RandomWords
|
|
86
91
|
lengths
|
87
92
|
end
|
88
93
|
|
94
|
+
# Display debug message
|
95
|
+
def debug(msg)
|
96
|
+
return unless @debug
|
97
|
+
|
98
|
+
"%#{msg}%"
|
99
|
+
end
|
100
|
+
|
89
101
|
# Shortcut for RandomWords::Config.create_user_dictionary
|
90
102
|
# @param title [String] The title of the user dictionary
|
91
103
|
# @return [Symbol] The symbolized name of the dictionary
|
@@ -149,6 +161,7 @@ module RandomWords
|
|
149
161
|
# @!visibility private
|
150
162
|
def test_random
|
151
163
|
RandomWords.testing = true
|
164
|
+
@debug = true
|
152
165
|
@use_extended_punctuation = true
|
153
166
|
res = []
|
154
167
|
res << random_noun
|
@@ -291,7 +304,7 @@ module RandomWords
|
|
291
304
|
def generate_combined_sentence(length = nil)
|
292
305
|
length ||= define_length(@sentence_length)
|
293
306
|
sentence = generate_sentence
|
294
|
-
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
|
295
308
|
|
296
309
|
while sentence.length < length
|
297
310
|
# Generate a random number of sentences to combine
|
@@ -301,7 +314,7 @@ module RandomWords
|
|
301
314
|
sentence = "#{sentence.strip.no_term(terminators)}, #{random_coordinating_conjunction} #{new_sentence.no_term(terminators)}"
|
302
315
|
end
|
303
316
|
|
304
|
-
sentence.to_sent(random_terminator).fix_caps(terminators)
|
317
|
+
sentence.to_sent(random_terminator).fix_caps(terminators).expand_debug
|
305
318
|
end
|
306
319
|
|
307
320
|
# Generate a random paragraph
|
@@ -318,12 +331,6 @@ module RandomWords
|
|
318
331
|
sentences.join(' ').strip.compress
|
319
332
|
end
|
320
333
|
|
321
|
-
# Generate a random name
|
322
|
-
# @return [String] A randomly generated name
|
323
|
-
def name
|
324
|
-
random_name
|
325
|
-
end
|
326
|
-
|
327
334
|
# Generate a random code language
|
328
335
|
# @return [Symbol] A randomly selected code language
|
329
336
|
def code_lang
|
@@ -366,6 +373,12 @@ module RandomWords
|
|
366
373
|
RandomWords::LoremMarkdown.new(settings).output
|
367
374
|
end
|
368
375
|
|
376
|
+
# Generate a random name
|
377
|
+
# @return [String] A randomly generated name
|
378
|
+
def name
|
379
|
+
"#{debug('NAM')}#{random_name}"
|
380
|
+
end
|
381
|
+
|
369
382
|
private
|
370
383
|
|
371
384
|
# Struct for overflow configuration
|
@@ -431,21 +444,25 @@ module RandomWords
|
|
431
444
|
additional_clauses = generate_additional_clauses
|
432
445
|
sentence_components.concat(additional_clauses)
|
433
446
|
sentence_components.map!(&:strip)
|
434
|
-
break if sentence_components.join(' ').length >= length
|
435
|
-
|
436
|
-
conjunction = if roll(50) || (RandomWords.testing && !RandomWords.tested.include?('subordinate_conjunction'))
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
# sentence_components.unshift(conjunction.capitalize) # Place conjunction at the start
|
444
|
-
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?
|
445
458
|
end
|
446
459
|
|
447
460
|
# Join all parts into a single sentence
|
448
|
-
|
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
|
449
466
|
end
|
450
467
|
|
451
468
|
# Convert a length symbol to a specific length value
|
@@ -492,7 +509,7 @@ module RandomWords
|
|
492
509
|
# @example
|
493
510
|
# random_conjunction # Returns a random conjunction
|
494
511
|
def random_conjunction
|
495
|
-
coordinating_conjunctions.sample
|
512
|
+
"#{debug('COC')}#{coordinating_conjunctions.sample}"
|
496
513
|
end
|
497
514
|
|
498
515
|
# Generate a random number with a plural noun
|
@@ -502,68 +519,72 @@ module RandomWords
|
|
502
519
|
# random_number_with_plural # Returns a string like "three cats"
|
503
520
|
def random_number_with_plural
|
504
521
|
num = rand(1000)
|
505
|
-
number =
|
522
|
+
number = if roll(50)
|
523
|
+
num.to_word(@numbers)
|
524
|
+
else
|
525
|
+
num.to_commas
|
526
|
+
end
|
506
527
|
if num == 1 || (RandomWords.testing && !RandomWords.tested.include?('random_noun'))
|
507
528
|
RandomWords.tested << 'random_noun' if RandomWords.testing
|
508
|
-
"#{number} #{random_noun}"
|
529
|
+
"#{debug('NUM')}#{number} #{random_noun}"
|
509
530
|
else
|
510
531
|
RandomWords.tested << 'random_plural_noun' if RandomWords.testing
|
511
|
-
"#{number} #{random_plural_noun}"
|
532
|
+
"#{debug('NUM')}#{number} #{random_plural_noun}"
|
512
533
|
end
|
513
534
|
end
|
514
535
|
|
515
536
|
# Generate a random phrase
|
516
537
|
# @return [String] A randomly selected phrase
|
517
538
|
def random_phrase
|
518
|
-
phrases.sample
|
539
|
+
"#{debug('PHR')}#{phrases.sample}"
|
519
540
|
end
|
520
541
|
|
521
542
|
# Generate a random noun
|
522
543
|
# @return [String] A randomly selected noun
|
523
544
|
def random_noun
|
524
|
-
nouns.sample
|
545
|
+
"#{debug('NOU')}#{nouns.sample}"
|
525
546
|
end
|
526
547
|
|
527
548
|
# Generate a random plural noun
|
528
549
|
# @return [String] A randomly selected plural noun
|
529
550
|
def random_plural_noun
|
530
|
-
plural_nouns.sample
|
551
|
+
"#{debug('PLN')}#{plural_nouns.sample}"
|
531
552
|
end
|
532
553
|
|
533
554
|
# Generate a random verb
|
534
555
|
# @return [String] A randomly selected verb
|
535
556
|
def random_verb
|
536
|
-
verbs.sample
|
557
|
+
"#{debug('VER')}#{verbs.sample}"
|
537
558
|
end
|
538
559
|
|
539
560
|
# Generate a random plural verb
|
540
561
|
# @return [String] A randomly selected plural verb
|
541
562
|
def random_plural_verb
|
542
|
-
plural_verbs.sample
|
563
|
+
"#{debug('PLV')}#{plural_verbs.sample}"
|
543
564
|
end
|
544
565
|
|
545
566
|
# Generate a random passive verb
|
546
567
|
# @return [String] A randomly selected passive verb
|
547
568
|
def random_passive_verb
|
548
|
-
passive_verbs.sample
|
569
|
+
"#{debug('PAV')}#{passive_verbs.sample}"
|
549
570
|
end
|
550
571
|
|
551
572
|
# Generate a random adverb
|
552
573
|
# @return [String] A randomly selected adverb
|
553
574
|
def random_adverb
|
554
|
-
adverbs.sample
|
575
|
+
"#{debug('ADV')}#{adverbs.sample}"
|
555
576
|
end
|
556
577
|
|
557
578
|
# Generate a random adjective
|
558
579
|
# @return [String] A randomly selected adjective
|
559
580
|
def random_adjective
|
560
|
-
adjectives.sample
|
581
|
+
"#{debug('ADJ')}#{adjectives.sample}"
|
561
582
|
end
|
562
583
|
|
563
584
|
# Generate a random article
|
564
585
|
# @return [String] A randomly selected article
|
565
586
|
def random_article
|
566
|
-
articles.
|
587
|
+
"#{debug('ART')}#{articles.rotate[0]}"
|
567
588
|
end
|
568
589
|
|
569
590
|
# Generate a random article for a noun
|
@@ -580,7 +601,7 @@ module RandomWords
|
|
580
601
|
RandomWords.tested << 'random_article_for_word' if RandomWords.testing
|
581
602
|
if word.start_with?(/[aeiou]/i) && article =~ /^an?$/i
|
582
603
|
article = 'an'
|
583
|
-
elsif
|
604
|
+
elsif /^an$/i.match?(article)
|
584
605
|
article = 'a'
|
585
606
|
end
|
586
607
|
article
|
@@ -589,13 +610,13 @@ module RandomWords
|
|
589
610
|
# Generate a random plural article
|
590
611
|
# @return [String] A randomly selected plural article
|
591
612
|
def random_plural_article
|
592
|
-
plural_articles.
|
613
|
+
"#{debug('PLA')}#{plural_articles.rotate[0]}"
|
593
614
|
end
|
594
615
|
|
595
616
|
# Generate a random clause
|
596
617
|
# @return [String] A randomly selected clause
|
597
618
|
def random_clause
|
598
|
-
clauses.sample
|
619
|
+
"#{debug('CLA')}#{clauses.sample}"
|
599
620
|
end
|
600
621
|
|
601
622
|
# Generate a random set of separators
|
@@ -606,25 +627,25 @@ module RandomWords
|
|
606
627
|
# Generate a random separator
|
607
628
|
# @return [String] A randomly selected separator
|
608
629
|
def random_separator
|
609
|
-
random_separators.sample
|
630
|
+
"#{debug('SEP')}#{random_separators.sample}"
|
610
631
|
end
|
611
632
|
|
612
633
|
# Generate a random subordinate conjunction
|
613
634
|
# @return [String] A randomly selected subordinate conjunction
|
614
635
|
def random_subordinate_conjunction
|
615
|
-
subordinate_conjunctions.
|
636
|
+
"#{debug('SUC')}#{subordinate_conjunctions.rotate[0]}"
|
616
637
|
end
|
617
638
|
|
618
639
|
# Generate a random coordinating conjunction
|
619
640
|
# @return [String] A randomly selected coordinating conjunction
|
620
641
|
def random_coordinating_conjunction
|
621
|
-
coordinating_conjunctions.
|
642
|
+
"#{debug('COC')}#{coordinating_conjunctions.rotate[0]}"
|
622
643
|
end
|
623
644
|
|
624
645
|
# Generate a random preposition
|
625
646
|
# @return [String] A randomly selected preposition
|
626
647
|
def random_preposition
|
627
|
-
prepositions.
|
648
|
+
"#{debug('PRE')}#{prepositions.rotate[0]}"
|
628
649
|
end
|
629
650
|
|
630
651
|
# Generate a random prepositional phrase
|
@@ -641,7 +662,7 @@ module RandomWords
|
|
641
662
|
"#{random_article_for_word(noun)} #{noun}"
|
642
663
|
end
|
643
664
|
|
644
|
-
"#{preposition} #{phrase}"
|
665
|
+
"#{debug('PRP')}#{preposition} #{phrase}"
|
645
666
|
end
|
646
667
|
|
647
668
|
# Generate a random terminator
|
@@ -670,15 +691,16 @@ module RandomWords
|
|
670
691
|
# @example
|
671
692
|
# generate_main_clause # Returns a random main clause
|
672
693
|
def generate_main_clause
|
673
|
-
|
674
|
-
|
675
|
-
start_with_name = roll(20)
|
676
|
-
beginning = if start_with_phrase
|
694
|
+
beginning = case rand(10)
|
695
|
+
when 0..1
|
677
696
|
random_phrase
|
678
|
-
|
697
|
+
when 2..3
|
679
698
|
"#{random_number_with_plural} #{random_adverb} #{random_plural_verb}"
|
680
|
-
|
699
|
+
when 4..5
|
681
700
|
random_name
|
701
|
+
when 6..7
|
702
|
+
noun = random_noun
|
703
|
+
"#{random_adverb}, #{random_article_for_word(noun)} #{noun} #{random_verb}"
|
682
704
|
else
|
683
705
|
noun = random_noun
|
684
706
|
adjective = random_adjective
|
@@ -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,19 +84,19 @@ 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
97
|
# "Hello, , World!".dedup_punctuation # => "Hello, World!"
|
90
98
|
def dedup_punctuation
|
91
|
-
gsub(/([,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1')
|
99
|
+
gsub(/((%[A-Z]+%)?[,.;:—] ?)+/, '\1').gsub(/([,;:—])/, '\1')
|
92
100
|
end
|
93
101
|
|
94
102
|
# Generate a sentence with capitalization and terminator
|
@@ -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