jido 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/jido.rb +1 -0
  3. data/lib/jido/conjugator.rb +93 -9
  4. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -1,5 +1,6 @@
1
1
  require 'jido/conjugator'
2
2
 
3
+ # Container for the conjugator. Maybe other stuff will go in here later..
3
4
  module Jido
4
5
  # Convenience method for Jido::Conjugator.load
5
6
  def Jido.load lang, options = {}
@@ -2,11 +2,27 @@ require 'rubygems'
2
2
  require 'nokogiri'
3
3
 
4
4
  module Jido
5
+
6
+ # This is it, folks.
5
7
  class Conjugator
8
+
9
+ # The language used in this Conjugator instance.
10
+ # Verbs given to this instance are expected to be in this language.
11
+ # This instance will conjugate verbs according to rules of this language.
6
12
  attr_reader :lang
7
13
 
8
- # Accepted options:
9
- #
14
+ # Create a Jido::Conjugator instance.
15
+ # Load and parse the corresponding XML data file, and parse any provided options.
16
+ #
17
+ # Accepted options (keys of the `options` hash should be symbols, not strings):
18
+ # * <code>:forms</code>: Only return conjugations for the given verb forms / tenses.
19
+ # Jido::Conjugator.new 'fr', :forms => %w{prs futant}
20
+ # * <code>:paradigms</code>: Only return conjugations for the given paradigms.
21
+ # Jido::Conjugator.new 'fr', :paradigms => [{:person => '1', :quant => 'sg'}]
22
+ # * <code>:forms_except</code>: Return all conjugations except those for the given verb forms / tenses.
23
+ # Jido::Conjugator.new 'fr', :forms_except => 'prs'
24
+ # * <code>:paradigms_except</code>: Return all conjugations except those for the given paradigms.
25
+ # Jido::Conjugator.new 'fr', :paradigms_except => [{:person => '3', :quant => 'pl'}]
10
26
  def initialize lang, options = {}
11
27
  @lang = lang
12
28
 
@@ -21,10 +37,35 @@ module Jido
21
37
 
22
38
  @data = Nokogiri.XML data_file, nil, 'UTF-8'
23
39
  data_file.close
40
+
41
+ self.options = options
42
+ end
43
+
44
+ # Change the options for this Conjugator instance.
45
+ # See Conjugator::new for possible options.
46
+ def options= options
47
+ @options = options
48
+ @forms = check_for_list_option :forms
49
+ @forms_except = check_for_list_option(:forms_except) || [ ]
50
+ @paradigms = check_for_list_option :paradigms
51
+ @paradigms_except = check_for_list_option(:paradigms_except) || [ ]
52
+ end
53
+
54
+ # Interpret the provided option when a list is expected.
55
+ # Used to provide functionality like:
56
+ # jido.conjugate 'be', :form => 'prs'
57
+ # jido.conjugate 'be', :form => %w{prs pst prf}
58
+ def check_for_list_option option_name
59
+ return nil if @options[option_name].nil?
60
+
61
+ return [@options[option_name]] if @options[option_name].is_a?(String) or @options[option_name].is_a(Hash)
62
+ return @options[option_name] if @options[option_name].is_a?(Array)
63
+
64
+ raise "Invalid data type provided for option #{option_name}: a list was expected. Please provide a single string element or an array of strings."
24
65
  end
25
66
 
26
67
  # Get the possible verb form IDs for any conjugated verb.
27
- # Jido.load('fr').forms # => ['PRS', 'PCOMP', 'IMP', ...]
68
+ # Jido.load('fr').forms # => ['PRS', 'PCOMP', 'IMP', ...]
28
69
  def forms
29
70
  if @forms.nil?
30
71
  @forms = []
@@ -36,6 +77,23 @@ module Jido
36
77
  @forms
37
78
  end
38
79
 
80
+ # Set the forms to conjugate. Use Conjugator#forms to find possible values for a certain instance.
81
+ # jido.forms = %w{prs pst imp} # conjugate only for present, past, and imperfect tenses
82
+ # jido.forms = 'futant' # conjugate only for future anterior tense
83
+ # jido.forms = jido.forms[0..4] # conjugate for the first 5 forms stored in the conjugator
84
+ def forms= forms
85
+ @options[:forms] = forms
86
+ @forms = check_for_list_option :forms
87
+ end
88
+
89
+ # Set the forms to not conjugate. Use Conjugator#forms to find possible values for a certain instance.
90
+ # jido.forms_except = 'prs' # conjugate for all forms but the present tense
91
+ def forms_except= forms_except
92
+ @options[:forms_except] = forms_except
93
+ @forms_except = check_for_list_option :forms_except
94
+ end
95
+
96
+ # Get the fallbacks for this language, in case an exact match for a given verb is not found.
39
97
  def fallbacks
40
98
  if @fallbacks.nil?
41
99
  @fallbacks = []
@@ -47,6 +105,12 @@ module Jido
47
105
  @fallbacks
48
106
  end
49
107
 
108
+ # Get the possible paradigm IDs for any conjugated verb.
109
+ # Each paradigm is a hash, with two keys:
110
+ # * <code>:person</code>
111
+ # * <code>:quant</code>
112
+ #
113
+ # Jido.load('fr').paradigms # => [{:person => '1', :quant => 'sg'}, {:person => '1', :quant => 'pl'}, ...]
50
114
  def paradigms
51
115
  if @paradigms.nil?
52
116
  @paradigms = []
@@ -58,6 +122,20 @@ module Jido
58
122
  @paradigms
59
123
  end
60
124
 
125
+ # See Conjugator#paradigms for the expected structure of the parameter.
126
+ # jido.paradigms = [{:person => '1', :quant => 'sg'}, {:person => '3', :quant => 'pl'}] # conjugate for only 1SG and 3PL
127
+ def paradigms= paradigms
128
+ @options[:paradigms] = paradigms
129
+ @paradigms = check_for_list_option :paradigms
130
+ end
131
+
132
+ # See Conjugator#paradigms for the expected structure of the parameter.
133
+ def paradigms_except= paradigms_except
134
+ @options[:paradigms_except] = paradigms_except
135
+ @paradigms_except = check_for_list_option :paradigms_except
136
+ end
137
+
138
+ # Hmm.. what does this do.. ?
61
139
  def conjugate verb
62
140
  @current_el = @data.at_xpath "/verbs/verb[@word='#{verb}']"
63
141
  @current_el = get_fallback_for_verb(verb) if @current_el.nil?
@@ -65,10 +143,11 @@ module Jido
65
143
 
66
144
  ret = {}
67
145
  @current_el_parents = [] # array of parents of the element, sorted by priority - parents earlier in the array will be picked over later ones
68
- store_parents @current_el # populate the parents array = @current_el['inherit'].nil? ? nil : @data.at_xpath("/verbs/verbset[@id='#{@current_el['inherit']}']")
146
+ store_parents @current_el # populate the parents array
69
147
 
70
148
  group = nil; group_search = nil
71
149
  forms.each do |form|
150
+ next if @forms_except.include?(form)
72
151
  ret[form] = {}
73
152
 
74
153
  group_search = "group[@id='#{form}']"
@@ -84,6 +163,8 @@ module Jido
84
163
  pdgmgroup = nil; pdgmgroup_search = nil
85
164
  paradigm = nil; paradigm_search = nil
86
165
  paradigms.each do |paradigm|
166
+ next if @paradigms_except.include?(paradigm)
167
+
87
168
  pdgmgroup_search = "group[@id='#{form}']/pdgmgroup[@id='#{paradigm[:person]}']"
88
169
  pdgmgroup = search_current_el pdgmgroup_search
89
170
 
@@ -140,14 +221,14 @@ module Jido
140
221
  ret
141
222
  end
142
223
 
143
- # Find all parents of a given verb / verbset, and store them in @current_el_parents
224
+ # Find all parents of a given verb / verbset, and store them in <code>@current_el_parents</code>
144
225
  # (if a verb / verbset #1 inherits a verb / verbset #2, then #2 is the parent of #1)
145
226
  #
146
- # Structure note: verbs are "final" objects. Verbs cannot be inherited; they are always at the bottom of a hierarchy.
227
+ # Structure note: verbs are <em>final</em> objects. Verbs cannot be inherited; they are always at the bottom of a hierarchy.
147
228
  # Verbsets can inherit / be inherited by other verbsets, and verbs can inherit verbsets.
148
- # In other words.. verb = final class, verb set = abstract class
229
+ # In other words.. <strong>verb = final class, verb set = abstract class</strong>.
149
230
  #
150
- # Yay recursion :)
231
+ # Yay, recursion :)
151
232
  def store_parents el
152
233
  return if el['inherit'].nil?
153
234
 
@@ -172,6 +253,8 @@ module Jido
172
253
  nil
173
254
  end
174
255
 
256
+ # Find a fallback verbset whose regex matches the given verb.
257
+ # Used when an exact verb element cannot be matched with an input verb (that is, in most cases).
175
258
  def get_fallback_for_verb verb
176
259
  fallbacks.each do |fallback|
177
260
  if verb.match fallback[:regex]
@@ -183,8 +266,9 @@ module Jido
183
266
  false
184
267
  end
185
268
 
269
+ # Return a string describing the instance.
186
270
  def inspect
187
- "<Conjugator @lang => '#{@lang}'>"
271
+ "#<Conjugator @lang=\"#{@lang}\">"
188
272
  end
189
273
  end
190
274
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jido
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Hans Engel
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-16 00:00:00 -07:00
13
+ date: 2011-02-20 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -118,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
118
  requirements:
119
119
  - - ">="
120
120
  - !ruby/object:Gem::Version
121
- hash: -307564322398907677
121
+ hash: 1316283612953836984
122
122
  segments:
123
123
  - 0
124
124
  version: "0"