poefy 1.1.0 → 2.0.0
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 +5 -5
- data/.rspec +1 -1
- data/LICENSE +13 -13
- data/README.md +844 -844
- data/Rakefile +6 -6
- data/bin/poefy +347 -310
- data/bin/poefy_make +91 -91
- data/data/st_therese_of_lisieux.txt +3700 -3700
- data/data/whitman_leaves.txt +17815 -17815
- data/lib/poefy/core_extensions/array.rb +121 -121
- data/lib/poefy/database.rb +151 -151
- data/lib/poefy/db_type.rb +57 -57
- data/lib/poefy/generation.rb +16 -18
- data/lib/poefy/poem_base.rb +114 -112
- data/lib/poefy/poetic_forms.rb +529 -529
- data/lib/poefy/self.rb +39 -39
- data/lib/poefy/version.rb +26 -26
- data/lib/poefy.rb +46 -46
- data/poefy.gemspec +5 -3
- data/settings.yml +2 -2
- data/spec/poefy_unit_spec.rb +621 -621
- data/spec/spec_helper.rb +11 -11
- metadata +9 -12
data/lib/poefy/generation.rb
CHANGED
@@ -17,7 +17,7 @@ module Poefy
|
|
17
17
|
def poem poetic_form = @poetic_form
|
18
18
|
|
19
19
|
# Can't do much if the database doesn't exist.
|
20
|
-
raise Poefy::MissingDatabase unless @corpus.
|
20
|
+
raise Poefy::MissingDatabase unless @corpus.exist?
|
21
21
|
|
22
22
|
# Validate the poetic form hash.
|
23
23
|
raise ArgumentError, 'Argument must be a hash' unless
|
@@ -206,10 +206,6 @@ module Poefy
|
|
206
206
|
# words as there are lines to be matched.
|
207
207
|
rhymes = nil
|
208
208
|
|
209
|
-
# If all the lines include a 'regex' condition,
|
210
|
-
# then we can specify to only query for matching lines.
|
211
|
-
regex_all = regex_for_all line_conds
|
212
|
-
|
213
209
|
# If all the lines include a 'syllable' condition,
|
214
210
|
# then we can specify to only query for matching lines.
|
215
211
|
begin
|
@@ -226,7 +222,7 @@ module Poefy
|
|
226
222
|
# For each rhyme, get all lines and try to sastify all conditions.
|
227
223
|
out = []
|
228
224
|
rhymes.shuffle.each do |rhyme|
|
229
|
-
out = try_rhyme(conditions, rhyme, min_max,
|
225
|
+
out = try_rhyme(conditions, rhyme, min_max, line_conds)
|
230
226
|
break if !out.empty?
|
231
227
|
end
|
232
228
|
if out.empty?
|
@@ -300,14 +296,26 @@ module Poefy
|
|
300
296
|
|
301
297
|
# Loop through the rhymes until we find one that works.
|
302
298
|
# (In a reasonable time-frame)
|
303
|
-
def try_rhyme conditions, rhyme, syllable_min_max = nil,
|
299
|
+
private def try_rhyme conditions, rhyme, syllable_min_max = nil, line_conds = nil
|
304
300
|
output = []
|
305
301
|
lines = @corpus.lines_by_rhyme(rhyme, syllable_min_max)
|
306
302
|
|
307
303
|
# To reduce the number of permutations, reject lines
|
308
304
|
# that do not match any of the lines regex.
|
309
|
-
|
305
|
+
regex_conds = line_conds.map { |i| i[:regex] }
|
306
|
+
if !regex_conds.include?(nil) and !regex_conds.include?(//)
|
307
|
+
new_lines = []
|
308
|
+
[*regex_conds].each do |regex_group|
|
309
|
+
possible_lines = lines.dup
|
310
|
+
[*regex_group].each do |regex|
|
311
|
+
possible_lines.reject! { |i| !(i['line'].match(regex)) }
|
312
|
+
end
|
313
|
+
new_lines += possible_lines
|
314
|
+
end
|
315
|
+
lines = new_lines
|
316
|
+
end
|
310
317
|
|
318
|
+
# Get a sample from the lines that works for all the conditions.
|
311
319
|
begin
|
312
320
|
Timeout::timeout(2) do
|
313
321
|
output = lines.shuffle.conditional_sample(conditions)
|
@@ -334,16 +342,6 @@ module Poefy
|
|
334
342
|
min_max
|
335
343
|
end
|
336
344
|
|
337
|
-
# If every line has a regex, then return a regex union.
|
338
|
-
def regex_for_all line_conds
|
339
|
-
output = nil
|
340
|
-
if line_conds.all?{ |i| i[:regex] }
|
341
|
-
all_regex = line_conds.map{ |i| i[:regex] }
|
342
|
-
output = Regexp.union all_regex.flatten
|
343
|
-
end
|
344
|
-
output
|
345
|
-
end
|
346
|
-
|
347
345
|
end
|
348
346
|
|
349
347
|
end
|
data/lib/poefy/poem_base.rb
CHANGED
@@ -1,112 +1,114 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: UTF-8
|
3
|
-
|
4
|
-
################################################################################
|
5
|
-
# Base internals for the Poem class.
|
6
|
-
################################################################################
|
7
|
-
|
8
|
-
module Poefy
|
9
|
-
|
10
|
-
module PoemBase
|
11
|
-
|
12
|
-
attr_reader :corpus, :local, :overwrite
|
13
|
-
|
14
|
-
def initialize db_name, options =
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
lines
|
23
|
-
|
24
|
-
|
25
|
-
.
|
26
|
-
|
27
|
-
|
28
|
-
if
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
@
|
60
|
-
@
|
61
|
-
@poetic_form
|
62
|
-
@poetic_form =
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
output[:
|
81
|
-
output[:
|
82
|
-
output[:
|
83
|
-
output[:
|
84
|
-
output[:
|
85
|
-
output[:
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
end
|
111
|
-
|
112
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Encoding: UTF-8
|
3
|
+
|
4
|
+
################################################################################
|
5
|
+
# Base internals for the Poem class.
|
6
|
+
################################################################################
|
7
|
+
|
8
|
+
module Poefy
|
9
|
+
|
10
|
+
module PoemBase
|
11
|
+
|
12
|
+
attr_reader :corpus, :local, :overwrite
|
13
|
+
|
14
|
+
def initialize db_name, options = nil
|
15
|
+
options ||= {}
|
16
|
+
handle_options options
|
17
|
+
@corpus = Poefy::Database.new db_name, @local
|
18
|
+
end
|
19
|
+
|
20
|
+
# Make a database using the given lines.
|
21
|
+
def make_database input, description = nil, overwrite = @overwrite
|
22
|
+
lines = validate_lines input
|
23
|
+
lines.map! do |line|
|
24
|
+
line.force_encoding('utf-8')
|
25
|
+
.gsub("\u00A0", ' ')
|
26
|
+
.strip
|
27
|
+
end
|
28
|
+
@corpus.close if @corpus
|
29
|
+
if overwrite
|
30
|
+
@corpus.make_new! lines, description
|
31
|
+
else
|
32
|
+
@corpus.make_new lines, description
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def make_database! input, description = nil
|
36
|
+
make_database input, description, true
|
37
|
+
end
|
38
|
+
|
39
|
+
# Close the database.
|
40
|
+
def close
|
41
|
+
@corpus.close
|
42
|
+
end
|
43
|
+
|
44
|
+
# Validate the lines. Arg could be a filename,
|
45
|
+
# newline delimited string, or array of lines.
|
46
|
+
def validate_lines input
|
47
|
+
|
48
|
+
# If the input is a file, then read it.
|
49
|
+
lines = File.exist?(input.to_s) ? File.read(input) : input
|
50
|
+
|
51
|
+
# If lines is not an array, assume string and split on newlines.
|
52
|
+
lines.respond_to?(:each) ? lines : lines.split("\n")
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
# Handle the optional initialize options hash.
|
58
|
+
def handle_options options
|
59
|
+
@overwrite = !!options[:overwrite]
|
60
|
+
@local = !!options[:local]
|
61
|
+
@poetic_form = {}
|
62
|
+
@poetic_form[:proper] = true
|
63
|
+
@poetic_form[:proper] = !!options[:proper] if options[:proper]
|
64
|
+
@poetic_form = validate_poetic_form options
|
65
|
+
end
|
66
|
+
|
67
|
+
# Make sure the options hash is in order.
|
68
|
+
def validate_poetic_form poetic_form
|
69
|
+
input, output = poetic_form, {}
|
70
|
+
form_string = get_valid_form input[:form]
|
71
|
+
|
72
|
+
# Apply ':form_from_text' before any others.
|
73
|
+
if input[:form_from_text]
|
74
|
+
lines = validate_lines input[:form_from_text]
|
75
|
+
form = poetic_form_from_text lines
|
76
|
+
input = form.merge input
|
77
|
+
end
|
78
|
+
|
79
|
+
# Handle obvious inputs.
|
80
|
+
output[:form] = form_string if form_string
|
81
|
+
output[:rhyme] = input[:rhyme] if input[:rhyme]
|
82
|
+
output[:indent] = input[:indent] if input[:indent]
|
83
|
+
output[:syllable] = input[:syllable] if input[:syllable]
|
84
|
+
output[:regex] = input[:regex] if input[:regex]
|
85
|
+
output[:acrostic] = input[:acrostic] if input[:acrostic]
|
86
|
+
output[:acrostic_x] = input[:acrostic_x] if input[:acrostic_x]
|
87
|
+
output[:transform] = input[:transform] if input[:transform]
|
88
|
+
|
89
|
+
# Tokenise string to arrays and hashes.
|
90
|
+
if output[:rhyme]
|
91
|
+
output[:rhyme] = tokenise_rhyme output[:rhyme]
|
92
|
+
end
|
93
|
+
rhyme = get_poetic_form_rhyme_longest(output)
|
94
|
+
if output[:syllable] and rhyme != ' '
|
95
|
+
output[:syllable] = transform_input_syllable output[:syllable], rhyme
|
96
|
+
end
|
97
|
+
if output[:regex]
|
98
|
+
output[:regex] = transform_input_regex output[:regex], rhyme
|
99
|
+
end
|
100
|
+
|
101
|
+
# Get from instance by default.
|
102
|
+
output[:proper] = input[:proper].nil? ?
|
103
|
+
@poetic_form[:proper] : input[:proper]
|
104
|
+
|
105
|
+
# Tiny amendment to solve later errors.
|
106
|
+
output[:rhyme] = ' ' if output[:rhyme] == ''
|
107
|
+
output
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
################################################################################
|