jmadlibs 0.8.5 → 0.8.6
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/lib/jmadlibs.rb +142 -28
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1aec2baf45a205e130f6e95d2bfecbb28a6bbccc
|
|
4
|
+
data.tar.gz: e63a3fb08afd81dd9fffdc3c65851b73bf3eca2a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c300dff0dc468dc74c05a20d4860365dac2fe1721bfd34abe5fe491eba2eba214196e6d82a15cc10dd499d4ad84572a7189e9599bf05c6a90f62d83f0e563775
|
|
7
|
+
data.tar.gz: 4d64216506bd0c1cea07a6f52221366492dd15836dc49fdb75006c538474ce3bb4684bfd8e12da42cbc0f5ea21531f3f5b22ae52375a4facc1db04147e70041f
|
data/lib/jmadlibs.rb
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
# > <wordlist%a> prepends a/an based on first letter of returned word
|
|
10
10
|
# > capitalised substitutions: <Wordlist>
|
|
11
11
|
#
|
|
12
|
-
#
|
|
12
|
+
# * joins between two lists: <lista+listb>
|
|
13
13
|
|
|
14
14
|
# All of the above can be combined and appear in the results of a parsing,
|
|
15
15
|
# so having any operator appear in a substitution word list is possible.
|
|
@@ -18,15 +18,19 @@
|
|
|
18
18
|
# * Currently, {{alt aa|alt ab}|{alt ba|alt bb}} will fail. Regex needs work.
|
|
19
19
|
# * add pluralisation: bike->bikes, boss->bosses, fox->foxes, etc. https://en.wikipedia.org/wiki/English_plurals
|
|
20
20
|
# * \ escaping of token signifiers. For instance, \[thing] treated as plaintext
|
|
21
|
+
# * Redo <lista+listb> parsing to use list lengths and random index generation rather than current concatenation of
|
|
22
|
+
# copies.
|
|
23
|
+
# * Look into allowing $0-9 specifiers per-list in <lista+listb> constructions
|
|
21
24
|
|
|
22
25
|
|
|
23
26
|
class JMadlibs
|
|
24
|
-
def initialize(pattern=nil, library=nil)
|
|
27
|
+
def initialize(pattern=nil, library=nil, variantString = "")
|
|
25
28
|
@loglevels = {0 => "NONE", 1 => "ERROR", 2 => "WARN", 3 => "INFO", 4 => "DEBUG", 5 => "ALL"}
|
|
26
29
|
@loglevel = 3
|
|
27
30
|
@rng = Random.new
|
|
28
31
|
setPattern(pattern)
|
|
29
32
|
setLibrary(library)
|
|
33
|
+
setVariants(variantString)
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
def log(msg, priority=5)
|
|
@@ -54,6 +58,10 @@ class JMadlibs
|
|
|
54
58
|
end
|
|
55
59
|
end
|
|
56
60
|
|
|
61
|
+
def setVariants(variantString)
|
|
62
|
+
@variants = variantString
|
|
63
|
+
end
|
|
64
|
+
|
|
57
65
|
def setLogLevel(loglevel)
|
|
58
66
|
if loglevel.is_a? String then loglevel = @loglevels.key(loglevel) end
|
|
59
67
|
@loglevel = loglevel
|
|
@@ -62,23 +70,23 @@ class JMadlibs
|
|
|
62
70
|
|
|
63
71
|
def loadList(filename)
|
|
64
72
|
if File.file?(filename)
|
|
65
|
-
|
|
73
|
+
log "Loading word lists from " + filename, "INFO"
|
|
66
74
|
@library = {}
|
|
67
75
|
currentList = ""
|
|
68
|
-
|
|
76
|
+
currentListContents = []
|
|
69
77
|
|
|
70
78
|
File.foreach(filename).with_index do |line|
|
|
71
79
|
line = line.strip
|
|
72
80
|
if line != "" and !line.start_with?('#')
|
|
73
81
|
matched = /^==(.+)==$/.match(line)
|
|
74
82
|
if !matched.nil? # new list identifier
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
83
|
+
if currentList != "" # save old list, if one exists.
|
|
84
|
+
addList(currentList, currentListContents)
|
|
85
|
+
end
|
|
78
86
|
currentList = matched[1] # update working targets for new list
|
|
79
87
|
currentListContents = []
|
|
80
88
|
elsif currentList == "" # we have no current list; this must be a pattern
|
|
81
|
-
|
|
89
|
+
setPattern(line)
|
|
82
90
|
else # word to be added to current list
|
|
83
91
|
currentListContents.push(line)
|
|
84
92
|
end
|
|
@@ -90,14 +98,72 @@ class JMadlibs
|
|
|
90
98
|
end
|
|
91
99
|
end
|
|
92
100
|
|
|
93
|
-
def anify(word) # If word begins with a vowel, prefix 'an', else prefix 'a'
|
|
101
|
+
def anify(word) # If word begins with a vowel, prefix 'an ', else prefix 'a '
|
|
94
102
|
if (/^[aeiou]/.match(word).nil?)
|
|
95
103
|
result = "a " + word
|
|
96
104
|
else
|
|
97
105
|
result = "an " + word
|
|
98
106
|
end
|
|
99
107
|
|
|
100
|
-
|
|
108
|
+
return result
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def getSpecified(word, specifier = nil) # get the correct representation of a word
|
|
112
|
+
#TODO: Convert ex. marry^marries^married^marrying to specified subword, return first if no specifier
|
|
113
|
+
|
|
114
|
+
# find default
|
|
115
|
+
options = word.count("\^")
|
|
116
|
+
|
|
117
|
+
if options == 0 # no variants exist
|
|
118
|
+
return word
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
default = word[0..word.index("^")-1]
|
|
122
|
+
|
|
123
|
+
if specifier.nil?
|
|
124
|
+
return default
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
variant = 0
|
|
128
|
+
|
|
129
|
+
if !specifier.index(/[0-9]/).nil?
|
|
130
|
+
variant = specifier.to_i
|
|
131
|
+
else
|
|
132
|
+
variant = @variants.index(specifier)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
if variant.nil?
|
|
136
|
+
log "Unknown variant identifier '" + specifier + "', using default.", 3
|
|
137
|
+
return default
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
start = 0
|
|
141
|
+
|
|
142
|
+
if variant >= options
|
|
143
|
+
log "Unknown variant identifier '" + variant.to_s + "', using default.", 3
|
|
144
|
+
return default
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
for i in 0..variant # find start of correct option
|
|
149
|
+
start = word.index("^", start+1)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
finish = word.index("^", start+1)
|
|
153
|
+
|
|
154
|
+
result = ""
|
|
155
|
+
|
|
156
|
+
if finish.nil?
|
|
157
|
+
result = word[start+1..-1]
|
|
158
|
+
else
|
|
159
|
+
result = word[start+1..finish-1]
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
if result.empty?
|
|
163
|
+
return default
|
|
164
|
+
else
|
|
165
|
+
return result
|
|
166
|
+
end
|
|
101
167
|
end
|
|
102
168
|
|
|
103
169
|
def pluralise(word)
|
|
@@ -108,24 +174,25 @@ class JMadlibs
|
|
|
108
174
|
# o -> oes
|
|
109
175
|
# (se?)(sh)(ch)
|
|
110
176
|
|
|
111
|
-
# this is a pretty big problem!
|
|
177
|
+
# this is a pretty big problem! POssibly solvable with getSpecified instead.
|
|
112
178
|
return word
|
|
113
179
|
end
|
|
114
180
|
|
|
115
181
|
def resolve_substitution(target)
|
|
116
182
|
up = false
|
|
117
|
-
|
|
183
|
+
specifiers = ""
|
|
118
184
|
|
|
119
185
|
# do we need to do post-processing?
|
|
120
|
-
post = target.rindex(/\$[
|
|
186
|
+
post = target.rindex(/\$[a-zA-Z0-9]+/)
|
|
121
187
|
|
|
122
188
|
if !post.nil?
|
|
123
|
-
|
|
189
|
+
specifiers = target.slice(post+1, target.length - post - 1)
|
|
190
|
+
variant = specifiers.scan(/[a-z0-9]/)
|
|
191
|
+
specifiers = specifiers.scan(/[A-Z]/)
|
|
124
192
|
target = target.slice(0, post)
|
|
125
193
|
end
|
|
126
194
|
|
|
127
|
-
|
|
128
|
-
if !(/^[A-Z]/.match(target).nil?)
|
|
195
|
+
if !(/^[A-Z]/.match(target).nil?) # do we need to capitalise?
|
|
129
196
|
target = target.downcase
|
|
130
197
|
up = true
|
|
131
198
|
end
|
|
@@ -134,39 +201,86 @@ class JMadlibs
|
|
|
134
201
|
mult = target.index /\+/
|
|
135
202
|
|
|
136
203
|
if mult.nil? # only one list
|
|
137
|
-
|
|
204
|
+
if @library[target].nil?
|
|
205
|
+
log "Missing wordlist: " + target, 2
|
|
206
|
+
return "MISSING"
|
|
207
|
+
end
|
|
138
208
|
result = parse_pattern(@library[target].sample(random: @rng))
|
|
139
209
|
|
|
140
210
|
else # more than one list
|
|
141
211
|
multlist = []
|
|
142
212
|
listnames = []
|
|
143
213
|
|
|
214
|
+
if false # testing new method
|
|
215
|
+
|
|
144
216
|
# as long as we still have alternatives, keep adding them to listnames
|
|
145
|
-
|
|
217
|
+
while !mult.nil?
|
|
146
218
|
listnames << target.slice(0, mult)
|
|
147
|
-
|
|
219
|
+
target = target.slice(mult+1, target.length - mult - 1)
|
|
148
220
|
mult = target.index /\+/
|
|
149
221
|
end
|
|
150
|
-
|
|
222
|
+
listnames << target # append final alternative
|
|
151
223
|
|
|
152
224
|
# combine lists for sampling
|
|
153
225
|
listnames.each do |list|
|
|
154
|
-
|
|
226
|
+
if !@library[list].nil? then multlist += @library[list] end
|
|
155
227
|
end
|
|
156
228
|
|
|
157
|
-
|
|
229
|
+
if multlist.length == 0 # no valid options found
|
|
230
|
+
log "Missing wordlist: " + target, 2
|
|
231
|
+
return "MISSING"
|
|
232
|
+
end
|
|
158
233
|
result = parse_pattern(multlist.sample(random: @rng))
|
|
234
|
+
|
|
235
|
+
else # Test of new routine for sampling multiple lists
|
|
236
|
+
while !mult.nil?
|
|
237
|
+
listnames << target.slice(0, mult)
|
|
238
|
+
target = target.slice(mult+1, target.length - mult - 1)
|
|
239
|
+
mult = target.index /\+/
|
|
240
|
+
end
|
|
241
|
+
listnames << target # append final alternative
|
|
242
|
+
|
|
243
|
+
# Find total count of options
|
|
244
|
+
max = 0
|
|
245
|
+
|
|
246
|
+
listnames.each do |list|
|
|
247
|
+
if !@library[list].nil? then max += @library[list].length end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Pick index within overall size
|
|
251
|
+
index = @rng.rand(max)
|
|
252
|
+
|
|
253
|
+
# iterate through lists until we find the list the index fits into
|
|
254
|
+
listnames.each do |list|
|
|
255
|
+
if index >= @library[list].length # Not in this list, so discard its indices
|
|
256
|
+
index -= @library[list].length
|
|
257
|
+
else
|
|
258
|
+
result = parse_pattern(@library[list][index]) # in this list; return index
|
|
259
|
+
break
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
159
263
|
end
|
|
160
264
|
|
|
161
|
-
#
|
|
162
|
-
|
|
163
|
-
|
|
265
|
+
# Dealing with possible variant suffixes
|
|
266
|
+
|
|
267
|
+
if result.count("\^") > 0 # if we have options...
|
|
268
|
+
if !variant.nil? and variant.length > 0 # get variant if it exists
|
|
269
|
+
variant = variant.slice!(0)
|
|
270
|
+
result = getSpecified(result, variant)
|
|
271
|
+
else # get the default
|
|
272
|
+
result = getSpecified(result)
|
|
273
|
+
end
|
|
164
274
|
end
|
|
165
|
-
|
|
275
|
+
|
|
276
|
+
# do post-processing
|
|
277
|
+
|
|
278
|
+
if specifiers.include? "A"
|
|
166
279
|
result = anify(result)
|
|
167
280
|
end
|
|
168
|
-
|
|
169
|
-
|
|
281
|
+
|
|
282
|
+
if specifiers.include? "U"
|
|
283
|
+
result[0] = result[0].upcase
|
|
170
284
|
end
|
|
171
285
|
|
|
172
286
|
return result
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jmadlibs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gareth Morgan
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
11
|
+
date: 2017-08-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A madlibs engine with several powerful features
|
|
14
14
|
email: jacelium@gmail.com
|