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,121 +1,121 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: UTF-8
3
-
4
- ################################################################################
5
- # Monkey patch the Array class.
6
- ################################################################################
7
-
8
- #--
9
- # Declare module structure.
10
- #++
11
- module Poefy
12
- module CoreExtensions
13
- module Array
14
- module SortByDistance
15
- end
16
- module ModuloIndex
17
- end
18
- end
19
- end
20
- end
21
-
22
- #--
23
- # Define module methods.
24
- #++
25
- module Poefy::CoreExtensions::Array::SortByDistance
26
-
27
- ##
28
- # Take an array index and return a permutation of the
29
- # items sorted by distance from that index.
30
- # If 'index' is not specified, return an Enumerator
31
- # of the results for all indices, in order.
32
- #
33
- # The ':reverse' keyword argument switches the equally close
34
- # neighbours from lowest index first to highest first.
35
- # It's an option added mostly for completeness, but it's
36
- # there if you need it.
37
- #
38
- def sort_by_distance_from_index index = nil, reverse: false
39
-
40
- # Return Enumerator of all possible output arrays.
41
- if index.nil?
42
- Enumerator.new(self.count) do |y|
43
- self.each.with_index do |value, index|
44
- y << self.sort_by_distance_from_index(index, reverse: reverse)
45
- end
46
- end
47
-
48
- # Return Enumerator of results for a single index.
49
- else
50
- Enumerator.new(self.count) do |y|
51
- y << self[index]
52
- counter = 0
53
- loop do
54
- counter += 1
55
-
56
- # Consider negative indices OOB, not from array tail.
57
- below_index = index - counter
58
- below_index = nil if below_index < 0
59
- below = self[below_index] if below_index
60
-
61
- # This is fine, uses nil as default value if OOB.
62
- above = self[index + counter]
63
-
64
- # Both the elements with index one higher and one lower
65
- # are equally close neighbours to the subject element.
66
- # The default is to output the element with the lowest
67
- # index first. With ':reverse' set to true, the highest
68
- # index is appended first.
69
- if reverse
70
- y << above if above
71
- y << below if below
72
- else
73
- y << below if below
74
- y << above if above
75
- end
76
-
77
- # Break if we're at the last element.
78
- break if !above and !below
79
- end
80
- end
81
- end
82
- end
83
-
84
- ##
85
- # Find all elements that match 'value' and return the
86
- # sort_by_distance results for all, as an Enumerator.
87
- #
88
- def sort_by_distance_from_value value = nil, reverse: false
89
- matching = self.each_index.select { |i| self[i] == value }
90
- Enumerator.new(matching.count) do |y|
91
- matching.each do |index|
92
- y << self.sort_by_distance_from_index(index, reverse: reverse)
93
- end
94
- end
95
- end
96
- end
97
-
98
- #--
99
- # Define module methods.
100
- #++
101
- module Poefy::CoreExtensions::Array::ModuloIndex
102
-
103
- ##
104
- # Return elements located at specific index periods.
105
- #
106
- def modulo_index(divider, remainder = 0, start_index = 0)
107
- self.values_at(* self.each_index.select do |i|
108
- (i + start_index) % divider == remainder
109
- end)
110
- end
111
- end
112
-
113
- #--
114
- # Extend Array class.
115
- #++
116
- class Array
117
- include Poefy::CoreExtensions::Array::SortByDistance
118
- include Poefy::CoreExtensions::Array::ModuloIndex
119
- end
120
-
121
- ################################################################################
1
+ #!/usr/bin/env ruby
2
+ # Encoding: UTF-8
3
+
4
+ ################################################################################
5
+ # Monkey patch the Array class.
6
+ ################################################################################
7
+
8
+ #--
9
+ # Declare module structure.
10
+ #++
11
+ module Poefy
12
+ module CoreExtensions
13
+ module Array
14
+ module SortByDistance
15
+ end
16
+ module ModuloIndex
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ #--
23
+ # Define module methods.
24
+ #++
25
+ module Poefy::CoreExtensions::Array::SortByDistance
26
+
27
+ ##
28
+ # Take an array index and return a permutation of the
29
+ # items sorted by distance from that index.
30
+ # If 'index' is not specified, return an Enumerator
31
+ # of the results for all indices, in order.
32
+ #
33
+ # The ':reverse' keyword argument switches the equally close
34
+ # neighbours from lowest index first to highest first.
35
+ # It's an option added mostly for completeness, but it's
36
+ # there if you need it.
37
+ #
38
+ def sort_by_distance_from_index index = nil, reverse: false
39
+
40
+ # Return Enumerator of all possible output arrays.
41
+ if index.nil?
42
+ Enumerator.new(self.count) do |y|
43
+ self.each.with_index do |value, index|
44
+ y << self.sort_by_distance_from_index(index, reverse: reverse)
45
+ end
46
+ end
47
+
48
+ # Return Enumerator of results for a single index.
49
+ else
50
+ Enumerator.new(self.count) do |y|
51
+ y << self[index]
52
+ counter = 0
53
+ loop do
54
+ counter += 1
55
+
56
+ # Consider negative indices OOB, not from array tail.
57
+ below_index = index - counter
58
+ below_index = nil if below_index < 0
59
+ below = self[below_index] if below_index
60
+
61
+ # This is fine, uses nil as default value if OOB.
62
+ above = self[index + counter]
63
+
64
+ # Both the elements with index one higher and one lower
65
+ # are equally close neighbours to the subject element.
66
+ # The default is to output the element with the lowest
67
+ # index first. With ':reverse' set to true, the highest
68
+ # index is appended first.
69
+ if reverse
70
+ y << above if above
71
+ y << below if below
72
+ else
73
+ y << below if below
74
+ y << above if above
75
+ end
76
+
77
+ # Break if we're at the last element.
78
+ break if !above and !below
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ ##
85
+ # Find all elements that match 'value' and return the
86
+ # sort_by_distance results for all, as an Enumerator.
87
+ #
88
+ def sort_by_distance_from_value value = nil, reverse: false
89
+ matching = self.each_index.select { |i| self[i] == value }
90
+ Enumerator.new(matching.count) do |y|
91
+ matching.each do |index|
92
+ y << self.sort_by_distance_from_index(index, reverse: reverse)
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ #--
99
+ # Define module methods.
100
+ #++
101
+ module Poefy::CoreExtensions::Array::ModuloIndex
102
+
103
+ ##
104
+ # Return elements located at specific index periods.
105
+ #
106
+ def modulo_index(divider, remainder = 0, start_index = 0)
107
+ self.values_at(* self.each_index.select do |i|
108
+ (i + start_index) % divider == remainder
109
+ end)
110
+ end
111
+ end
112
+
113
+ #--
114
+ # Extend Array class.
115
+ #++
116
+ class Array
117
+ include Poefy::CoreExtensions::Array::SortByDistance
118
+ include Poefy::CoreExtensions::Array::ModuloIndex
119
+ end
120
+
121
+ ################################################################################
@@ -1,151 +1,151 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: UTF-8
3
-
4
- ################################################################################
5
- # Base class for connecting to a database.
6
- # Install gem 'poefy-sqlite3' or 'poefy-pg' for implementation.
7
- ################################################################################
8
-
9
- require_relative 'string_manipulation.rb'
10
-
11
- ################################################################################
12
-
13
- module Poefy
14
-
15
- class Database
16
-
17
- include Poefy::StringManipulation
18
-
19
- attr_reader :name, :local
20
-
21
- def initialize name, local = false
22
- @local = local
23
- @name = name.to_s
24
- @sproc = {}
25
- type
26
- db
27
- end
28
-
29
- ############################################################################
30
-
31
- # Validate that a database type has been required.
32
- # This will be overwritten by a database-specific method,
33
- # so raise an error if no database has been specified yet.
34
- # Due to the way 'bin/poefy' is set up, that code will fail before
35
- # this point is reached, so this error is only from Ruby calls.
36
- def type
37
- msg = "No database interface specified. " +
38
- "Please require 'poefy/sqlite3' or 'poefy/pg'"
39
- raise LoadError, msg
40
- end
41
-
42
- # Open instance database session, if not already existing.
43
- def db
44
- if not @db and exists?
45
- begin
46
- open_connection
47
- create_sprocs
48
- rescue
49
- @db = nil
50
- raise Poefy::StructureInvalid
51
- end
52
- end
53
- @db
54
- end
55
-
56
- # Close the database file.
57
- def close
58
- @sproc.each { |k, v| v.close rescue nil }
59
- @db.close if @db
60
- @db = nil
61
- end
62
-
63
- # Creates a database with the correct format.
64
- # Convert input lines array to SQL import format file.
65
- # Delete database if already exists.
66
- # Create database using SQL import file.
67
- # Delete both files.
68
- def make_new lines, description = nil
69
- make_new!(lines, description) if !exists?
70
- end
71
-
72
- # Force new database, overwriting existing.
73
- def make_new! lines, description = nil
74
-
75
- # Create a new database.
76
- new_connection
77
-
78
- # Create the lines table and the index.
79
- create_table table, description
80
-
81
- # Convert the lines array into an expanded array of rhyme metadata.
82
- import_data = lines_rhyme_metadata lines
83
-
84
- # Import the data.
85
- insert_lines table, import_data
86
-
87
- # Recreate the stored procedures.
88
- create_sprocs
89
- end
90
-
91
- # Public interfaces for private stored procedure methods.
92
- # Use instance variables to keep a cache of the results.
93
- def rhymes_by_count rhyme_count, syllable_min_max = nil
94
- db
95
- @rbc = Hash.new { |h,k| h[k] = {} } if @rbc.nil?
96
- if @rbc[rhyme_count][syllable_min_max].nil?
97
- @rbc[rhyme_count][syllable_min_max] = if syllable_min_max
98
- sproc_rhymes_by_count_syllables(rhyme_count, syllable_min_max)
99
- else
100
- sproc_rhymes_by_count(rhyme_count)
101
- end
102
- end
103
- @rbc[rhyme_count][syllable_min_max].dup
104
- end
105
- def lines_by_rhyme rhyme, syllable_min_max = nil
106
- db
107
- @la = Hash.new { |h,k| h[k] = {} } if @la.nil?
108
- if @la[rhyme][syllable_min_max].nil?
109
- @la[rhyme][syllable_min_max] = if syllable_min_max
110
- sproc_lines_by_rhyme_syllables(rhyme, syllable_min_max)
111
- else
112
- sproc_lines_by_rhyme(rhyme)
113
- end
114
- end
115
- @la[rhyme][syllable_min_max].dup
116
- end
117
-
118
- private
119
-
120
- ##########################################################################
121
-
122
- # For each line, figure out the needed rhyme metadata.
123
- # Output is an array: [line, final_word, rhyme, syllables]
124
- def lines_rhyme_metadata lines
125
- output = []
126
- lines.map do |line|
127
-
128
- # Don't add the line if it contains a blacklisted? substring.
129
- next if Wordfilter.blacklisted? line
130
-
131
- # Get the phrase info for the line.
132
- phrase = phrase_info line
133
- syll = phrase[:syllables]
134
- rhymes = phrase[:rhymes]
135
- final = phrase[:last_word]
136
-
137
- # There may be more than one rhyme, so add a database
138
- # record for each rhyme.
139
- rhymes.each do |rhyme|
140
- output << [line, syll, final, rhyme]
141
- end
142
- end
143
-
144
- output
145
- end
146
-
147
- end
148
-
149
- end
150
-
151
- ################################################################################
1
+ #!/usr/bin/env ruby
2
+ # Encoding: UTF-8
3
+
4
+ ################################################################################
5
+ # Base class for connecting to a database.
6
+ # Install gem 'poefy-sqlite3' or 'poefy-pg' for implementation.
7
+ ################################################################################
8
+
9
+ require_relative 'string_manipulation.rb'
10
+
11
+ ################################################################################
12
+
13
+ module Poefy
14
+
15
+ class Database
16
+
17
+ include Poefy::StringManipulation
18
+
19
+ attr_reader :name, :local
20
+
21
+ def initialize name, local = false
22
+ @local = local
23
+ @name = name.to_s
24
+ @sproc = {}
25
+ type
26
+ db
27
+ end
28
+
29
+ ############################################################################
30
+
31
+ # Validate that a database type has been required.
32
+ # This will be overwritten by a database-specific method,
33
+ # so raise an error if no database has been specified yet.
34
+ # Due to the way 'bin/poefy' is set up, that code will fail before
35
+ # this point is reached, so this error is only from Ruby calls.
36
+ def type
37
+ msg = "No database interface specified. " +
38
+ "Please require 'poefy/sqlite3' or 'poefy/pg'"
39
+ raise LoadError, msg
40
+ end
41
+
42
+ # Open instance database session, if not already existing.
43
+ def db
44
+ if not @db and exist?
45
+ begin
46
+ open_connection
47
+ create_sprocs
48
+ rescue
49
+ @db = nil
50
+ raise Poefy::StructureInvalid
51
+ end
52
+ end
53
+ @db
54
+ end
55
+
56
+ # Close the database file.
57
+ def close
58
+ @sproc.each { |k, v| v.close rescue nil }
59
+ @db.close if @db
60
+ @db = nil
61
+ end
62
+
63
+ # Creates a database with the correct format.
64
+ # Convert input lines array to SQL import format file.
65
+ # Delete database if already exists.
66
+ # Create database using SQL import file.
67
+ # Delete both files.
68
+ def make_new lines, description = nil
69
+ make_new!(lines, description) unless exist?
70
+ end
71
+
72
+ # Force new database, overwriting existing.
73
+ def make_new! lines, description = nil
74
+
75
+ # Create a new database.
76
+ new_connection
77
+
78
+ # Create the lines table and the index.
79
+ create_table table, description
80
+
81
+ # Convert the lines array into an expanded array of rhyme metadata.
82
+ import_data = lines_rhyme_metadata lines
83
+
84
+ # Import the data.
85
+ insert_lines table, import_data
86
+
87
+ # Recreate the stored procedures.
88
+ create_sprocs
89
+ end
90
+
91
+ # Public interfaces for private stored procedure methods.
92
+ # Use instance variables to keep a cache of the results.
93
+ def rhymes_by_count rhyme_count, syllable_min_max = nil
94
+ db
95
+ @rbc = Hash.new { |h,k| h[k] = {} } if @rbc.nil?
96
+ if @rbc[rhyme_count][syllable_min_max].nil?
97
+ @rbc[rhyme_count][syllable_min_max] = if syllable_min_max
98
+ sproc_rhymes_by_count_syllables(rhyme_count, syllable_min_max)
99
+ else
100
+ sproc_rhymes_by_count(rhyme_count)
101
+ end
102
+ end
103
+ @rbc[rhyme_count][syllable_min_max].dup
104
+ end
105
+ def lines_by_rhyme rhyme, syllable_min_max = nil
106
+ db
107
+ @la = Hash.new { |h,k| h[k] = {} } if @la.nil?
108
+ if @la[rhyme][syllable_min_max].nil?
109
+ @la[rhyme][syllable_min_max] = if syllable_min_max
110
+ sproc_lines_by_rhyme_syllables(rhyme, syllable_min_max)
111
+ else
112
+ sproc_lines_by_rhyme(rhyme)
113
+ end
114
+ end
115
+ @la[rhyme][syllable_min_max].dup
116
+ end
117
+
118
+ private
119
+
120
+ ##########################################################################
121
+
122
+ # For each line, figure out the needed rhyme metadata.
123
+ # Output is an array: [line, final_word, rhyme, syllables]
124
+ def lines_rhyme_metadata lines
125
+ output = []
126
+ lines.map do |line|
127
+
128
+ # Don't add the line if it contains a blacklisted? substring.
129
+ next if Wordfilter.blacklisted? line
130
+
131
+ # Get the phrase info for the line.
132
+ phrase = phrase_info line
133
+ syll = phrase[:syllables]
134
+ rhymes = phrase[:rhymes]
135
+ final = phrase[:last_word]
136
+
137
+ # There may be more than one rhyme, so add a database
138
+ # record for each rhyme.
139
+ rhymes.each do |rhyme|
140
+ output << [line, syll, final, rhyme]
141
+ end
142
+ end
143
+
144
+ output
145
+ end
146
+
147
+ end
148
+
149
+ end
150
+
151
+ ################################################################################
data/lib/poefy/db_type.rb CHANGED
@@ -1,57 +1,57 @@
1
- #!/usr/bin/env ruby
2
- # Encoding: UTF-8
3
-
4
- ################################################################################
5
- # Methods for selecting which database interface to use.
6
- # And for including the correct gem, based on that choice.
7
- ################################################################################
8
-
9
- require 'yaml'
10
-
11
- ################################################################################
12
-
13
- module Poefy
14
-
15
- # Are we running this through the console? (Or as a Ruby library?)
16
- def self.console= bool
17
- @@console = !!bool
18
- end
19
- def self.console
20
- @@console ||= false
21
- end
22
-
23
- # View and amend the database type in the 'settings' file.
24
- def self.database_type= db_name
25
- settings_file = Poefy.root + '/settings.yml'
26
- File.open(settings_file, 'w') do |file|
27
- hsh = {'database' => db_name}
28
- file.write hsh.to_yaml
29
- end
30
- @@database_type = nil
31
- end
32
- def self.database_type create_file = true
33
- @@database_type ||= nil
34
- return @@database_type if @@database_type
35
- settings_file = Poefy.root + '/settings.yml'
36
- if not File.exists?(settings_file)
37
- return nil if !create_file
38
- Poefy.database_type = 'pg'
39
- end
40
- @@database_type = YAML::load_file(settings_file)['database']
41
- end
42
-
43
- # Requires the chosen database interface gem.
44
- def self.require_db db_interface_gem = nil
45
- begin
46
- @@database_type = db_interface_gem if db_interface_gem
47
- require 'poefy/' + (db_interface_gem || Poefy.database_type)
48
-
49
- # Replace with custom exception.
50
- rescue LoadError
51
- raise Poefy::MissingDBInterface
52
- end
53
- end
54
-
55
- end
56
-
57
- ################################################################################
1
+ #!/usr/bin/env ruby
2
+ # Encoding: UTF-8
3
+
4
+ ################################################################################
5
+ # Methods for selecting which database interface to use.
6
+ # And for including the correct gem, based on that choice.
7
+ ################################################################################
8
+
9
+ require 'yaml'
10
+
11
+ ################################################################################
12
+
13
+ module Poefy
14
+
15
+ # Are we running this through the console? (Or as a Ruby library?)
16
+ def self.console= bool
17
+ @@console = !!bool
18
+ end
19
+ def self.console
20
+ @@console ||= false
21
+ end
22
+
23
+ # View and amend the database type in the 'settings' file.
24
+ def self.database_type= db_name
25
+ settings_file = Poefy.root + '/settings.yml'
26
+ File.open(settings_file, 'w') do |file|
27
+ hsh = {'database' => db_name}
28
+ file.write hsh.to_yaml
29
+ end
30
+ @@database_type = nil
31
+ end
32
+ def self.database_type create_file = true
33
+ @@database_type ||= nil
34
+ return @@database_type if @@database_type
35
+ settings_file = Poefy.root + '/settings.yml'
36
+ unless File.exist?(settings_file)
37
+ return nil if !create_file
38
+ Poefy.database_type = 'pg'
39
+ end
40
+ @@database_type = YAML::load_file(settings_file)['database']
41
+ end
42
+
43
+ # Requires the chosen database interface gem.
44
+ def self.require_db db_interface_gem = nil
45
+ begin
46
+ @@database_type = db_interface_gem if db_interface_gem
47
+ require 'poefy/' + (db_interface_gem || Poefy.database_type)
48
+
49
+ # Replace with custom exception.
50
+ rescue LoadError
51
+ raise Poefy::MissingDBInterface
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ ################################################################################
@@ -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.exists?
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