verbs 2.0.9 → 2.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,213 +1,213 @@
1
- module Verbs
2
- module Conjugator
3
- extend self
4
-
5
- class Conjugations
6
- include Singleton
7
-
8
- attr_reader :irregulars, :single_terminal_consonants, :copulars
9
-
10
- def initialize
11
- @irregulars, @single_terminal_consonants, @copulars = {}, [], {}
12
- end
13
-
14
- def irregular(infinitive, preterite = nil, past_participle = nil, &blk)
15
- if block_given?
16
- irregular = ::Verbs::Verb.new infinitive, &blk
17
- else
18
- raise ArgumentError, "Standard irregular verbs must specify preterite and past participle forms" unless preterite and past_participle
19
- irregular = ::Verbs::Verb.new infinitive, :preterite => preterite, :past_participle => past_participle
20
- end
21
- @irregulars[infinitive] = irregular
22
- end
23
-
24
- def single_terminal_consonant(infinitive)
25
- @single_terminal_consonants << infinitive
26
- end
27
- end
28
-
29
- def conjugations
30
- if block_given?
31
- yield Conjugations.instance
32
- else
33
- Conjugations.instance
34
- end
35
- end
36
-
37
- def conjugate(infinitive, options = {})
38
- tense = options[:tense] || :present # present, past, future
39
- person = options[:person] || :third # first, second, third
40
- plurality = options[:plurality] || :singular # singular, plural
41
- diathesis = options[:diathesis] || :active # active, passive
42
- mood = options[:mood] || :indicative # conditional, imperative, indicative, injunctive, optative, potential, subjunctive
43
- aspect = options[:aspect] || :habitual # perfective, habitual, progressive, perfect, prospective
44
-
45
- form = form_for(tense, aspect)
46
-
47
- conjugation = form.map { |e| resolve e, infinitive, tense, person, plurality }.join(' ').strip
48
-
49
- if options[:subject]
50
- actor = options.delete(:subject)
51
- actor = subject(options).humanize if actor.is_a?(TrueClass)
52
- end
53
-
54
- "#{actor} #{conjugation}".strip
55
- end
56
-
57
- def subject(options)
58
- case [options[:person], options[:plurality]]
59
- when [:first, :singular]
60
- 'I'
61
- when [:first, :plural]
62
- 'we'
63
- when [:second, :singular], [:second, :plural]
64
- 'you'
65
- when [:third, :singular]
66
- 'he'
67
- when [:third, :plural]
68
- 'they'
69
- end
70
- end
71
-
72
- private
73
-
74
- def resolve(element, infinitive, tense, person, plurality)
75
- case element
76
- when String
77
- element
78
- when :infinitive
79
- infinitive
80
- when :present, :past, :present_participle, :past_participle
81
- inflect infinitive, element, person, plurality
82
- when Symbol
83
- inflect element, tense, person, plurality
84
- end
85
- end
86
-
87
- def inflect(infinitive, inflection, person, plurality)
88
- send(*([inflection, infinitive, person, plurality][0, method(inflection).arity + 1]))
89
- end
90
-
91
- def present(infinitive, person, plurality)
92
- if verb = conjugations.irregulars[infinitive]
93
- conjugate_irregular(verb, :tense => :present, :person => person, :plurality => plurality)
94
- elsif person == :third and plurality == :singular
95
- present_third_person_singular_form_for infinitive
96
- else
97
- infinitive
98
- end
99
- end
100
-
101
- def past(infinitive, person, plurality)
102
- if verb = conjugations.irregulars[infinitive]
103
- conjugate_irregular(verb, :tense => :past, :person => person, :plurality => plurality)
104
- else
105
- regular_preterite_for infinitive
106
- end
107
- end
108
-
109
- def present_participle(infinitive)
110
- if infinitive.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{CONSONANT_PATTERN}$/) and !conjugations.single_terminal_consonants.include?(infinitive)
111
- present_participle_with_doubled_terminal_consonant_for infinitive
112
- elsif infinitive.to_s.match(/c$/)
113
- infinitive.to_s.concat('king').to_sym
114
- elsif infinitive.to_s.match(/ye$/) or infinitive.to_s.match(/oe$/) or infinitive.to_s.match(/nge$/) or infinitive.to_s.match(/ee$/)
115
- infinitive.to_s.concat('ing').to_sym
116
- elsif infinitive.to_s.match(/ie$/)
117
- infinitive.to_s[0..-2].concat('ying').to_sym
118
- else
119
- infinitive.to_s[0..-1].concat('ing').to_sym
120
- end
121
- end
122
-
123
- def past_participle(infinitive)
124
- if verb = conjugations.irregulars[infinitive]
125
- conjugate_irregular(verb, :tense => :past, :derivative => :participle)
126
- else
127
- regular_preterite_for infinitive
128
- end
129
- end
130
-
131
- def conjugate_irregular(verb, options)
132
- return verb[options] if verb[options]
133
-
134
- tense = options[:tense]
135
- person = options[:person]
136
- plurality = options[:plurality]
137
- derivative = options[:derivative]
138
-
139
- if [tense, person, plurality] == [:present, :third, :singular]
140
- present_third_person_singular_form_for verb
141
- elsif [tense, derivative] == [:past, :participle]
142
- verb.past_participle
143
- elsif tense == :present
144
- verb.infinitive
145
- elsif tense == :past
146
- verb.preterite
147
- end
148
- end
149
-
150
- def present_third_person_singular_form_for(verb)
151
- infinitive = case verb
152
- when Verb
153
- verb.infinitive
154
- when String, Symbol
155
- verb.to_sym
156
- end
157
- if infinitive.to_s.match(/#{CONSONANT_PATTERN}y$/)
158
- infinitive.to_s.gsub(/y$/, 'ies').to_sym
159
- elsif infinitive.to_s.match(/[szx]$/) or infinitive.to_s.match(/[sc]h$/)
160
- infinitive.to_s.concat('es').to_sym
161
- else
162
- infinitive.to_s.concat('s').to_sym
163
- end
164
- end
165
-
166
- def regular_preterite_for(verb)
167
- infinitive = case verb
168
- when Verb
169
- verb.infinitive
170
- when String, Symbol
171
- verb.to_sym
172
- end
173
- if verb.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{DOUBLED_CONSONANT_PATTERN}$/) and !conjugations.single_terminal_consonants.include?(verb)
174
- regular_preterite_with_doubled_terminal_consonant_for verb
175
- elsif verb.to_s.match(/#{CONSONANT_PATTERN}e$/) or verb.to_s.match(/ye$/) or verb.to_s.match(/oe$/) or verb.to_s.match(/nge$/) or verb.to_s.match(/ie$/) or verb.to_s.match(/ee$/)
176
- infinitive.to_s.concat('d').to_sym
177
- elsif verb.to_s.match(/#{CONSONANT_PATTERN}y$/)
178
- infinitive.to_s.chomp('y').concat('ied').to_sym
179
- else
180
- infinitive.to_s.concat('ed').to_sym
181
- end
182
- end
183
-
184
- def regular_preterite_with_doubled_terminal_consonant_for(verb)
185
- regular_preterite_for verb.to_s.concat(verb.to_s[-1,1]).to_sym
186
- end
187
-
188
- def present_participle_with_doubled_terminal_consonant_for(verb)
189
- present_participle verb.to_s.concat(verb.to_s[-1,1]).to_sym
190
- end
191
-
192
- def form_for(tense, aspect)
193
- form = []
194
- if tense == :future
195
- form << 'will'
196
- form << :infinitive if aspect == :habitual
197
- form.concat ['have', :past_participle] if aspect == :perfect
198
- form.concat ['be having', :past_participle] if aspect == :perfective
199
- form.concat ['be', :present_participle] if aspect == :progressive
200
- form.concat ['be about to', :infinitive] if aspect == :prospective
201
- else
202
- form.concat ['usually', :past_participle] if [tense, aspect] == [:past, :habitual]
203
- form.concat [:have, :past_participle] if aspect == :perfect
204
- form << :past if [tense, aspect] == [:past, :perfective]
205
- form.concat [:be, :present_participle] if aspect == :progressive
206
- form.concat [:be, 'about to', :infinitive] if aspect == :prospective
207
- form << :present if [tense, aspect] == [:present, :habitual]
208
- form.concat [:be, 'having', :past_participle] if [tense, aspect] == [:present, :perfective]
209
- end
210
- form
211
- end
212
- end
213
- end
1
+ module Verbs
2
+ module Conjugator
3
+ extend self
4
+
5
+ class Conjugations
6
+ include Singleton
7
+
8
+ attr_reader :irregulars, :single_terminal_consonants, :copulars
9
+
10
+ def initialize
11
+ @irregulars, @single_terminal_consonants, @copulars = {}, [], {}
12
+ end
13
+
14
+ def irregular(infinitive, preterite = nil, past_participle = nil, &blk)
15
+ if block_given?
16
+ irregular = ::Verbs::Verb.new infinitive, &blk
17
+ else
18
+ raise ArgumentError, "Standard irregular verbs must specify preterite and past participle forms" unless preterite and past_participle
19
+ irregular = ::Verbs::Verb.new infinitive, :preterite => preterite, :past_participle => past_participle
20
+ end
21
+ @irregulars[infinitive] = irregular
22
+ end
23
+
24
+ def single_terminal_consonant(infinitive)
25
+ @single_terminal_consonants << infinitive
26
+ end
27
+ end
28
+
29
+ def conjugations
30
+ if block_given?
31
+ yield Conjugations.instance
32
+ else
33
+ Conjugations.instance
34
+ end
35
+ end
36
+
37
+ def conjugate(infinitive, options = {})
38
+ tense = options[:tense] || :present # present, past, future
39
+ person = options[:person] || :third # first, second, third
40
+ plurality = options[:plurality] || :singular # singular, plural
41
+ diathesis = options[:diathesis] || :active # active, passive
42
+ mood = options[:mood] || :indicative # conditional, imperative, indicative, injunctive, optative, potential, subjunctive
43
+ aspect = options[:aspect] || :habitual # perfective, habitual, progressive, perfect, prospective
44
+
45
+ form = form_for(tense, aspect)
46
+
47
+ conjugation = form.map { |e| resolve e, infinitive, tense, person, plurality }.join(' ').strip
48
+
49
+ if options[:subject]
50
+ actor = options.delete(:subject)
51
+ actor = subject(options).humanize if actor.is_a?(TrueClass)
52
+ end
53
+
54
+ "#{actor} #{conjugation}".strip
55
+ end
56
+
57
+ def subject(options)
58
+ case [options[:person], options[:plurality]]
59
+ when [:first, :singular]
60
+ 'I'
61
+ when [:first, :plural]
62
+ 'we'
63
+ when [:second, :singular], [:second, :plural]
64
+ 'you'
65
+ when [:third, :singular]
66
+ 'he'
67
+ when [:third, :plural]
68
+ 'they'
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def resolve(element, infinitive, tense, person, plurality)
75
+ case element
76
+ when String
77
+ element
78
+ when :infinitive
79
+ infinitive
80
+ when :present, :past, :present_participle, :past_participle
81
+ inflect infinitive, element, person, plurality
82
+ when Symbol
83
+ inflect element, tense, person, plurality
84
+ end
85
+ end
86
+
87
+ def inflect(infinitive, inflection, person, plurality)
88
+ send(*([inflection, infinitive, person, plurality][0, method(inflection).arity + 1]))
89
+ end
90
+
91
+ def present(infinitive, person, plurality)
92
+ if verb = conjugations.irregulars[infinitive]
93
+ conjugate_irregular(verb, :tense => :present, :person => person, :plurality => plurality)
94
+ elsif person == :third and plurality == :singular
95
+ present_third_person_singular_form_for infinitive
96
+ else
97
+ infinitive
98
+ end
99
+ end
100
+
101
+ def past(infinitive, person, plurality)
102
+ if verb = conjugations.irregulars[infinitive]
103
+ conjugate_irregular(verb, :tense => :past, :person => person, :plurality => plurality)
104
+ else
105
+ regular_preterite_for infinitive
106
+ end
107
+ end
108
+
109
+ def present_participle(infinitive)
110
+ if infinitive.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{CONSONANT_PATTERN}$/) and !conjugations.single_terminal_consonants.include?(infinitive)
111
+ present_participle_with_doubled_terminal_consonant_for infinitive
112
+ elsif infinitive.to_s.match(/c$/)
113
+ infinitive.to_s.concat('king').to_sym
114
+ elsif infinitive.to_s.match(/ye$/) or infinitive.to_s.match(/oe$/) or infinitive.to_s.match(/nge$/) or infinitive.to_s.match(/ee$/)
115
+ infinitive.to_s.concat('ing').to_sym
116
+ elsif infinitive.to_s.match(/ie$/)
117
+ infinitive.to_s[0..-2].concat('ying').to_sym
118
+ else
119
+ infinitive.to_s[0..-1].concat('ing').to_sym
120
+ end
121
+ end
122
+
123
+ def past_participle(infinitive)
124
+ if verb = conjugations.irregulars[infinitive]
125
+ conjugate_irregular(verb, :tense => :past, :derivative => :participle)
126
+ else
127
+ regular_preterite_for infinitive
128
+ end
129
+ end
130
+
131
+ def conjugate_irregular(verb, options)
132
+ return verb[options] if verb[options]
133
+
134
+ tense = options[:tense]
135
+ person = options[:person]
136
+ plurality = options[:plurality]
137
+ derivative = options[:derivative]
138
+
139
+ if [tense, person, plurality] == [:present, :third, :singular]
140
+ present_third_person_singular_form_for verb
141
+ elsif [tense, derivative] == [:past, :participle]
142
+ verb.past_participle
143
+ elsif tense == :present
144
+ verb.infinitive
145
+ elsif tense == :past
146
+ verb.preterite
147
+ end
148
+ end
149
+
150
+ def present_third_person_singular_form_for(verb)
151
+ infinitive = case verb
152
+ when Verb
153
+ verb.infinitive
154
+ when String, Symbol
155
+ verb.to_sym
156
+ end
157
+ if infinitive.to_s.match(/#{CONSONANT_PATTERN}y$/)
158
+ infinitive.to_s.gsub(/y$/, 'ies').to_sym
159
+ elsif infinitive.to_s.match(/[szx]$/) or infinitive.to_s.match(/[sc]h$/)
160
+ infinitive.to_s.concat('es').to_sym
161
+ else
162
+ infinitive.to_s.concat('s').to_sym
163
+ end
164
+ end
165
+
166
+ def regular_preterite_for(verb)
167
+ infinitive = case verb
168
+ when Verb
169
+ verb.infinitive
170
+ when String, Symbol
171
+ verb.to_sym
172
+ end
173
+ if verb.to_s.match(/#{CONSONANT_PATTERN}#{VOWEL_PATTERN}#{DOUBLED_CONSONANT_PATTERN}$/) and !conjugations.single_terminal_consonants.include?(verb)
174
+ regular_preterite_with_doubled_terminal_consonant_for verb
175
+ elsif verb.to_s.match(/#{CONSONANT_PATTERN}e$/) or verb.to_s.match(/ye$/) or verb.to_s.match(/oe$/) or verb.to_s.match(/nge$/) or verb.to_s.match(/ie$/) or verb.to_s.match(/ee$/)
176
+ infinitive.to_s.concat('d').to_sym
177
+ elsif verb.to_s.match(/#{CONSONANT_PATTERN}y$/)
178
+ infinitive.to_s.chomp('y').concat('ied').to_sym
179
+ else
180
+ infinitive.to_s.concat('ed').to_sym
181
+ end
182
+ end
183
+
184
+ def regular_preterite_with_doubled_terminal_consonant_for(verb)
185
+ regular_preterite_for verb.to_s.concat(verb.to_s[-1,1]).to_sym
186
+ end
187
+
188
+ def present_participle_with_doubled_terminal_consonant_for(verb)
189
+ present_participle verb.to_s.concat(verb.to_s[-1,1]).to_sym
190
+ end
191
+
192
+ def form_for(tense, aspect)
193
+ form = []
194
+ if tense == :future
195
+ form << 'will'
196
+ form << :infinitive if aspect == :habitual
197
+ form.concat ['have', :past_participle] if aspect == :perfect
198
+ form.concat ['be having', :past_participle] if aspect == :perfective
199
+ form.concat ['be', :present_participle] if aspect == :progressive
200
+ form.concat ['be about to', :infinitive] if aspect == :prospective
201
+ else
202
+ form.concat ['usually', :past_participle] if [tense, aspect] == [:past, :habitual]
203
+ form.concat [:have, :past_participle] if aspect == :perfect
204
+ form << :past if [tense, aspect] == [:past, :perfective]
205
+ form.concat [:be, :present_participle] if aspect == :progressive
206
+ form.concat [:be, 'about to', :infinitive] if aspect == :prospective
207
+ form << :present if [tense, aspect] == [:present, :habitual]
208
+ form.concat [:be, 'having', :past_participle] if [tense, aspect] == [:present, :perfective]
209
+ end
210
+ form
211
+ end
212
+ end
213
+ end
data/lib/verbs/verb.rb CHANGED
@@ -1,40 +1,40 @@
1
- module Verbs
2
- class Verb
3
- attr_reader :infinitive, :preterite, :past_participle
4
-
5
- def initialize(infinitive, options = {}, &blk)
6
- @infinitive = infinitive
7
- @forms = {}
8
- if block_given?
9
- yield self
10
- else
11
- @preterite = options[:preterite]
12
- @past_participle = options[:past_participle]
13
- end
14
- end
15
-
16
- def form(word, options = {})
17
- raise ArgumentError, 'Irregular verb specifications must identify tense and must identify either derivative or person and plurality' unless options[:tense] and (options[:derivative] or (options[:person] and options[:plurality]))
18
-
19
- tense = options[:tense]
20
-
21
- @forms[:present] ||= {}
22
- @forms[:past] ||= {}
23
- if derivative = options[:derivative]
24
- @forms[tense][derivative] = word
25
- elsif person = options[:person]
26
- @forms[tense][person] ||= {}
27
- @forms[tense][person][options[:plurality]] = word
28
- end
29
- end
30
-
31
- def [](options = {})
32
- tense, person, plurality, derivative = options[:tense], options[:person], options[:plurality], options[:derivative]
33
- if tense and person and plurality
34
- @forms[tense].andand[person].andand[plurality]
35
- elsif tense and derivative
36
- @forms[tense].andand[derivative]
37
- end
38
- end
39
- end
40
- end
1
+ module Verbs
2
+ class Verb
3
+ attr_reader :infinitive, :preterite, :past_participle
4
+
5
+ def initialize(infinitive, options = {}, &blk)
6
+ @infinitive = infinitive
7
+ @forms = {}
8
+ if block_given?
9
+ yield self
10
+ else
11
+ @preterite = options[:preterite]
12
+ @past_participle = options[:past_participle]
13
+ end
14
+ end
15
+
16
+ def form(word, options = {})
17
+ raise ArgumentError, 'Irregular verb specifications must identify tense and must identify either derivative or person and plurality' unless options[:tense] and (options[:derivative] or (options[:person] and options[:plurality]))
18
+
19
+ tense = options[:tense]
20
+
21
+ @forms[:present] ||= {}
22
+ @forms[:past] ||= {}
23
+ if derivative = options[:derivative]
24
+ @forms[tense][derivative] = word
25
+ elsif person = options[:person]
26
+ @forms[tense][person] ||= {}
27
+ @forms[tense][person][options[:plurality]] = word
28
+ end
29
+ end
30
+
31
+ def [](options = {})
32
+ tense, person, plurality, derivative = options[:tense], options[:person], options[:plurality], options[:derivative]
33
+ if tense and person and plurality
34
+ @forms[tense].try(:[], person).try(:[], plurality)
35
+ elsif tense and derivative
36
+ @forms[tense].try(:[], derivative)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,32 +1,32 @@
1
- module Verbs
2
- module Verblike
3
- class Wrapper
4
- def initialize(base)
5
- @base = base
6
- end
7
-
8
- def conjugate(options)
9
- words = @base.to_s.split(' ')
10
- words.shift if words.first.downcase == 'to'
11
- infinitive = words.shift.to_sym
12
- conjugation = ::Verbs::Conjugator.conjugate infinitive, options
13
- conjugated = words.unshift(conjugation.to_s).join(' ')
14
- @base.is_a?(Symbol) ? conjugated.to_sym : conjugated
15
- end
16
- end
17
-
18
- module Access
19
- def verb
20
- ::Verbs::Verblike::Wrapper.new self
21
- end
22
- end
23
- end
24
- end
25
-
26
- class String
27
- include ::Verbs::Verblike::Access
28
- end
29
-
30
- class Symbol
31
- include ::Verbs::Verblike::Access
32
- end
1
+ module Verbs
2
+ module Verblike
3
+ class Wrapper
4
+ def initialize(base)
5
+ @base = base
6
+ end
7
+
8
+ def conjugate(options)
9
+ words = @base.to_s.split(' ')
10
+ words.shift if words.first.downcase == 'to'
11
+ infinitive = words.shift.to_sym
12
+ conjugation = ::Verbs::Conjugator.conjugate infinitive, options
13
+ conjugated = words.unshift(conjugation.to_s).join(' ')
14
+ @base.is_a?(Symbol) ? conjugated.to_sym : conjugated
15
+ end
16
+ end
17
+
18
+ module Access
19
+ def verb
20
+ ::Verbs::Verblike::Wrapper.new self
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ class String
27
+ include ::Verbs::Verblike::Access
28
+ end
29
+
30
+ class Symbol
31
+ include ::Verbs::Verblike::Access
32
+ end
data/lib/verbs.rb CHANGED
@@ -1,16 +1,16 @@
1
- require 'singleton'
2
- require 'verbs/verb'
3
- require 'verbs/conjugator'
4
- require 'verbs/conjugations'
5
- require 'verbs/verblike'
6
- require 'andand'
7
- require 'active_support'
8
-
9
- module Verbs
10
- CONSONANTS = %w(b c d f g h j k l m n p q r s t v w x z)
11
- CONSONANT_PATTERN = "[#{CONSONANTS.join}]"
12
- DOUBLED_CONSONANTS = %w(b c d f g h j k l m n p q r s t w z)
13
- DOUBLED_CONSONANT_PATTERN = "[#{DOUBLED_CONSONANTS.join}]"
14
- VOWELS = %w(a e i o u y)
15
- VOWEL_PATTERN = "[#{VOWELS.join}]"
16
- end
1
+ require 'singleton'
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'verbs/verb'
5
+ require 'verbs/conjugator'
6
+ require 'verbs/conjugations'
7
+ require 'verbs/verblike'
8
+
9
+ module Verbs
10
+ CONSONANTS = %w(b c d f g h j k l m n p q r s t v w x z)
11
+ CONSONANT_PATTERN = "[#{CONSONANTS.join}]"
12
+ DOUBLED_CONSONANTS = %w(b c d f g h j k l m n p q r s t w z)
13
+ DOUBLED_CONSONANT_PATTERN = "[#{DOUBLED_CONSONANTS.join}]"
14
+ VOWELS = %w(a e i o u y)
15
+ VOWEL_PATTERN = "[#{VOWELS.join}]"
16
+ end
data/test/test_verbs.rb CHANGED
@@ -1,11 +1,4 @@
1
1
  require 'helper'
2
- require 'active_support/version'
3
- %w{
4
- active_support/core_ext/object/misc
5
- active_support/inflector
6
- }.each do |active_support_3_requirement|
7
- require active_support_3_requirement
8
- end if ActiveSupport::VERSION::MAJOR == 3
9
2
 
10
3
  class TestVerbs < Test::Unit::TestCase
11
4
  def test_copular_conjugation