random-words 1.0.11 → 1.0.13
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/.rubocop.yml +267 -20
- data/.rubocop_todo.yml +0 -313
- data/CHANGELOG.md +36 -0
- data/bin/randw +54 -194
- data/generator.cgi +155 -0
- data/lib/random-words/array.rb +0 -2
- data/lib/random-words/boolean.rb +1 -1
- data/lib/random-words/config.rb +6 -6
- data/lib/random-words/generator.rb +60 -32
- data/lib/random-words/html2markdown.rb +80 -20
- data/lib/random-words/{lorem-markdown.rb → lorem_html.rb} +123 -56
- data/lib/random-words/{number-to-word.rb → number_to_word.rb} +5 -5
- data/lib/random-words/numeric.rb +2 -2
- data/lib/random-words/string.rb +10 -3
- data/lib/random-words/terminal.rb +185 -0
- data/lib/random-words/version.rb +1 -1
- data/lib/random-words/words/corporate/clauses.txt +100 -100
- data/lib/random-words/words/walken/adjectives.txt +139 -0
- data/lib/random-words/words/walken/adverbs.txt +164 -0
- data/lib/random-words/words/walken/articles-plural.txt +10 -0
- data/lib/random-words/words/walken/articles-singular.txt +10 -0
- data/lib/random-words/words/walken/clauses.txt +200 -0
- data/lib/random-words/words/walken/config.yml +4 -0
- data/lib/random-words/words/walken/conjunctions-coordinating.txt +20 -0
- data/lib/random-words/words/walken/conjunctions-subordinate.txt +28 -0
- data/lib/random-words/words/walken/names.txt +101 -0
- data/lib/random-words/words/walken/nouns-plural.txt +330 -0
- data/lib/random-words/words/walken/nouns-singular.txt +500 -0
- data/lib/random-words/words/walken/numbers.yml +5 -0
- data/lib/random-words/words/walken/phrases.txt +45 -0
- data/lib/random-words/words/walken/prepositions.txt +45 -0
- data/lib/random-words/words/walken/terminators.txt +17 -0
- data/lib/random-words/words/walken/verbs-passive.txt +370 -0
- data/lib/random-words/words/walken/verbs-plural.txt +294 -0
- data/lib/random-words/words/walken/verbs-singular.txt +262 -0
- data/lib/random-words.rb +3 -2
- metadata +24 -4
@@ -2,23 +2,44 @@
|
|
2
2
|
|
3
3
|
module RandomWords
|
4
4
|
# Generates random Lorem Ipsum text in Markdown format.
|
5
|
-
class
|
5
|
+
class LoremHTML
|
6
6
|
# Stores the output
|
7
7
|
attr_accessor :output
|
8
8
|
|
9
9
|
# Stores the RandomWords::Generator
|
10
10
|
attr_accessor :generator
|
11
11
|
|
12
|
+
# Stores the title
|
13
|
+
attr_reader :title
|
14
|
+
|
12
15
|
# Generates random Lorem Ipsum text.
|
13
16
|
# @param options [Hash] Options for generating Lorem Ipsum text.
|
17
|
+
#
|
18
|
+
# @option options [Symbol] :source The source of the words (:latin, :english, :french, etc.).
|
19
|
+
# @option options [Integer] :grafs The number of paragraphs to generate.
|
20
|
+
# @option options [Integer] :sentences The number of sentences per paragraph.
|
21
|
+
# @option options [Symbol] :length The length of the text (:short, :medium, :long, :very_long).
|
22
|
+
# @option options [Boolean] :decorate Whether to include emphasis and strong tags.
|
23
|
+
# @option options [Boolean] :link Whether to include links.
|
24
|
+
# @option options [Boolean] :ul Whether to include unordered lists.
|
25
|
+
# @option options [Boolean] :ol Whether to include ordered lists.
|
26
|
+
# @option options [Boolean] :dl Whether to include definition lists.
|
27
|
+
# @option options [Boolean] :bq Whether to include blockquotes.
|
28
|
+
# @option options [Boolean] :code Whether to include code blocks.
|
29
|
+
# @option options [Boolean] :mark Whether to include mark tags.
|
30
|
+
# @option options [Boolean] :headers Whether to include headers.
|
31
|
+
# @option options [Boolean] :table Whether to include tables.
|
32
|
+
# @option options [Boolean] :extended Whether to use extended punctuation.
|
14
33
|
# @return [String] A string of random Lorem Ipsum text.
|
15
34
|
def initialize(options = {})
|
16
35
|
@added = {
|
17
|
-
|
36
|
+
italic: false,
|
18
37
|
strong: false,
|
19
38
|
link: false,
|
20
39
|
code: false,
|
21
|
-
mark: false
|
40
|
+
mark: false,
|
41
|
+
footnote: false,
|
42
|
+
hr: false
|
22
43
|
}
|
23
44
|
|
24
45
|
defaults = {
|
@@ -36,7 +57,10 @@ module RandomWords
|
|
36
57
|
mark: false,
|
37
58
|
headers: false,
|
38
59
|
table: false,
|
39
|
-
extended: false
|
60
|
+
extended: false,
|
61
|
+
footnote: false,
|
62
|
+
hr: false,
|
63
|
+
image: false
|
40
64
|
}
|
41
65
|
|
42
66
|
@options = defaults.merge(options)
|
@@ -46,28 +70,36 @@ module RandomWords
|
|
46
70
|
paragraph_length: @options[:sentences],
|
47
71
|
use_extended_punctuation: @options[:extended]
|
48
72
|
})
|
73
|
+
@title = fragment(2, 5).cap_first
|
49
74
|
|
50
75
|
@output = ''
|
51
76
|
strong = @options[:decorate]
|
52
|
-
|
77
|
+
italic = @options[:decorate]
|
53
78
|
links = @options[:link]
|
54
79
|
code = @options[:code]
|
55
80
|
mark = @options[:mark]
|
81
|
+
footnotes = @options[:footnote]
|
82
|
+
@options[:hr]
|
83
|
+
@options[:image]
|
56
84
|
force = []
|
57
85
|
# Generate the specified number of paragraphs.
|
58
86
|
@options[:grafs].times.with_index do |_, i|
|
59
|
-
@
|
60
|
-
|
61
|
-
strong = Random.rand(0..3).zero? && @options[:decorate]
|
62
|
-
links = Random.rand(0..3).zero? && @options[:link]
|
63
|
-
code = Random.rand(0..3).zero? && @options[:code]
|
64
|
-
|
65
|
-
if i == @options[:grafs] - 2
|
87
|
+
if @options[:grafs] == 1 || i == @options[:grafs] - 2
|
88
|
+
force << :footnote if !@added[:footnote] && @options[:footnote]
|
66
89
|
force << :code if !@added[:code] && @options[:code]
|
67
90
|
force << :link if !@added[:link] && @options[:link]
|
68
|
-
force << :
|
91
|
+
force << :italic if !@added[:italic] && @options[:decorate]
|
69
92
|
force << :strong if !@added[:strong] && @options[:decorate]
|
70
93
|
end
|
94
|
+
|
95
|
+
@output += paragraph(1, italic: italic, strong: strong, links: links, code: code, mark: mark, footnotes: footnotes,
|
96
|
+
force: force)
|
97
|
+
italic = Random.rand(0..3).zero? && @options[:decorate]
|
98
|
+
strong = Random.rand(0..3).zero? && @options[:decorate]
|
99
|
+
links = Random.rand(0..3).zero? && @options[:link]
|
100
|
+
code = Random.rand(0..3).zero? && @options[:code]
|
101
|
+
footnotes = Random.rand(0..3).zero? && @options[:footnote]
|
102
|
+
|
71
103
|
@output += "\n\n"
|
72
104
|
end
|
73
105
|
|
@@ -75,47 +107,61 @@ module RandomWords
|
|
75
107
|
end
|
76
108
|
|
77
109
|
# Use the RandomWords class to generate the Lorem Ipsum text.
|
110
|
+
#
|
111
|
+
# @return [String] The generated Lorem Ipsum HTML.
|
78
112
|
def generate
|
79
113
|
compress_newlines
|
114
|
+
handle_lists
|
115
|
+
handle_basic_elements
|
116
|
+
handle_content_blocks
|
117
|
+
finalize_output
|
118
|
+
@output
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
80
122
|
|
123
|
+
def handle_lists
|
81
124
|
items = { short: 4, medium: 8, long: 10, very_long: 12 }[@options[:length]]
|
82
125
|
|
83
126
|
if @options[:ul] && @options[:ol]
|
84
|
-
# If both unordered and ordered lists are specified, add them both.
|
85
127
|
inject_block(1, -> { list(items, :ul) })
|
86
128
|
inject_block(1, -> { list(items, :ol) })
|
87
129
|
elsif @options[:ul]
|
88
|
-
# If only unordered list is specified, add it.
|
89
130
|
inject_block(2, -> { list(items, :ul) })
|
90
131
|
elsif @options[:ol]
|
91
|
-
# If only ordered list is specified, add it.
|
92
132
|
inject_block(2, -> { list(items, :ol) })
|
93
133
|
end
|
134
|
+
end
|
94
135
|
|
95
|
-
|
96
|
-
inject_block(1, -> {
|
136
|
+
def handle_basic_elements
|
137
|
+
inject_block(1, -> { "<hr>\n\n" }) if @options[:hr]
|
97
138
|
|
98
|
-
|
99
|
-
inject_block(1, -> { blockquote(items / 2) }) if @options[:bq]
|
139
|
+
return unless @options[:image]
|
100
140
|
|
101
|
-
|
102
|
-
|
141
|
+
inject_block(1, lambda {
|
142
|
+
"<img src=\"https://picsum.photos/400/200\" alt=\"#{fragment(1, 4).cap_first}\" />\n\n"
|
143
|
+
})
|
144
|
+
end
|
103
145
|
|
104
|
-
|
105
|
-
|
146
|
+
def handle_content_blocks
|
147
|
+
items = { short: 4, medium: 8, long: 10, very_long: 12 }[@options[:length]]
|
106
148
|
|
107
|
-
|
149
|
+
inject_block(1, -> { list(items, :dl) }) if @options[:dl]
|
150
|
+
inject_block(1, -> { blockquote(items / 2) }) if @options[:bq]
|
151
|
+
inject_headers if @options[:headers]
|
152
|
+
inject_block(1, -> { code }) if @options[:code]
|
108
153
|
inject_block(1, -> { table }) if @options[:table]
|
154
|
+
end
|
109
155
|
|
156
|
+
def finalize_output
|
110
157
|
ensure_block_newlines
|
111
158
|
compress_newlines
|
112
159
|
convert_punctuation
|
113
160
|
end
|
114
161
|
|
115
|
-
private
|
116
|
-
|
117
162
|
# Convert non-ascii punctuation to Markdown equivalents.
|
118
163
|
# @param [String] text The text to convert.
|
164
|
+
#
|
119
165
|
# @return [String] The converted text.
|
120
166
|
def convert_punctuation
|
121
167
|
text = @output
|
@@ -129,31 +175,40 @@ module RandomWords
|
|
129
175
|
@output = text
|
130
176
|
end
|
131
177
|
|
132
|
-
#
|
133
|
-
|
134
|
-
|
178
|
+
# Roll a random number to determine if an action should occur
|
179
|
+
# @param percent [Integer] 1-100 percent chance of the action occurring (1-100)
|
180
|
+
#
|
181
|
+
# @return [Boolean] True if the action occurs, false otherwise
|
182
|
+
#
|
183
|
+
# @example
|
184
|
+
# roll(50) # 50% chance of returning true
|
185
|
+
def roll(percent)
|
186
|
+
rand(1..100) <= percent
|
135
187
|
end
|
136
188
|
|
137
189
|
# Generates a list of items.
|
138
190
|
# @param [Integer] count The number of items to generate.
|
139
191
|
# @param [Symbol] type The type of list to generate (:ul, :ol, :dl).
|
192
|
+
#
|
140
193
|
# @return [String] The generated list.
|
141
194
|
def list(count, type)
|
142
195
|
ul = "\n\n<#{type}>\n"
|
143
196
|
|
144
197
|
count.times do
|
145
|
-
links = roll(
|
146
|
-
|
147
|
-
strong = roll(
|
148
|
-
code = roll(
|
149
|
-
mark = roll(
|
198
|
+
links = roll(20) && @options[:link]
|
199
|
+
italic = roll(50) && @options[:decorate]
|
200
|
+
strong = roll(50) && @options[:decorate]
|
201
|
+
code = roll(10) && @options[:code]
|
202
|
+
mark = roll(10) && @options[:mark]
|
203
|
+
|
150
204
|
frag = fragment(1, 4).cap_first
|
151
205
|
long_frag = fragment(4, 8).cap_first
|
152
206
|
long_frag = inject_inline(long_frag, -> { link }) if links
|
153
|
-
long_frag = inject_inline(long_frag, -> { emphasis(:em) }) if
|
207
|
+
long_frag = inject_inline(long_frag, -> { emphasis(:em) }) if italic
|
154
208
|
long_frag = inject_inline(long_frag, -> { emphasis(:strong) }) if strong
|
155
209
|
long_frag = inject_inline(long_frag, -> { code_span }, 1) if code
|
156
210
|
long_frag = inject_inline(long_frag, -> { emphasis(:mark) }, 1) if mark
|
211
|
+
|
157
212
|
long_frag = long_frag.restore_spaces if links
|
158
213
|
if type == :dl
|
159
214
|
ul += "\t<dt>#{frag}</dt>\n"
|
@@ -168,8 +223,9 @@ module RandomWords
|
|
168
223
|
# Generates a blockquote.
|
169
224
|
# @param [Integer] count The number of items to generate.
|
170
225
|
# @param [Boolean] nested Whether the blockquote is nested.
|
226
|
+
#
|
171
227
|
# @return [String] The generated blockquote.
|
172
|
-
def blockquote(count, nested
|
228
|
+
def blockquote(count, nested: false)
|
173
229
|
if count > 1 && !nested
|
174
230
|
level1 = Random.rand(1..count - 1).to_i
|
175
231
|
level2 = count - level1
|
@@ -181,14 +237,12 @@ module RandomWords
|
|
181
237
|
newline = nested ? '' : "\n\n"
|
182
238
|
quote = "#{newline}<blockquote>\n"
|
183
239
|
quote += paragraph(level1,
|
184
|
-
|
240
|
+
italic: @options[:decorate],
|
185
241
|
strong: @options[:decorate],
|
186
242
|
links: @options[:link],
|
187
243
|
code: @options[:code],
|
188
|
-
mark: @options[:mark]).
|
189
|
-
|
190
|
-
)
|
191
|
-
quote += blockquote(level2, true).strip if level2.positive?
|
244
|
+
mark: @options[:mark]).squeeze("\n")
|
245
|
+
quote += blockquote(level2, nested: true).strip if level2.positive?
|
192
246
|
"#{quote}#{cite}</blockquote>#{newline}"
|
193
247
|
end
|
194
248
|
|
@@ -295,7 +349,17 @@ module RandomWords
|
|
295
349
|
path = "/#{path_string(4, 8)}/#{path_string(4, 8)}"
|
296
350
|
title = fragment(4, 8).cap_first
|
297
351
|
"<a%%href=\"https://example.com#{path}\"%%title=\"#{title.preserve_spaces}\">#{fragment(1,
|
298
|
-
8)
|
352
|
+
8)}</a>"
|
353
|
+
end
|
354
|
+
|
355
|
+
# Generates a random footnote
|
356
|
+
# @return [String] The generated footnote.
|
357
|
+
def footnote
|
358
|
+
@added[:footnote] = true
|
359
|
+
@counter ||= 0
|
360
|
+
@counter += 1
|
361
|
+
%(<a%%href="#fn:#{@counter}"%%id="fnref:#{@counter}"%%title="#{fragment(10,
|
362
|
+
20)}"%%class="footnote"><sup>#{@counter}</sup></a>)
|
299
363
|
end
|
300
364
|
|
301
365
|
# Injects a block of text into the output.
|
@@ -335,7 +399,7 @@ module RandomWords
|
|
335
399
|
def inject_inline(text, block, max = nil)
|
336
400
|
max ||= { short: 1, medium: 2, long: 3, very_long: 4 }[@options[:length]]
|
337
401
|
|
338
|
-
words = text.split
|
402
|
+
words = text.split
|
339
403
|
out = []
|
340
404
|
count = 0
|
341
405
|
while words.any?
|
@@ -355,30 +419,33 @@ module RandomWords
|
|
355
419
|
# Generates a paragraph of Lorem Ipsum text.
|
356
420
|
# The `words` method generates a specified number of words.
|
357
421
|
# @param [Integer] count The number of paragraphs to generate.
|
358
|
-
# @param [Boolean]
|
422
|
+
# @param [Boolean] italic Whether to include italic tags.
|
359
423
|
# @param [Boolean] strong Whether to include strong tags.
|
360
424
|
# @param [Boolean] links Whether to include links.
|
361
425
|
# @param [Boolean] code Whether to include code tags.
|
362
426
|
# @param [Boolean] mark Whether to include mark tags.
|
363
427
|
# @param [Array] force An array of tags to force.
|
364
428
|
# @return [String] The generated paragraph.
|
365
|
-
def paragraph(count,
|
429
|
+
def paragraph(count, italic: false, strong: false, links: false, code: false, mark: false, footnotes: false, force: [])
|
366
430
|
output = ''
|
431
|
+
|
367
432
|
s = { short: 2, medium: 4, long: 6, very_long: 8 }[@options[:length]]
|
368
433
|
count.times do
|
369
434
|
p = @generator.sentences(s).join(' ')
|
370
|
-
should_em = force.include?(:
|
371
|
-
should_strong = force.include?(:strong) || (strong && roll(
|
372
|
-
should_code = force.include?(:code) || (code && roll(
|
373
|
-
should_link = force.include?(:
|
374
|
-
should_mark = force.include?(:mark) || (mark && roll(
|
435
|
+
should_em = force.include?(:italic) || (italic && roll(60))
|
436
|
+
should_strong = force.include?(:strong) || (strong && roll(60))
|
437
|
+
should_code = force.include?(:code) || (code && roll(40))
|
438
|
+
should_link = force.include?(:link) || (links && roll(40))
|
439
|
+
should_mark = force.include?(:mark) || (mark && roll(40))
|
440
|
+
should_footnote = force.include?(:footnote) || (footnotes && roll(40))
|
441
|
+
|
375
442
|
p = inject_inline(p, -> { link }) if should_link
|
376
|
-
p = inject_inline(p, -> { emphasis(
|
377
|
-
p = inject_inline(p, -> { emphasis(
|
378
|
-
p = inject_inline(p, -> { emphasis(
|
443
|
+
p = inject_inline(p, -> { emphasis(:em) }) if should_em
|
444
|
+
p = inject_inline(p, -> { emphasis(:strong) }) if should_strong
|
445
|
+
p = inject_inline(p, -> { emphasis(:mark) }, 1) if should_mark
|
379
446
|
p = inject_inline(p, -> { code_span }, 1) if should_code
|
380
|
-
|
381
|
-
output += "<p>#{p.restore_spaces.cap_first.term('.')}</p>\n\n"
|
447
|
+
fn = should_footnote ? footnote.restore_spaces : ''
|
448
|
+
output += "<p>#{p.restore_spaces.cap_first.term('.')}#{fn}</p>\n\n"
|
382
449
|
end
|
383
450
|
output
|
384
451
|
end
|
@@ -33,10 +33,10 @@ module RandomWords
|
|
33
33
|
# 126620.to_word # => "one hundred twenty six thousand six hundred twenty"
|
34
34
|
def to_word(numbers)
|
35
35
|
tmp = self / 1000
|
36
|
-
final = (self % 1000).hundred_to_word(
|
36
|
+
final = (self % 1000).hundred_to_word(numbers, 2)
|
37
37
|
place = 3 # special-case the tens and below
|
38
38
|
until tmp.zero?
|
39
|
-
final = (tmp % 1000).hundred_to_word(
|
39
|
+
final = "#{(tmp % 1000).hundred_to_word(numbers, place)} #{final}"
|
40
40
|
place += 1
|
41
41
|
tmp /= 1000
|
42
42
|
end
|
@@ -54,7 +54,7 @@ module RandomWords
|
|
54
54
|
protected
|
55
55
|
|
56
56
|
# Convert a number to its word representation for the hundreds place.
|
57
|
-
def hundred_to_word(place = 0
|
57
|
+
def hundred_to_word(numbers, place = 0)
|
58
58
|
if zero?
|
59
59
|
''
|
60
60
|
elsif self < 10
|
@@ -83,7 +83,7 @@ module RandomWords
|
|
83
83
|
def append_place(word, place, numbers)
|
84
84
|
places = numbers[:places]
|
85
85
|
if place > 2
|
86
|
-
word
|
86
|
+
"#{word} #{places[place]}"
|
87
87
|
else
|
88
88
|
word
|
89
89
|
end
|
@@ -100,7 +100,7 @@ module RandomWords
|
|
100
100
|
tens = self / 10
|
101
101
|
ones = self % 10
|
102
102
|
ten = numbers[:tens][tens - 2]
|
103
|
-
ten
|
103
|
+
"#{ten} #{ones.zero? ? '' : ones.digit_to_word(numbers)}"
|
104
104
|
else
|
105
105
|
teen_to_word(numbers)
|
106
106
|
end
|
data/lib/random-words/numeric.rb
CHANGED
@@ -6,8 +6,8 @@ module RandomWords
|
|
6
6
|
def to_commas
|
7
7
|
num = to_s.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
|
8
8
|
dec = num.split('.')
|
9
|
-
num = dec[0]
|
9
|
+
num = "#{dec[0]}.#{dec[1].to_s.ljust(2, '0')[0, 2]}" if dec[1]
|
10
10
|
num
|
11
11
|
end
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
data/lib/random-words/string.rb
CHANGED
@@ -280,7 +280,7 @@ module RandomWords
|
|
280
280
|
# @return [String] The colorized text.
|
281
281
|
# @example
|
282
282
|
# colorize_text("Hello, World!", :red) # => "\e[31mHello, World!\e[0m"
|
283
|
-
def colorize_text(text, color, testing
|
283
|
+
def colorize_text(text, color, testing: false)
|
284
284
|
return text if !$stdout.isatty && !testing
|
285
285
|
|
286
286
|
return text unless colors.key?(color)
|
@@ -321,13 +321,20 @@ module RandomWords
|
|
321
321
|
|
322
322
|
# Expand abbreviated debug statements in the string.
|
323
323
|
# @return [String] The expanded debug string.
|
324
|
-
def expand_debug(testing
|
324
|
+
def expand_debug(testing: false)
|
325
325
|
gsub(/%(#{Regexp.union(expansions.keys)})%?/) do
|
326
326
|
match = Regexp.last_match
|
327
327
|
|
328
328
|
return match unless expansions.key?(match[1])
|
329
329
|
|
330
|
-
colorize_text("[#{expansions[match[1]][0] || match}]", expansions[match[1]][1] || :white, testing)
|
330
|
+
colorize_text("[#{expansions[match[1]][0] || match}]", expansions[match[1]][1] || :white, testing: testing)
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
def article_agree
|
335
|
+
gsub(/\ban? (\w)/i) do |match|
|
336
|
+
word = match[1]
|
337
|
+
/\A[aeiou]/i.match?(word) ? "an #{word}" : "a #{word}"
|
331
338
|
end
|
332
339
|
end
|
333
340
|
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RandomWords
|
4
|
+
# Terminal methods for testing
|
5
|
+
class Terminal
|
6
|
+
def initialize(source = :english)
|
7
|
+
# Create an instance of the generator
|
8
|
+
@sentence_generator = RandomWords::Generator.new(source)
|
9
|
+
@sentence_generator.use_extended_punctuation = true
|
10
|
+
@colors = {
|
11
|
+
text: :yellow,
|
12
|
+
counter: :boldcyan,
|
13
|
+
marker: :boldgreen,
|
14
|
+
bracket: :cyan,
|
15
|
+
language: :boldmagenta
|
16
|
+
}
|
17
|
+
@sources = @sentence_generator.sources.keys.map(&:to_s)
|
18
|
+
end
|
19
|
+
|
20
|
+
def colors
|
21
|
+
{
|
22
|
+
black: 30,
|
23
|
+
red: 31,
|
24
|
+
green: 32,
|
25
|
+
yellow: 33,
|
26
|
+
blue: 34,
|
27
|
+
magenta: 35,
|
28
|
+
cyan: 36,
|
29
|
+
white: 37,
|
30
|
+
boldblack: '1;30',
|
31
|
+
boldred: '1;31',
|
32
|
+
boldgreen: '1;32',
|
33
|
+
boldyellow: '1;33',
|
34
|
+
boldblue: '1;34',
|
35
|
+
boldmagenta: '1;35',
|
36
|
+
boldcyan: '1;36',
|
37
|
+
boldwhite: '1;37',
|
38
|
+
reset: 0
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def colorize_text(text, color)
|
43
|
+
return text unless $stdout.isatty
|
44
|
+
|
45
|
+
return text unless colors.key?(color)
|
46
|
+
|
47
|
+
color_code = colors[color]
|
48
|
+
|
49
|
+
"\e[#{color_code}m#{text}\e[0m"
|
50
|
+
end
|
51
|
+
|
52
|
+
def header1(text)
|
53
|
+
puts colorize_text("\n\n#{text}", :boldgreen)
|
54
|
+
puts colorize_text('=' * text.length, :boldgreen)
|
55
|
+
puts "\n"
|
56
|
+
end
|
57
|
+
|
58
|
+
def header2(text)
|
59
|
+
puts colorize_text("\n\n#{text}", :boldyellow)
|
60
|
+
puts colorize_text('-' * text.length, :boldyellow)
|
61
|
+
puts "\n"
|
62
|
+
end
|
63
|
+
|
64
|
+
def paragraphs(length, count = 3)
|
65
|
+
@sentence_generator.sentence_length = length
|
66
|
+
@sentence_generator.paragraph_length = count
|
67
|
+
@sentence_generator.source = @sources.sample
|
68
|
+
|
69
|
+
header2("Random Paragraph (#{@sentence_generator.paragraph_length} #{@sentence_generator.sentence_length} sentences)")
|
70
|
+
graf = @sentence_generator.paragraph
|
71
|
+
puts text(graf)
|
72
|
+
puts counter("#{graf.split(/ /).count} words, #{graf.length} characters")
|
73
|
+
end
|
74
|
+
|
75
|
+
def sentence(length)
|
76
|
+
header2("Random #{length} sentence")
|
77
|
+
@sentence_generator.sentence_length = length
|
78
|
+
s = @sentence_generator.sentence
|
79
|
+
puts text(s)
|
80
|
+
puts counter("#{s.split(/ /).count} words, #{s.length} characters")
|
81
|
+
end
|
82
|
+
|
83
|
+
# Generate and print random combined sentences
|
84
|
+
def combined_sentences(length = nil)
|
85
|
+
number_of_sentences = length || 2
|
86
|
+
|
87
|
+
header1('Random Combined Sentences:')
|
88
|
+
@sources.count.times do |i|
|
89
|
+
@sentence_generator.source = @sources[i - 1]
|
90
|
+
@sentence_generator.sentence_length = :medium
|
91
|
+
@sentence_generator.paragraph_length = number_of_sentences
|
92
|
+
@sentence_generator.sentences(number_of_sentences)
|
93
|
+
s = @sentence_generator.sentence
|
94
|
+
puts "#{marker} #{text(s)}"
|
95
|
+
puts counter("#{s.split(/ /).count} words, #{s.length} characters")
|
96
|
+
puts colorize_text(' ' * 2, :boldwhite)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def random_sentences
|
101
|
+
header1('Random Sentences')
|
102
|
+
@sentence_generator.lengths.keys.each_with_index do |length, index|
|
103
|
+
@sentence_generator.source = @sources[index]
|
104
|
+
sentence(length)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def random_paragraphs
|
109
|
+
header1('Random Paragraphs')
|
110
|
+
@sentence_generator.lengths.keys.each_with_index do |length, index|
|
111
|
+
@sentence_generator.source = @sources[index]
|
112
|
+
paragraphs(length, 3)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Generate and print specified number of words
|
117
|
+
def random_words
|
118
|
+
number_of_words = []
|
119
|
+
@sources.count.times do |i|
|
120
|
+
number_of_words << (((i * i) * 5) + 10)
|
121
|
+
end
|
122
|
+
|
123
|
+
header1("#{number_of_words} Random Words")
|
124
|
+
@sources.each_with_index do |source, index|
|
125
|
+
header2("#{number_of_words[index]} Random Words")
|
126
|
+
@sentence_generator.source = source
|
127
|
+
s = @sentence_generator.words(number_of_words[index])
|
128
|
+
puts "#{marker} #{text(s)} "
|
129
|
+
puts counter("#{s.split(/ /).count} words, #{s.length} characters")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Generate and print specified number of characters
|
134
|
+
def random_characters
|
135
|
+
header1('Random Characters (exact length)')
|
136
|
+
[20, 50, 120, 200, 500].each_with_index do |i, index|
|
137
|
+
@sentence_generator.source = @sources[index]
|
138
|
+
chars = @sentence_generator.characters(i, whole_words: true)
|
139
|
+
puts "#{marker} #{colorize_text("#{i}:", :boldwhite)} #{text(chars)} #{counter(chars.length)}"
|
140
|
+
end
|
141
|
+
|
142
|
+
# Generate and print specified number of characters
|
143
|
+
max_characters = [15, 25, 53, 110, 600]
|
144
|
+
min_characters = [10, 20, 50, 100, 500]
|
145
|
+
|
146
|
+
header1('Random Characters (length range)')
|
147
|
+
max_characters.count.times do |i|
|
148
|
+
@sentence_generator.source = @sources[i]
|
149
|
+
chars = @sentence_generator.characters(min_characters[i], max_characters[i], whole_words: true)
|
150
|
+
range = "#{marker} #{colorize_text("[#{min_characters[i]}-#{max_characters[i]}]: ", :boldwhite)}"
|
151
|
+
puts "#{range}#{text(chars)} #{counter(chars.length)}"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def marker
|
156
|
+
colorize_text('•', @colors[:marker])
|
157
|
+
end
|
158
|
+
|
159
|
+
def text(text)
|
160
|
+
colorize_text(text, @colors[:text])
|
161
|
+
end
|
162
|
+
|
163
|
+
def language
|
164
|
+
"#{colorize_text('(',
|
165
|
+
@colors[:bracket])}#{colorize_text(@sentence_generator.source,
|
166
|
+
@colors[:language])}#{colorize_text(')',
|
167
|
+
@colors[:bracket])}"
|
168
|
+
end
|
169
|
+
|
170
|
+
def counter(length)
|
171
|
+
"#{colorize_text('[',
|
172
|
+
@colors[:bracket])}#{colorize_text(length.to_s,
|
173
|
+
@colors[:counter])}#{colorize_text(']',
|
174
|
+
@colors[:bracket])} #{language}"
|
175
|
+
end
|
176
|
+
|
177
|
+
def print_test
|
178
|
+
combined_sentences(3)
|
179
|
+
random_sentences
|
180
|
+
random_paragraphs
|
181
|
+
random_words
|
182
|
+
random_characters
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
data/lib/random-words/version.rb
CHANGED