Class: Zenlish::Inflect::InflectionTable
- Inherits:
-
Object
- Object
- Zenlish::Inflect::InflectionTable
- Defined in:
- lib/zenlish/inflect/inflection_table.rb
Instance Attribute Summary collapse
-
#headings ⇒ Object
readonly
Returns the value of attribute headings.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Instance Method Summary collapse
- #add_heading(aHeading) ⇒ Object
- #add_rule(aRule) ⇒ Object
- #all_inflections(aLexeme) ⇒ Object
- #inflect(aLexeme, theConstraints) ⇒ Object
-
#initialize(aName) ⇒ InflectionTable
constructor
A new instance of InflectionTable.
Constructor Details
#initialize(aName) ⇒ InflectionTable
Returns a new instance of InflectionTable.
10 11 12 13 14 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 10 def initialize(aName) @name = aName @headings = [] @rules = [] end |
Instance Attribute Details
#headings ⇒ Object (readonly)
Returns the value of attribute headings.
7 8 9 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 7 def headings @headings end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 6 def name @name end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
8 9 10 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 8 def rules @rules end |
Instance Method Details
#add_heading(aHeading) ⇒ Object
16 17 18 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 16 def add_heading(aHeading) @headings << aHeading end |
#add_rule(aRule) ⇒ Object
20 21 22 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 20 def add_rule(aRule) @rules << aRule end |
#all_inflections(aLexeme) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 52 def all_inflections(aLexeme) constraints = Array.new(headings.size) { |_i| nil } heading_matches = [] headings.each_with_index do |hd, _idx| heading_matches << hd.all_matches(aLexeme) end w_forms = nil if constraints.size == 1 all_combos = heading_matches w_forms = all_combos.first.map do |item| inflect(aLexeme, [item]) end else vector1 = heading_matches.shift all_combos = vector1.product(*heading_matches) w_forms = all_combos.map do |combination| inflect(aLexeme, combination) end end w_forms.uniq end |
#inflect(aLexeme, theConstraints) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/zenlish/inflect/inflection_table.rb', line 24 def inflect(aLexeme, theConstraints) constraints = if theConstraints.nil? || theConstraints.empty? Array.new(headings.size) { |_i| nil } else theConstraints end err_msg = "Table has #{headings.size} headings, instead of #{constraints.size}" raise StandardError, err_msg if constraints.size != headings.size actuals = [] headings.each_with_index do |hd, idx| if constraints[idx] actuals << constraints[idx] else actuals << hd.evaluate_for(aLexeme) end end # Hit policy: first matching_rule = rules.find do |rule| rule.success?(headings, aLexeme, actuals) end unless matching_rule err_msg = "No rule in table covers case: lexeme #{aLexeme}, actuals: #{p actuals}." raise StandardError, err_msg end matching_rule.apply(headings, aLexeme, actuals) end |