lokap-verbs 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,360 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The program conjugates most common english verbs with the following option:
4
+ # * :tense => :past or :present or :future (default: :present)
5
+ # * :person => :first or :second or :third (default: :third)
6
+ # * :plurality => :singular or :plural (default: :singular)
7
+ # * :aspect => :habitual or :perfect or :perfective or :progressive or :prospective (default: :habitual, or :perfective for past tense)
8
+ # * :mood => :indicative or :imperative or :subjunctive (default: :indicative)
9
+ # * :diathesis => :active or :passive (default: :active)
10
+ #
11
+ # Author:: Andy Rossmeissl
12
+ # Copyright:: Copyright (c) 2009 Andy Rossmeissl
13
+ # License:: Found in LICENSE file
14
+
15
+ module Verbs
16
+ module Conjugator
17
+ extend self
18
+
19
+ # This class determines the conjugations from the given options (or defaults)
20
+ # conjugations are then applied to the verb
21
+ class Conjugations
22
+ include Singleton
23
+
24
+ # permit outside functions access to these variables
25
+ attr_reader :irregulars, :single_terminal_consonants, :copulars
26
+
27
+ # Creates initial variables for class
28
+ def initialize
29
+ @irregulars = {}
30
+ @single_terminal_consonants = []
31
+ @copulars = {}
32
+ end
33
+
34
+ # Determines irregular verbs from the expression
35
+ # Params:
36
+ # * infinitive, the given verb
37
+ # * preterite, denote events that took place in the past
38
+ # * past_participle, form of a verb, ending in 'ed'
39
+ # * &blk, block of code that may be run
40
+ def irregular(infinitive, preterite = nil, past_participle = nil, &blk)
41
+ if block_given?
42
+ # create new Verb object with infinitive and &blk
43
+ irregular = ::Verbs::Verb.new infinitive, &blk
44
+ else
45
+ unless preterite && past_participle
46
+ raise ArgumentError,
47
+ 'Standard irregular verbs must specify preterite and past participle forms'
48
+ end
49
+
50
+ # create new Verb object with infinitive, preterite and past_participle
51
+ irregular = ::Verbs::Verb.new infinitive, preterite: preterite, past_participle: past_participle
52
+ end
53
+ @irregulars[infinitive] = irregular
54
+ end
55
+
56
+ # Find single terminal consonant with the infinitive
57
+ # Params:
58
+ # * infinitive, the given verb
59
+ def single_terminal_consonant(infinitive)
60
+ @single_terminal_consonants << infinitive
61
+ end
62
+ end
63
+
64
+ # Runs a block of code if given in a class instance
65
+ # else only class instance is created
66
+ def conjugations
67
+ if block_given?
68
+ yield Conjugations.instance
69
+ else
70
+ Conjugations.instance
71
+ end
72
+ end
73
+
74
+ # Using options given, determine the conjugation and the subject
75
+ # Return the subject, if there is one, and the proper conjugation
76
+ # Params:
77
+ # * infinitive, the given verb
78
+ # * options, the list of parameters to alter the conjugation
79
+ def conjugate(infinitive, options = {})
80
+ infinitive = infinitive.dup if infinitive.is_a?(String)
81
+
82
+ # set all options according to parameter, or the default
83
+ tense = options[:tense] || :present # present, past, future
84
+ person = options[:person] || :third # first, second, third
85
+ plurality = options[:plurality] || :singular # singular, plural
86
+ diathesis = options[:diathesis] || :active # active, passive
87
+ mood = options[:mood] || :indicative # imperative, subjunctive
88
+ aspect = options[:aspect] || default_aspect(options) # perfective, habitual, progressive, perfect, prospective
89
+
90
+ check_for_improper_constructions(infinitive, tense, person, mood, diathesis) # find incompatabilities
91
+ form = form_for(tense, aspect, diathesis) # find form array based on tense and aspect
92
+
93
+ # map form array to conjugation array, applying infinitive and options to the array
94
+ conjugation = form.map { |e| resolve e, infinitive, tense, person, plurality, mood }.join(' ').strip
95
+
96
+ if options[:subject] # When options includes a subject,
97
+ actor = options.delete(:subject) # remove from options and make subject humanized
98
+ actor = subject(options).humanize if actor.is_a?(TrueClass)
99
+ end
100
+
101
+ "#{actor} #{conjugation}".strip
102
+ end
103
+
104
+ # Finds the pronoun associated with the subject for the conjugation
105
+ # Returns the pronoun
106
+ # Params:
107
+ # * options, list of options given to determine conjugation
108
+ def subject(options)
109
+ case [options[:person], options[:plurality]]
110
+ when %i[first singular]
111
+ 'I'
112
+ when %i[first plural]
113
+ 'we'
114
+ when %i[second singular], %i[second plural]
115
+ 'you'
116
+ when %i[third singular]
117
+ 'he'
118
+ when %i[third plural]
119
+ 'they'
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ # Resolves conflictions between options of the conjugation
126
+ # Params:
127
+ # * element,
128
+ # * infinitive, the given verb
129
+ # * tense, an option given by the user
130
+ # * person, an option given by the user
131
+ # * plurality, an option given by the user
132
+ # * mood, an option given by the user
133
+ def resolve(element, infinitive, tense, person, plurality, mood)
134
+ case element
135
+ when String
136
+ element
137
+ when :infinitive
138
+ infinitive
139
+ when :present, :past, :present_participle, :past_participle
140
+ inflect infinitive, element, person, plurality, mood
141
+ when Symbol
142
+ inflect element, tense, person, plurality, mood
143
+ end
144
+ end
145
+
146
+ # Change the form to express the proper grammatical function
147
+ # Params:
148
+ # * infinitive,the given verb
149
+ # * inflection, form to be changed
150
+ # * person, an option given by the user
151
+ # * plurality, an option given by the user
152
+ # * mood, an option given by the user
153
+ def inflect(infinitive, inflection, person, plurality, mood)
154
+ send(*[inflection, infinitive, person, plurality, mood][0, method(inflection).arity + 1])
155
+ end
156
+
157
+ def present(infinitive, person, plurality, mood)
158
+ if (verb = conjugations.irregulars[infinitive])
159
+ conjugate_irregular(verb, tense: :present, person: person, plurality: plurality, mood: mood)
160
+ elsif (person == :third) && (plurality == :singular) && (mood != :subjunctive)
161
+ present_third_person_singular_form_for infinitive
162
+ else
163
+ infinitive
164
+ end
165
+ end
166
+
167
+ # Conjugate verb to past with relevent options determining outcome
168
+ # Params:
169
+ # * infinitive, the given verb
170
+ # * person, the subject of the verb
171
+ # * plurality, an option given by the user
172
+ # * mood, an option given by the user
173
+ def past(infinitive, person, plurality, mood)
174
+ if (verb = conjugations.irregulars[infinitive])
175
+ conjugate_irregular(verb, tense: :past, person: person, plurality: plurality, mood: mood)
176
+ else
177
+ regular_preterite_for infinitive
178
+ end
179
+ end
180
+
181
+ # Forming verb to apply present tense endings
182
+ # Params:
183
+ # * infinitive, the given verb
184
+ def present_participle(infinitive)
185
+ if infinitive.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{CONSONANTS_WITHOUT_C_PATTERN}$/) &&
186
+ !conjugations.single_terminal_consonants.include?(infinitive.to_sym)
187
+ return present_participle_with_doubled_terminal_consonant_for infinitive
188
+ end
189
+
190
+ case infinitive.to_s
191
+ when /c$/
192
+ "#{infinitive}k"
193
+ when /(^be|ye|oe|nge|ee)$/
194
+ infinitive.to_s
195
+ when /ie$/
196
+ infinitive.to_s.gsub(/ie$/, 'y')
197
+ when /#{VOWEL_PATTERN}#{CONSONANT_PATTERN}e$/, /ue$/
198
+ infinitive.to_s[0..-2]
199
+ else # rubocop:disable Lint/DuplicateBranch
200
+ infinitive.to_s
201
+ end.dup.concat('ing').to_sym
202
+ end
203
+
204
+ # Forming verb to apply past tense endings
205
+ # Params:
206
+ # * infinitive, the given verb
207
+ def past_participle(infinitive)
208
+ if (verb = conjugations.irregulars[infinitive])
209
+ conjugate_irregular(verb, tense: :past, derivative: :participle)
210
+ else
211
+ regular_preterite_for infinitive
212
+ end
213
+ end
214
+
215
+ #
216
+ # Params:
217
+ # * verb,
218
+ # * options,
219
+ def conjugate_irregular(verb, options)
220
+ return verb[options] if verb[options]
221
+
222
+ tense = options[:tense]
223
+ person = options[:person]
224
+ plurality = options[:plurality]
225
+ derivative = options[:derivative]
226
+
227
+ if [tense, person, plurality] == %i[present third singular]
228
+ present_third_person_singular_form_for verb
229
+ elsif [tense, derivative] == %i[past participle]
230
+ verb.past_participle
231
+ elsif tense == :present
232
+ verb.infinitive
233
+ elsif tense == :past
234
+ verb.preterite
235
+ end
236
+ end
237
+
238
+ # Apply thir person rules to the verb for the conjugation
239
+ # Params:
240
+ # * verb, apply proper third person rules to this
241
+ def present_third_person_singular_form_for(verb)
242
+ infinitive = verb.is_a?(Verb) ? verb.infinitive.to_s : verb.to_s
243
+
244
+ case infinitive
245
+ when /[a-z&&#{CONSONANT_PATTERN}]y$/i
246
+ "#{infinitive[0..-2]}ies"
247
+ when /(ss|sh|t?ch|zz|x|#{CONSONANT_PATTERN}o)$/i
248
+ "#{infinitive}es"
249
+ when /[^s]s$/i
250
+ "#{infinitive}ses"
251
+ else
252
+ "#{infinitive}s"
253
+ end
254
+ end
255
+
256
+ # Apply the regular past tense to a given verb for the conjugation
257
+ # Params:
258
+ # * verb, apply regular past tense rules to this
259
+ def regular_preterite_for(verb)
260
+ infinitive = verb.is_a?(Verb) ? verb.infinitive.to_s : verb.to_s
261
+
262
+ if verb.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{DOUBLED_CONSONANT_WITHOUT_C_PATTERN}$/) &&
263
+ !conjugations.single_terminal_consonants.include?(verb.to_sym)
264
+ return regular_preterite_with_doubled_terminal_consonant_for verb
265
+ end
266
+
267
+ case verb.to_s
268
+ when /(#{CONSONANT_PATTERN}e|ye|oe|nge|ie|ee|ue)$/
269
+ infinitive.concat('d').to_sym
270
+ when /#{CONSONANT_PATTERN}y$/
271
+ infinitive.gsub(/y$/, 'ied').to_sym
272
+ when /c$/
273
+ infinitive.gsub(/c$/, 'cked').to_sym
274
+ else
275
+ infinitive.concat('ed').to_sym
276
+ end
277
+ end
278
+
279
+ # Apply proper rules to consonant endings
280
+ # Params:
281
+ # * verb, apply double consonant to this
282
+ def regular_preterite_with_doubled_terminal_consonant_for(verb)
283
+ regular_preterite_for verb.to_s.concat(verb.to_s[-1, 1]).to_sym
284
+ end
285
+
286
+ # Apply proper rules to consonant endings
287
+ # Params:
288
+ # * verb, apply double consonant to this
289
+ def present_participle_with_doubled_terminal_consonant_for(verb)
290
+ if /c$/ =~ verb.to_s
291
+ present_participle verb.to_sym
292
+ else
293
+ present_participle verb.to_s.concat(verb.to_s[-1, 1]).to_sym
294
+ end
295
+ end
296
+
297
+ # Add appropriate aspects to the tense of the conjugation
298
+ # Params:
299
+ # * tense, an option given by the user
300
+ # * aspect, an option given by the user
301
+ # * diathesis, an option given by the user
302
+ def form_for(tense, aspect, diathesis)
303
+ form = []
304
+ case diathesis
305
+ when :active
306
+ if tense == :future
307
+ form << 'will'
308
+ form << :infinitive if aspect == :habitual
309
+ form.concat ['have', :past_participle] if aspect == :perfect
310
+ form.concat ['be having', :past_participle] if aspect == :perfective
311
+ form.concat ['be', :present_participle] if aspect == :progressive
312
+ form.concat ['be about to', :infinitive] if aspect == :prospective
313
+ else
314
+ form.concat ['used to', :infinitive] if [tense, aspect] == %i[past habitual]
315
+ form.concat %i[have past_participle] if aspect == :perfect
316
+ form << :past if [tense, aspect] == %i[past perfective]
317
+ form.concat %i[be present_participle] if aspect == :progressive
318
+ form.concat [:be, 'about to', :infinitive] if aspect == :prospective
319
+ form << :present if [tense, aspect] == %i[present habitual]
320
+ form.concat [:be, 'having', :past_participle] if [tense, aspect] == %i[present perfective]
321
+ end
322
+ when :passive
323
+ if tense == :future
324
+ form << 'will'
325
+ form.concat ['be', :past_participle] if aspect == :habitual
326
+ form.concat ['have been', :past_participle] if aspect == :perfect
327
+ form.concat ['be being', :past_participle] if aspect == :progressive
328
+ form.concat ['be about to be', :past_participle] if aspect == :prospective
329
+ else
330
+ form.concat ['used to be', :past_participle] if [tense, aspect] == %i[past habitual]
331
+ form.concat [:have, 'been', :past_participle] if aspect == :perfect
332
+ form.concat %i[be past_participle] if [tense, aspect] == %i[past perfective]
333
+ form.concat [:be, 'being', :past_participle] if aspect == :progressive
334
+ form.concat [:be, 'about to be', :past_participle] if aspect == :prospective
335
+ form.concat %i[be past_participle] if [tense, aspect] == %i[present habitual]
336
+ end
337
+ end
338
+ form
339
+ end
340
+
341
+ # Confirm an imperative mood contains the present tense and second person
342
+ # Params:
343
+ # * tense, an option given by the user
344
+ # * person, how the conjugation refers to the subject
345
+ # * mood, an option given by the user
346
+ # * diathesis, an option given by the user
347
+ def check_for_improper_constructions(infinitive, tense, person, mood, diathesis)
348
+ if (mood == :imperative) && !((person == :second) && (tense == :present))
349
+ raise Verbs::ImproperConstruction, 'The imperative mood requires present tense and second person'
350
+ end
351
+ if (infinitive.to_sym == :be) && (diathesis == :passive)
352
+ raise Verbs::ImproperConstruction, 'There is no passive diathesis for the copula'
353
+ end
354
+ end
355
+
356
+ def default_aspect(options)
357
+ options[:tense] == :past ? :perfective : :habitual
358
+ end
359
+ end
360
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verbs
4
+ class ImproperConstruction < ArgumentError
5
+ end
6
+ end
data/lib/verbs/verb.rb ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verbs
4
+ class Verb
5
+ attr_reader :infinitive, :preterite, :past_participle
6
+
7
+ def initialize(infinitive, options = {})
8
+ @infinitive = infinitive
9
+ @forms = {}
10
+ if block_given?
11
+ yield self
12
+ else
13
+ @preterite = options[:preterite]
14
+ @past_participle = options[:past_participle]
15
+ end
16
+ end
17
+
18
+ def form(word, options = {})
19
+ unless options[:tense] && (options[:derivative] || options[:mood] || (options[:person] && options[:plurality]))
20
+ raise ArgumentError,
21
+ 'Irregular verb specifications must identify tense and must identify either derivative, mood, or person and plurality'
22
+ end
23
+
24
+ tense = options[:tense]
25
+
26
+ @forms[:present] ||= {}
27
+ @forms[:past] ||= {}
28
+ if (derivative = options[:derivative])
29
+ @forms[tense][derivative] = word
30
+ elsif (mood = options[:mood])
31
+ @forms[tense][mood] = word
32
+ elsif (person = options[:person])
33
+ @forms[tense][person] ||= {}
34
+ @forms[tense][person][options[:plurality]] = word
35
+ end
36
+ end
37
+
38
+ def [](options = {})
39
+ tense = options[:tense]
40
+ person = options[:person]
41
+ plurality = options[:plurality]
42
+ derivative = options[:derivative]
43
+ mood = options[:mood]
44
+ if tense && person && plurality && mood
45
+ @forms[tense].try(:[], mood) || @forms[tense].try(:[], person).try(:[], plurality)
46
+ elsif tense && derivative
47
+ @forms[tense].try(:[], derivative)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verbs
4
+ module Verblike
5
+ class Wrapper
6
+ def initialize(base)
7
+ @base = base
8
+ end
9
+
10
+ def conjugate(options)
11
+ words = @base.to_s.split
12
+ words.shift if words.first.downcase == 'to'
13
+ infinitive = words.shift.downcase.to_sym
14
+ conjugation = ::Verbs::Conjugator.conjugate infinitive, options
15
+ conjugated = words.unshift(conjugation.to_s).join(' ')
16
+ @base.is_a?(Symbol) ? conjugated.to_sym : conjugated
17
+ end
18
+ end
19
+
20
+ module Access
21
+ def verb
22
+ ::Verbs::Verblike::Wrapper.new self
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ # class String
29
+ # include ::Verbs::Verblike::Access
30
+ # end
31
+ #
32
+ # class Symbol
33
+ # include ::Verbs::Verblike::Access
34
+ # end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verbs
4
+ VERSION = '3.1.1'
5
+ end
data/lib/verbs.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'time'
5
+ require 'active_support'
6
+ require 'active_support/core_ext'
7
+ require 'verbs/verb'
8
+ require 'verbs/conjugator'
9
+ require 'verbs/conjugations'
10
+ require 'verbs/verblike'
11
+ require 'verbs/improper_construction'
12
+
13
+ module Verbs
14
+ CONSONANTS = %w[b c d f g h j k l m n p q r s t v w x z].freeze
15
+ CONSONANT_PATTERN = "[#{CONSONANTS.join}]"
16
+ CONSONANTS_WITHOUT_C = (CONSONANTS - ['c']).freeze
17
+ CONSONANTS_WITHOUT_C_PATTERN = "[#{CONSONANTS_WITHOUT_C.join}]"
18
+ DOUBLED_CONSONANTS = (CONSONANTS - %w[v w x]).freeze
19
+ DOUBLED_CONSONANT_PATTERN = "[#{DOUBLED_CONSONANTS.join}]"
20
+ DOUBLED_CONSONANTS_WITHOUT_C = (DOUBLED_CONSONANTS - ['c']).freeze
21
+ DOUBLED_CONSONANT_WITHOUT_C_PATTERN = "[#{DOUBLED_CONSONANTS_WITHOUT_C.join}]"
22
+ VOWELS = %w[a e i o u y].freeze
23
+ VOWEL_PATTERN = "[#{VOWELS.join}]"
24
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :release do
4
+ desc 'generate changelog'
5
+ task :generate_changelog do |_task, _args|
6
+ `github_changelog_generator -u rossmeissl -p verbs`
7
+ end
8
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'verbs'