poefy 1.1.1 → 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.
@@ -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
- handle_options options
16
- @corpus = Poefy::Database.new db_name, @local
17
- end
18
-
19
- # Make a database using the given lines.
20
- def make_database input, description = nil, overwrite = @overwrite
21
- lines = validate_lines input
22
- lines.map! do |line|
23
- line.force_encoding('utf-8')
24
- .gsub("\u00A0", ' ')
25
- .strip
26
- end
27
- @corpus.close if @corpus
28
- if overwrite
29
- @corpus.make_new! lines, description
30
- else
31
- @corpus.make_new lines, description
32
- end
33
- end
34
- def make_database! input, description = nil
35
- make_database input, description, true
36
- end
37
-
38
- # Close the database.
39
- def close
40
- @corpus.close
41
- end
42
-
43
- # Validate the lines. Arg could be a filename,
44
- # newline delimited string, or array of lines.
45
- def validate_lines input
46
-
47
- # If the input is a file, then read it.
48
- lines = File.exists?(input.to_s) ? File.read(input) : input
49
-
50
- # If lines is not an array, assume string and split on newlines.
51
- lines.respond_to?(:each) ? lines : lines.split("\n")
52
- end
53
-
54
- private
55
-
56
- # Handle the optional initialize options hash.
57
- def handle_options options
58
- @overwrite = options[:overwrite] || false
59
- @local = options[:local] || false
60
- @poetic_form = {}
61
- @poetic_form[:proper] = options[:proper] || true
62
- @poetic_form = validate_poetic_form options
63
- end
64
-
65
- # Make sure the options hash is in order.
66
- def validate_poetic_form poetic_form
67
- input, output = poetic_form, {}
68
- form_string = get_valid_form input[:form]
69
-
70
- # Apply ':form_from_text' before any others.
71
- if input[:form_from_text]
72
- lines = validate_lines input[:form_from_text]
73
- form = poetic_form_from_text lines
74
- input = form.merge input
75
- end
76
-
77
- # Handle obvious inputs.
78
- output[:form] = form_string if form_string
79
- output[:rhyme] = input[:rhyme] if input[:rhyme]
80
- output[:indent] = input[:indent] if input[:indent]
81
- output[:syllable] = input[:syllable] if input[:syllable]
82
- output[:regex] = input[:regex] if input[:regex]
83
- output[:acrostic] = input[:acrostic] if input[:acrostic]
84
- output[:acrostic_x] = input[:acrostic_x] if input[:acrostic_x]
85
- output[:transform] = input[:transform] if input[:transform]
86
-
87
- # Tokenise string to arrays and hashes.
88
- if output[:rhyme]
89
- output[:rhyme] = tokenise_rhyme output[:rhyme]
90
- end
91
- rhyme = get_poetic_form_rhyme_longest(output)
92
- if output[:syllable] and rhyme != ' '
93
- output[:syllable] = transform_input_syllable output[:syllable], rhyme
94
- end
95
- if output[:regex]
96
- output[:regex] = transform_input_regex output[:regex], rhyme
97
- end
98
-
99
- # Get from instance by default.
100
- output[:proper] = input[:proper].nil? ?
101
- @poetic_form[:proper] : input[:proper]
102
-
103
- # Tiny amendment to solve later errors.
104
- output[:rhyme] = ' ' if output[:rhyme] == ''
105
- output
106
- end
107
-
108
- end
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
+ ################################################################################