poefy 0.6.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rspec +0 -1
- data/README.md +185 -37
- data/bin/poefy +88 -56
- data/bin/poefy_make +93 -0
- data/lib/poefy/conditional_sample.rb +85 -0
- data/lib/poefy/core_extensions/array.rb +78 -39
- data/lib/poefy/database.rb +53 -176
- data/lib/poefy/db_type.rb +64 -0
- data/lib/poefy/generation.rb +5 -27
- data/lib/poefy/handle_error.rb +9 -1
- data/lib/poefy/{poefy_gen_base.rb → poem_base.rb} +17 -37
- data/lib/poefy/poetic_form_from_text.rb +1 -1
- data/lib/poefy/poetic_forms.rb +0 -1
- data/lib/poefy/self.rb +39 -21
- data/lib/poefy/string_manipulation.rb +1 -1
- data/lib/poefy/version.rb +26 -29
- data/lib/poefy.rb +10 -15
- data/poefy.gemspec +16 -5
- data/spec/poefy_unit_spec.rb +199 -0
- data/spec/spec_helper.rb +2 -0
- metadata +53 -14
- data/data/spec_test_tiny.txt +0 -12
- data/lib/poefy/conditional_satisfaction.rb +0 -226
- data/spec/poefy_spec.rb +0 -722
data/spec/poefy_spec.rb
DELETED
@@ -1,722 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: UTF-8
|
3
|
-
|
4
|
-
################################################################################
|
5
|
-
|
6
|
-
describe Poefy::PoefyGen do
|
7
|
-
|
8
|
-
before(:all) do
|
9
|
-
@root = File.expand_path('../../', __FILE__)
|
10
|
-
db_file = "#{@root}/data/spec_test_tiny.db"
|
11
|
-
File.delete(db_file) if File.exists?(db_file)
|
12
|
-
end
|
13
|
-
after(:all) do
|
14
|
-
db_file = "#{@root}/data/spec_test_tiny.db"
|
15
|
-
File.delete(db_file) if File.exists?(db_file)
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "using tiny dataset spec_test_tiny.db / spec_test_tiny.txt" do
|
19
|
-
|
20
|
-
file_txt = "spec_test_tiny.txt"
|
21
|
-
file_db = "spec_test_tiny.db"
|
22
|
-
|
23
|
-
before(:each) do
|
24
|
-
@poefy = Poefy::PoefyGen.new(file_db, { proper: false })
|
25
|
-
end
|
26
|
-
after(:each) do
|
27
|
-
@poefy.close
|
28
|
-
end
|
29
|
-
it "initialised object not nil" do
|
30
|
-
expect(@poefy).to_not be_nil
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
|
34
|
-
it "should make the database '#{@root}/data/#{file_db}" do
|
35
|
-
db_file = "#{@root}/data/#{file_db}"
|
36
|
-
@poefy.make_database "#{@root}/data/#{file_txt}", true
|
37
|
-
expect(@poefy.db.exists?).to be true
|
38
|
-
expect(File.exists?(db_file)).to be true
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe ":rhyme option" do
|
43
|
-
|
44
|
-
describe "should return nil" do
|
45
|
-
it "blank, no argument" do
|
46
|
-
poem = @poefy.poem
|
47
|
-
expect(poem).to be_nil
|
48
|
-
end
|
49
|
-
it "({ })" do
|
50
|
-
poem = @poefy.poem ({ })
|
51
|
-
expect(poem).to be_nil
|
52
|
-
end
|
53
|
-
it "({ rhyme: nil })" do
|
54
|
-
poem = @poefy.poem ({ rhyme: nil })
|
55
|
-
expect(poem).to be_nil
|
56
|
-
end
|
57
|
-
it "({ rhyme: ' ' })" do
|
58
|
-
poem = @poefy.poem ({ rhyme: ' ' })
|
59
|
-
expect(poem).to be_nil
|
60
|
-
end
|
61
|
-
it "({ rhyme: '' })" do
|
62
|
-
poem = @poefy.poem ({ rhyme: '' })
|
63
|
-
expect(poem).to be_nil
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe "should return correct number of lines" do
|
68
|
-
rhymes = %w{a b z A aa ab zz AA AB AA1 A1 B1 Z1 AB1 A1A1A1A1B1B1B1B1B1}
|
69
|
-
rhymes += ['A1A1A1 A1A1A1 B1B1B1B1B1B1','a b c a b c']
|
70
|
-
rhymes += [' abc','abc ',' abc ']
|
71
|
-
rhymes += ['n aaa n','n aXXXa N1']
|
72
|
-
rhymes.each do |i|
|
73
|
-
it "({ rhyme: '#{i}' })" do
|
74
|
-
poem = @poefy.poem ({ rhyme: i })
|
75
|
-
expect(poem.count).to be i.gsub(/[0-9]/,'').length
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe "should accept characters other than number" do
|
81
|
-
rhymes = %w{. , : .. ., ,, :: (()) @ ~ <<>< A1A1A1...a;}
|
82
|
-
rhymes.each do |i|
|
83
|
-
it "({ rhyme: '#{i}' })" do
|
84
|
-
poem = @poefy.poem ({ rhyme: i })
|
85
|
-
expect(poem.count).to be i.gsub(/[0-9]/,'').length
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "should be nil if can't parse rhyme string" do
|
91
|
-
rhymes = %w{a1 b1 ab1 Ab1 AAAAABb1 1 1111 1122 11221 ;;::1. }
|
92
|
-
rhymes += ['AA Bb1','11 11','11 1 1','..1.']
|
93
|
-
rhymes.each do |i|
|
94
|
-
it "({ rhyme: '#{i}' })" do
|
95
|
-
poem = @poefy.poem ({ rhyme: i })
|
96
|
-
expect(poem).to be_nil
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "should be nil if can't complete rhyme string" do
|
102
|
-
rhymes = %w{aaaaaa abcd aaaaabbbbb}
|
103
|
-
rhymes.each do |i|
|
104
|
-
it "({ rhyme: '#{i}' })" do
|
105
|
-
poem = @poefy.poem ({ rhyme: i })
|
106
|
-
expect(poem).to be_nil
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
describe "should correctly repeat uppercase lines" do
|
112
|
-
lines = 200
|
113
|
-
it "({ rhyme: 'A' * #{lines} })" do
|
114
|
-
poem = @poefy.poem ({ rhyme: 'A' * lines })
|
115
|
-
expect(poem.count).to be lines
|
116
|
-
expect(poem.uniq.count).to be 1
|
117
|
-
end
|
118
|
-
it "({ rhyme: ('A'..'C').to_a.map { |i| i * #{lines} }.join })" do
|
119
|
-
rhyme = ('A'..'C').to_a.map { |i| i * lines }.join
|
120
|
-
poem = @poefy.poem ({ rhyme: rhyme })
|
121
|
-
expect(poem.count).to be lines * 3
|
122
|
-
expect(poem.uniq.count).to be 3
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
describe "should be nil if can't complete repeating rhyme string" do
|
127
|
-
lines = 200
|
128
|
-
it "({ rhyme: ('A'..'D').to_a.map { |i| i * #{lines} }.join })" do
|
129
|
-
rhyme = ('A'..'D').to_a.map { |i| i * lines }.join
|
130
|
-
poem = @poefy.poem ({ rhyme: rhyme })
|
131
|
-
expect(poem).to be_nil
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
describe ":form option" do
|
138
|
-
|
139
|
-
describe "should return correct number of lines" do
|
140
|
-
it "({ form: :default })" do
|
141
|
-
poem = @poefy.poem ({ form: :default })
|
142
|
-
expect(poem.count).to be 1
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
describe "should be nil if given a named form it can't fulfil" do
|
147
|
-
it "({ form: 'sonnet' })" do
|
148
|
-
poem = @poefy.poem ({ form: 'sonnet' })
|
149
|
-
expect(poem).to be_nil
|
150
|
-
end
|
151
|
-
it "({ form: :villanelle })" do
|
152
|
-
poem = @poefy.poem ({ form: :villanelle })
|
153
|
-
expect(poem).to be_nil
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
describe "should be nil if given a junk named form" do
|
158
|
-
it "({ form: 'sonnet_junk' })" do
|
159
|
-
poem = @poefy.poem ({ form: 'sonnet_junk' })
|
160
|
-
expect(poem).to be_nil
|
161
|
-
end
|
162
|
-
it "({ form: :not_a_form })" do
|
163
|
-
poem = @poefy.poem ({ form: :not_a_form })
|
164
|
-
expect(poem).to be_nil
|
165
|
-
end
|
166
|
-
it "({ form: :not_a_form, indent: '0010' })" do
|
167
|
-
poem = @poefy.poem ({ form: :not_a_form, indent: '0010' })
|
168
|
-
expect(poem).to be_nil
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
describe "should be valid if given a junk named form, and a rhyme" do
|
173
|
-
it "({ form: :not_a_form, rhyme: 'abcb' })" do
|
174
|
-
poem = @poefy.poem ({ form: :not_a_form, rhyme: 'abcb' })
|
175
|
-
expect(poem.count).to be 4
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
describe "should overwrite a named form if another option is specified" do
|
180
|
-
it "({ form: 'default', rhyme: 'ab' })" do
|
181
|
-
poem = @poefy.poem ({ form: 'default', rhyme: 'ab' })
|
182
|
-
expect(poem.count).to be 2
|
183
|
-
end
|
184
|
-
it "({ form: :villanelle, rhyme: 'abcb' })" do
|
185
|
-
poem = @poefy.poem ({ form: :villanelle, rhyme: 'abcb' })
|
186
|
-
expect(poem.count).to be 4
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
##############################################################################
|
193
|
-
|
194
|
-
describe "using dataset shakespeare.db / shakespeare_sonnets.txt" do
|
195
|
-
|
196
|
-
file_txt = "shakespeare_sonnets.txt"
|
197
|
-
file_db = "shakespeare.db"
|
198
|
-
|
199
|
-
# All the Shakespeare lines are pentameter, so some forms should fail.
|
200
|
-
forms = Poefy::PoeticForms::POETIC_FORMS
|
201
|
-
forms_fail = [:limerick, :haiku, :common, :ballad, :double_dactyl]
|
202
|
-
forms_pass = forms.keys - forms_fail
|
203
|
-
|
204
|
-
before(:each) do
|
205
|
-
@poefy = Poefy::PoefyGen.new(file_db, { proper: false })
|
206
|
-
end
|
207
|
-
after(:each) do
|
208
|
-
@poefy.close
|
209
|
-
end
|
210
|
-
|
211
|
-
it "initialised object not nil" do
|
212
|
-
expect(@poefy).to_not be_nil
|
213
|
-
end
|
214
|
-
|
215
|
-
describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
|
216
|
-
it "should make the database '#{@root}/data/#{file_db}" do
|
217
|
-
db_file = "#{@root}/data/#{file_db}"
|
218
|
-
input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
|
219
|
-
@poefy.make_database input
|
220
|
-
expect(@poefy.db.exists?).to be true
|
221
|
-
expect(File.exists?(db_file)).to be true
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
describe "using acrostic option" do
|
226
|
-
describe "should return correct number of lines" do
|
227
|
-
it "({ form: :sonnet, acrostic: 'pauldpthompson' })" do
|
228
|
-
poem = @poefy.poem ({ form: :sonnet,
|
229
|
-
acrostic: 'pauldpthompson' })
|
230
|
-
expect(poem.count).to be 14
|
231
|
-
end
|
232
|
-
end
|
233
|
-
describe "should fail to be created" do
|
234
|
-
it "({ form: :sonnet, acrostic: 'qqqqqqqqqqqqqq' })" do
|
235
|
-
poem = @poefy.poem ({ form: :sonnet,
|
236
|
-
acrostic: 'qqqqqqqqqqqqqq' })
|
237
|
-
expect(poem).to be_nil
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
describe "using form string" do
|
243
|
-
describe "should return correct number of lines" do
|
244
|
-
|
245
|
-
# Make sure each form's lines match the expected output.
|
246
|
-
# Generate a few to be sure.
|
247
|
-
forms_pass.each do |form|
|
248
|
-
it "({ form: #{form} })" do
|
249
|
-
10.times do
|
250
|
-
poem = @poefy.poem ({ form: form })
|
251
|
-
expect(poem.count).to satisfy do |c|
|
252
|
-
[*forms[form][:rhyme]].map do |r|
|
253
|
-
r.gsub(/[0-9]/,'').length
|
254
|
-
end.include?(c)
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
describe "should fail to be created" do
|
262
|
-
forms_fail.each do |form|
|
263
|
-
it "({ form: #{form} })" do
|
264
|
-
4.times do
|
265
|
-
poem = @poefy.poem ({ form: form })
|
266
|
-
expect(poem).to be_nil
|
267
|
-
end
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
describe "make sonnets" do
|
274
|
-
sonnet_options = [
|
275
|
-
{ rhyme: 'ababcdcdefefgg' },
|
276
|
-
{ rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
|
277
|
-
{ form: 'sonnet' },
|
278
|
-
{ form: :sonnet, syllable: 0 },
|
279
|
-
{ form: :sonnet, syllable: 10 },
|
280
|
-
{ form: :sonnet, regex: /^[A-Z].*$/ },
|
281
|
-
{ form: :sonnet, regex: '^[A-Z].*$' },
|
282
|
-
{ form: :sonnet, acrostic: 'pauldpthompson' },
|
283
|
-
{ form: 'sonnet', indent: '01010101001101' },
|
284
|
-
{ form: 'sonnet', proper: false }
|
285
|
-
]
|
286
|
-
sonnet_options.each do |option|
|
287
|
-
it "#{option}" do
|
288
|
-
4.times do
|
289
|
-
poem = @poefy.poem(option)
|
290
|
-
expect(poem).to_not be_nil
|
291
|
-
end
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
##############################################################################
|
298
|
-
|
299
|
-
describe "using dataset whitman.db / whitman_leaves.txt" do
|
300
|
-
|
301
|
-
file_txt = "whitman_leaves.txt"
|
302
|
-
file_db = "whitman.db"
|
303
|
-
|
304
|
-
# There's a good mix of syllable count, so all forms should pass.
|
305
|
-
forms = Poefy::PoeticForms::POETIC_FORMS
|
306
|
-
forms_pass = forms.keys
|
307
|
-
|
308
|
-
before(:each) do
|
309
|
-
@poefy = Poefy::PoefyGen.new(file_db, { proper: false })
|
310
|
-
end
|
311
|
-
after(:each) do
|
312
|
-
@poefy.close
|
313
|
-
end
|
314
|
-
|
315
|
-
it "initialised object not nil" do
|
316
|
-
expect(@poefy).to_not be_nil
|
317
|
-
end
|
318
|
-
|
319
|
-
describe "#make_database( '#{@root}/data/#{file_txt}', true )" do
|
320
|
-
it "should make the database '#{@root}/data/#{file_db}" do
|
321
|
-
db_file = "#{@root}/data/#{file_db}"
|
322
|
-
input = `sed '/[a-z]/!d' #{@root}/data/#{file_txt}`
|
323
|
-
@poefy.make_database input
|
324
|
-
expect(@poefy.db.exists?).to be true
|
325
|
-
expect(File.exists?(db_file)).to be true
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
describe "using form string" do
|
330
|
-
describe "should return correct number of lines" do
|
331
|
-
|
332
|
-
# Make sure each form's lines match the expected output.
|
333
|
-
# Generate a few to be sure.
|
334
|
-
forms_pass.each do |form|
|
335
|
-
it "({ form: #{form} })" do
|
336
|
-
10.times do
|
337
|
-
poem = @poefy.poem ({ form: form })
|
338
|
-
expect(poem.count).to satisfy do |c|
|
339
|
-
[*forms[form][:rhyme]].map do |r|
|
340
|
-
r.gsub(/[0-9]/,'').length
|
341
|
-
end.include?(c)
|
342
|
-
end
|
343
|
-
end
|
344
|
-
end
|
345
|
-
end
|
346
|
-
end
|
347
|
-
end
|
348
|
-
|
349
|
-
describe "make sonnets" do
|
350
|
-
sonnet_options = [
|
351
|
-
{ rhyme: 'ababcdcdefefgg' },
|
352
|
-
{ rhyme: 'abab cdcd efef gg', indent: '0101 0101 0011 01' },
|
353
|
-
{ form: 'sonnet' },
|
354
|
-
{ form: :sonnet, syllable: 0 },
|
355
|
-
{ form: :sonnet, syllable: 10 },
|
356
|
-
{ form: :sonnet, regex: /^[A-Z].*$/ },
|
357
|
-
{ form: :sonnet, regex: '^[A-Z].*$' },
|
358
|
-
{ form: :sonnet, acrostic: 'pauldpthompson' },
|
359
|
-
{ form: 'sonnet', indent: '01010101001101' },
|
360
|
-
{ form: 'sonnet', proper: false }
|
361
|
-
]
|
362
|
-
sonnet_options.each do |option|
|
363
|
-
it "#{option}" do
|
364
|
-
4.times do
|
365
|
-
poem = @poefy.poem(option)
|
366
|
-
expect(poem).to_not be_nil
|
367
|
-
end
|
368
|
-
end
|
369
|
-
end
|
370
|
-
end
|
371
|
-
|
372
|
-
describe "using syllable string" do
|
373
|
-
|
374
|
-
it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,0,8,6,8,6]' })" do
|
375
|
-
options = {
|
376
|
-
rhyme: 'abcb defe',
|
377
|
-
syllable: '[8,6,8,6,0,8,6,8,6]'
|
378
|
-
}
|
379
|
-
poem = @poefy.poem (options)
|
380
|
-
expect(poem.count).to be options[:rhyme].length
|
381
|
-
end
|
382
|
-
|
383
|
-
it "({ rhyme: 'abcb defe', syllable: '[8,6,8,6,8,6,8,6]' })" do
|
384
|
-
options = {
|
385
|
-
rhyme: 'abcb defe',
|
386
|
-
syllable: '[8,6,8,6,8,6,8,6]'
|
387
|
-
}
|
388
|
-
poem = @poefy.poem (options)
|
389
|
-
expect(poem.count).to be options[:rhyme].length
|
390
|
-
end
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
##############################################################################
|
395
|
-
|
396
|
-
describe "#transform_string_syllable" do
|
397
|
-
|
398
|
-
# Singleton which includes the method.
|
399
|
-
# Make the private methods public.
|
400
|
-
let(:obj) do
|
401
|
-
class Sing
|
402
|
-
include Poefy::PoeticForms
|
403
|
-
include Poefy::StringManipulation
|
404
|
-
public *private_instance_methods
|
405
|
-
end.new
|
406
|
-
end
|
407
|
-
describe "using rhyme string 'aabba'" do
|
408
|
-
input_and_output = [
|
409
|
-
['10',
|
410
|
-
{1=>10,2=>10,3=>10,4=>10,5=>10}],
|
411
|
-
['9,10,11',
|
412
|
-
{1=>[9,10,11],2=>[9,10,11],3=>[9,10,11],4=>[9,10,11],5=>[9,10,11]}],
|
413
|
-
['[8,8,5,5,8]',
|
414
|
-
{1=>8,2=>8,3=>5,4=>5,5=>8}],
|
415
|
-
['[[8,9],[8,9],[4,5,6],[4,5,6],[8,9]]',
|
416
|
-
{1=>[8,9],2=>[8,9],3=>[4,5,6],4=>[4,5,6],5=>[8,9]}],
|
417
|
-
['{1:8,2:8,3:5,4:5,5:8}',
|
418
|
-
{1=>8,2=>8,3=>5,4=>5,5=>8}],
|
419
|
-
['{1:8,2:8,3:5,5:8}',
|
420
|
-
{1=>8,2=>8,3=>5,4=>0,5=>8}],
|
421
|
-
['{0:99,1:8,2:8,3:5,5:8}',
|
422
|
-
{1=>8,2=>8,3=>5,4=>99,5=>8}],
|
423
|
-
['{1:[8,9],2:[8,9],3:[4,5,6],4:[4,5,6],5:[8,9]}',
|
424
|
-
{1=>[8,9],2=>[8,9],3=>[4,5,6],4=>[4,5,6],5=>[8,9]}],
|
425
|
-
['{1:[8,9],2:[8,9],3:[4,5,6],5:[8,9]}',
|
426
|
-
{1=>[8,9],2=>[8,9],3=>[4,5,6],4=>0,5=>[8,9]}],
|
427
|
-
['{0:99,1:[8,9],2:[8,9],3:[4,5,6],5:[8,9]}',
|
428
|
-
{1=>[8,9],2=>[8,9],3=>[4,5,6],4=>99,5=>[8,9]}],
|
429
|
-
['{0:[8,9],3:[4,5,6],4:[4,5,6]}',
|
430
|
-
{1=>[8,9],2=>[8,9],3=>[4,5,6],4=>[4,5,6],5=>[8,9]}],
|
431
|
-
['{1:8,5:8}',
|
432
|
-
{1=>8,2=>0,3=>0,4=>0,5=>8}],
|
433
|
-
['{1:8,2:8,3:5,-2:5,-1:8}',
|
434
|
-
{1=>8,2=>8,3=>5,4=>5,5=>8}]
|
435
|
-
]
|
436
|
-
input_and_output.each do |pair|
|
437
|
-
it "syllable: #{pair.first}" do
|
438
|
-
rhyme = obj.tokenise_rhyme('aabba')
|
439
|
-
out = obj.transform_string_syllable(pair.first, 'aabba')
|
440
|
-
again = obj.transform_string_syllable(out, 'aabba')
|
441
|
-
expect(out).to eq pair.last
|
442
|
-
expect(again).to eq out
|
443
|
-
expect(again).to eq pair.last
|
444
|
-
end
|
445
|
-
end
|
446
|
-
end
|
447
|
-
end
|
448
|
-
|
449
|
-
##############################################################################
|
450
|
-
|
451
|
-
describe "#transform_string_regex" do
|
452
|
-
|
453
|
-
# Singleton which includes the method.
|
454
|
-
# Make the private methods public.
|
455
|
-
let(:obj) do
|
456
|
-
class Sing
|
457
|
-
include Poefy::PoeticForms
|
458
|
-
include Poefy::StringManipulation
|
459
|
-
public *private_instance_methods
|
460
|
-
end.new
|
461
|
-
end
|
462
|
-
|
463
|
-
describe "using rhyme string 'aabba'" do
|
464
|
-
input_and_output = [
|
465
|
-
['^[^e]*$',
|
466
|
-
{
|
467
|
-
1=>/^[^e]*$/,
|
468
|
-
2=>/^[^e]*$/,
|
469
|
-
3=>/^[^e]*$/,
|
470
|
-
4=>/^[^e]*$/,
|
471
|
-
5=>/^[^e]*$/
|
472
|
-
}],
|
473
|
-
['[/(?=^[A-Z])(?=^[^eE]*$)/,/^[^eE]*$/,/^[^eE]*$/,/^[^eE]*$/,/^[^eE]*$/]',
|
474
|
-
{
|
475
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
476
|
-
2=>/^[^eE]*$/,
|
477
|
-
3=>/^[^eE]*$/,
|
478
|
-
4=>/^[^eE]*$/,
|
479
|
-
5=>/^[^eE]*$/
|
480
|
-
}],
|
481
|
-
['{1=>/(?=^[A-Z])(?=^[^eE]*$)/,2=>/^[^eE]*$/,3=>/^[^eE]*$/,4=>/^[^eE]*$/,5=>/^[^eE]*$/}',
|
482
|
-
{
|
483
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
484
|
-
2=>/^[^eE]*$/,
|
485
|
-
3=>/^[^eE]*$/,
|
486
|
-
4=>/^[^eE]*$/,
|
487
|
-
5=>/^[^eE]*$/
|
488
|
-
}],
|
489
|
-
['{0=>/^[^eE]*$/,1=>/(?=^[A-Z])(?=^[^eE]*$)/}',
|
490
|
-
{
|
491
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
492
|
-
2=>/^[^eE]*$/,
|
493
|
-
3=>/^[^eE]*$/,
|
494
|
-
4=>/^[^eE]*$/,
|
495
|
-
5=>/^[^eE]*$/
|
496
|
-
}],
|
497
|
-
['{1=>/(?=^[A-Z])(?=^[^eE]*$)/,2=>/^[^eE]*$/,3=>/^[^eE]*$/,5=>/^[^eE]*$/}',
|
498
|
-
{
|
499
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
500
|
-
2=>/^[^eE]*$/,
|
501
|
-
3=>/^[^eE]*$/,
|
502
|
-
4=>nil,
|
503
|
-
5=>/^[^eE]*$/
|
504
|
-
}],
|
505
|
-
['{1=>/(?=^[A-Z])(?=^[^eE]*$)/,4=>/^[^eE]*$/}',
|
506
|
-
{
|
507
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
508
|
-
2=>nil,
|
509
|
-
3=>nil,
|
510
|
-
4=>/^[^eE]*$/,
|
511
|
-
5=>nil
|
512
|
-
}],
|
513
|
-
['{1=>/(?=^[A-Z])(?=^[^eE]*$)/,2=>/^[^eE]*$/,3=>/^[^eE]*$/,-1=>/^[^eE]*$/,-2=>/^[^eE]*$/}',
|
514
|
-
{
|
515
|
-
1=>/(?=^[A-Z])(?=^[^eE]*$)/,
|
516
|
-
2=>/^[^eE]*$/,
|
517
|
-
3=>/^[^eE]*$/,
|
518
|
-
4=>/^[^eE]*$/,
|
519
|
-
5=>/^[^eE]*$/
|
520
|
-
}]
|
521
|
-
]
|
522
|
-
input_and_output.each do |pair|
|
523
|
-
it "regex: #{pair.first}" do
|
524
|
-
out = obj.transform_string_regex(pair.first, 'aabba')
|
525
|
-
again = obj.transform_string_regex(out, 'aabba')
|
526
|
-
expect(out).to eq pair.last
|
527
|
-
expect(again).to eq out
|
528
|
-
expect(again).to eq pair.last
|
529
|
-
end
|
530
|
-
end
|
531
|
-
end
|
532
|
-
end
|
533
|
-
|
534
|
-
##############################################################################
|
535
|
-
|
536
|
-
describe "reusing the same PoefyGen instance" do
|
537
|
-
it "should correctly merge the option hashes" do
|
538
|
-
|
539
|
-
# Default to use rondeau poetic form, and proper sentence validation
|
540
|
-
poefy = Poefy::PoefyGen.new('shakespeare', { form: 'rondeau', proper: true })
|
541
|
-
|
542
|
-
# Generate a properly sentenced rondeau
|
543
|
-
poem = poefy.poem
|
544
|
-
expect(poem.count).to be 17
|
545
|
-
|
546
|
-
# Generate a rondeau without proper validation
|
547
|
-
poem = poefy.poem ({ proper: false })
|
548
|
-
expect(poem.count).to be 17
|
549
|
-
|
550
|
-
# Generate a proper rondeau with a certain indentation
|
551
|
-
poem = poefy.poem ({ indent: '01012 0012 010112' })
|
552
|
-
expect(poem.count).to be 17
|
553
|
-
|
554
|
-
# Generate other forms
|
555
|
-
poem = poefy.poem ({ rhyme: 'abbaabbacdecde' })
|
556
|
-
expect(poem.count).to be 14
|
557
|
-
poem = poefy.poem ({ form: 'sonnet' })
|
558
|
-
expect(poem.count).to be 14
|
559
|
-
poem = poefy.poem ({ form: 'ballade' })
|
560
|
-
expect(poem.count).to be 31
|
561
|
-
|
562
|
-
# Generate a default rondeau again
|
563
|
-
poem = poefy.poem
|
564
|
-
expect(poem.count).to be 17
|
565
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
##############################################################################
|
569
|
-
|
570
|
-
describe "using the transform option" do
|
571
|
-
|
572
|
-
it "should correctly transform the output 1" do
|
573
|
-
poefy = Poefy::PoefyGen.new :shakespeare
|
574
|
-
transform_hash = {
|
575
|
-
4 => proc { |line, num, poem| line.upcase },
|
576
|
-
12 => proc { |line, num, poem| line.upcase }
|
577
|
-
}
|
578
|
-
poem = poefy.poem({ form: :sonnet, transform: transform_hash })
|
579
|
-
expect(poem.count).to be 14
|
580
|
-
expect(poem[3]).to eq poem[3].upcase
|
581
|
-
expect(poem[11]).to eq poem[11].upcase
|
582
|
-
end
|
583
|
-
|
584
|
-
it "should correctly transform the output 2" do
|
585
|
-
poefy = Poefy::PoefyGen.new :shakespeare
|
586
|
-
transform_hash = {
|
587
|
-
4 => proc { |line, num, poem| poem.count },
|
588
|
-
-3 => proc { |line, num, poem| poem.count },
|
589
|
-
7 => proc { |line, num, poem| 'test string' }
|
590
|
-
}
|
591
|
-
poem = poefy.poem({ form: :sonnet, transform: transform_hash })
|
592
|
-
expect(poem.count).to be 14
|
593
|
-
expect(poem[3]).to eq '14'
|
594
|
-
expect(poem[11]).to eq '14'
|
595
|
-
expect(poem[6]).to eq 'test string'
|
596
|
-
end
|
597
|
-
|
598
|
-
it "should correctly transform the output 3" do
|
599
|
-
poefy = Poefy::PoefyGen.new :shakespeare
|
600
|
-
transform_proc = proc { |line, num, poem| line.downcase }
|
601
|
-
poem = poefy.poem({ form: :sonnet, transform: transform_proc })
|
602
|
-
expect(poem.count).to be 14
|
603
|
-
poem.each do |i|
|
604
|
-
expect(i).to eq i.downcase
|
605
|
-
end
|
606
|
-
end
|
607
|
-
|
608
|
-
it "should correctly transform the output 4" do
|
609
|
-
poefy = Poefy::PoefyGen.new :shakespeare
|
610
|
-
transform_proc = proc { |line, num, poem| "#{num} #{line.downcase}" }
|
611
|
-
poem = poefy.poem({ form: :sonnet, transform: transform_proc })
|
612
|
-
expect(poem.count).to be 14
|
613
|
-
poem.each.with_index do |line, index|
|
614
|
-
expect(line).to eq line.downcase
|
615
|
-
first_word = line.split(' ').first
|
616
|
-
expect(first_word).to eq (index + 1).to_s
|
617
|
-
end
|
618
|
-
end
|
619
|
-
end
|
620
|
-
|
621
|
-
##############################################################################
|
622
|
-
|
623
|
-
describe "using the form_from_text option" do
|
624
|
-
before(:all) do
|
625
|
-
@file = "#{@root}/data/i_want_to_hold_your_hand.txt"
|
626
|
-
end
|
627
|
-
|
628
|
-
it "should use the exact poetic form 1" do
|
629
|
-
poefy = Poefy::PoefyGen.new(:beatles, {
|
630
|
-
form_from_text: @file
|
631
|
-
})
|
632
|
-
poem = poefy.poem
|
633
|
-
expect(poem.count).to be 46
|
634
|
-
end
|
635
|
-
|
636
|
-
it "should use the exact poetic form 2" do
|
637
|
-
poefy = Poefy::PoefyGen.new :beatles
|
638
|
-
poem = poefy.poem({
|
639
|
-
form_from_text: @file
|
640
|
-
})
|
641
|
-
expect(poem.count).to be 46
|
642
|
-
end
|
643
|
-
|
644
|
-
it "should correctly modify the poetic form 1" do
|
645
|
-
poefy = Poefy::PoefyGen.new(:beatles, {
|
646
|
-
form_from_text: @file,
|
647
|
-
syllable: 6
|
648
|
-
})
|
649
|
-
poem = poefy.poem
|
650
|
-
expect(poem.count).to be 46
|
651
|
-
end
|
652
|
-
|
653
|
-
it "should correctly modify the poetic form 2" do
|
654
|
-
poefy = Poefy::PoefyGen.new :beatles
|
655
|
-
poem = poefy.poem({
|
656
|
-
form_from_text: @file,
|
657
|
-
syllable: 6
|
658
|
-
})
|
659
|
-
expect(poem.count).to be 46
|
660
|
-
end
|
661
|
-
|
662
|
-
it "should correctly modify the poetic form 3" do
|
663
|
-
poefy = Poefy::PoefyGen.new(:beatles, {
|
664
|
-
form_from_text: @file
|
665
|
-
})
|
666
|
-
poem = poefy.poem({
|
667
|
-
syllable: 6
|
668
|
-
})
|
669
|
-
expect(poem.count).to be 46
|
670
|
-
end
|
671
|
-
|
672
|
-
it "should correctly replace the poetic form" do
|
673
|
-
poefy = Poefy::PoefyGen.new(:beatles, {
|
674
|
-
syllable: 6
|
675
|
-
})
|
676
|
-
poem = poefy.poem({
|
677
|
-
form_from_text: @file
|
678
|
-
})
|
679
|
-
expect(poem.count).to be 46
|
680
|
-
end
|
681
|
-
|
682
|
-
############################################################################
|
683
|
-
|
684
|
-
describe "private method #poetic_form_from_text" do
|
685
|
-
|
686
|
-
# Singleton which includes the method.
|
687
|
-
# Make the private methods public.
|
688
|
-
let(:obj) do
|
689
|
-
class Sing
|
690
|
-
include Poefy::PoeticFormFromText
|
691
|
-
include Poefy::StringManipulation
|
692
|
-
public *private_instance_methods
|
693
|
-
end.new
|
694
|
-
end
|
695
|
-
|
696
|
-
it "80 should rhyme with weighty" do
|
697
|
-
lines = ["Lorem ipsum dolor weighty", "Lorem ipsum dolor 80"]
|
698
|
-
form = obj.poetic_form_from_text(lines)
|
699
|
-
expect(form[:rhyme].uniq.count).to be 1
|
700
|
-
end
|
701
|
-
it "80 should not rhyme with shoe" do
|
702
|
-
lines = ["Lorem ipsum dolor shoe", "Lorem ipsum dolor 80"]
|
703
|
-
form = obj.poetic_form_from_text(lines)
|
704
|
-
expect(form[:rhyme].uniq.count).to_not be 1
|
705
|
-
end
|
706
|
-
it "2 should rhyme with shoe" do
|
707
|
-
lines = ["Lorem ipsum dolor shoe", "Lorem ipsum dolor 2"]
|
708
|
-
form = obj.poetic_form_from_text(lines)
|
709
|
-
expect(form[:rhyme].uniq.count).to be 1
|
710
|
-
end
|
711
|
-
it "2 should not rhyme with weighty" do
|
712
|
-
lines = ["Lorem ipsum dolor weighty", "Lorem ipsum dolor 2"]
|
713
|
-
form = obj.poetic_form_from_text(lines)
|
714
|
-
expect(form[:rhyme].uniq.count).to_not be 1
|
715
|
-
end
|
716
|
-
end
|
717
|
-
|
718
|
-
end
|
719
|
-
|
720
|
-
end
|
721
|
-
|
722
|
-
################################################################################
|