class Zenlish::Inflect::InflectionTable

Attributes

headings[R]
name[R]
rules[R]

Public Class Methods

new(aName) click to toggle source
# File lib/zenlish/inflect/inflection_table.rb, line 10
def initialize(aName)
  @name = aName
  @headings = []
  @rules = []
end

Public Instance Methods

add_heading(aHeading) click to toggle source
# File lib/zenlish/inflect/inflection_table.rb, line 16
def add_heading(aHeading)
  @headings << aHeading
end
add_rule(aRule) click to toggle source
# File lib/zenlish/inflect/inflection_table.rb, line 20
def add_rule(aRule)
  @rules << aRule
end
all_inflections(aLexeme) click to toggle source
# 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) click to toggle source
# 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