module English::Infinitive

Functions for deriving the infinitive forms of conjugated English words.

Constants

IRREGULAR

Irregular word => infinitive form

IRREGULAR_RULE
RULES

Suffix Rules

RULE_ORDER

List of rule suffixes in processing order.

Public Class Methods

ditto_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Use the original string. (-4)

# File lib/english/infinitive.rb, line 631
def self.ditto_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:ditto, suffix, suffix1, suffix2, order, number)
end
infinitives(word) click to toggle source

Returns the infinative and any acceptable alternatives.

# File lib/english/infinitive.rb, line 712
def self.infinitives(word)
  information(word)[1..-1]
end
information(word) click to toggle source

Return the rule applies and all viable infinitive forms of the given word.

# File lib/english/infinitive.rb, line 719
def self.information(word)
  word  = word.to_s
  word1 = word2 = suffix = rule = newword = ''

  if IRREGULAR.key?(word)
    word1 = IRREGULAR[word]
    word2 = nil
    rule  = IRREGULAR_RULE
  else
    prefixes = prefixes(word)
    rule = rule(word, prefixes)

    $stderr.puts "prefixes: %p" % prefixes if $DEBUG

    if rule
      suffix = rule.suffix

      $stderr.puts "Using rule %p (%p) for suffix %p" % 
          [rule.number, rule.type, rule.suffix] if $DEBUG

      case rule.type #shortest_prefix
      when :long
        word1 = prefixes[suffix][0]
        word2 = prefixes[suffix][1]
      when :short
        word1 = prefixes[suffix].last + rule.suffix1
        word2 = ''
      when :letter
        word1 = prefixes[suffix].last + rule.suffix1
        word2 = prefixes[suffix].last
      when :special
        word1 = prefixes[suffix].last + rule.suffix1
        word2 = prefixes[suffix].last + rule.suffix2
      when :ditto
        word1 = word
        word2 = ''
      else
        raise IndexError, "Couldn't find rule for shortest prefix %p" %  rule.type #shortest_prefix
      end

      $stderr.puts "For %p: word1: %p, word2: %p" % [rule.type, word1, word2] if $DEBUG

      # Rules 12b and 15: Strip off 'ed' or 'ing'.
      if rule.number == '12b' or rule.number == '15'
        # Do we have a monosyllable of this form:
        # o 0+ Consonants
        # o 1+ Vowel
        # o    2 Non-wx
        # Eg: tipped => tipp?
        # Then return tip and tipp.
        # Eg: swimming => swimm?
        # Then return tipswim and swimm.
        if /^([^aeiou]*[aeiou]+)([^wx])\2$/ =~ word2
          word1 = $1 + $2
          word2 = $1 + $2 + $2
        end
      end
    else
      word1 = word
      word2 = nil
      rule  = IRREGULAR_RULE
    end
  end

  return rule, word1, word2
end
inifinitive(word) click to toggle source

Returns the affinative and any acceptable alternatives.

# File lib/english/infinitive.rb, line 707
def self.inifinitive(word)
  information(word)[1]
end
letter_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Letter rule. (-2)

# File lib/english/infinitive.rb, line 621
def self.letter_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:letter, suffix, suffix1, suffix2, order, number)
end
long_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Long rule (longest prefix). (0)

# File lib/english/infinitive.rb, line 606
def self.long_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:long, suffix, suffix1, suffix2, order, number)
end
prefixes(word) click to toggle source

Calculate the prefixes for a given word.

# File lib/english/infinitive.rb, line 817
def self.prefixes(word)
  # Build up prefix[suffix] as an array of prefixes, from longest to shortest.
  prefix, suffix = nil, nil
  prefixes = Hash.new{ |h,k| h[k] = [] }
  # Build the hash of prefixes for the word
  1.upto(word.length) do |i|
    prefix = word[0, i]
    suffix = word[i..-1]
    (suffix.length - 1).downto(0) do |j|
      newword = prefix + suffix[0,j]
      prefixes[suffix].push(newword)
    end
  end
  return prefixes
end
rule(word, prefixes=nil) click to toggle source

Lookup the rule that applies to a given word.

# File lib/english/infinitive.rb, line 787
def self.rule(word, prefixes=nil)
  prefixes = prefixes(word) unless prefixes

  if IRREGULAR.key?(word)
    IRREGULAR_RULE
  else
    # Build up prefix[suffix] as an array of prefixes, from longest to shortest.
    prefix, suffix = nil, nil
    prefixes = Hash.new{ |h,k| h[k] = [] }
    # Build the hash of prefixes for the word
    1.upto(word.length) do |i|
      prefix = word[0, i]
      suffix = word[i..-1]
      (suffix.length - 1).downto(0) do |j|
        newword = prefix + suffix[0,j]
        prefixes[suffix].push(newword)
      end
    end
    # Now check for rules covering the prefixes for this word, picking
    # the first one if one was found.
    suffix = (RULE_ORDER & prefixes.keys).first
    if suffix
      RULES[suffix]
    else
      IRREGULAR_RULE
    end
  end
end
second_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Longer Rule (2nd longest prefix). (1)

# File lib/english/infinitive.rb, line 611
def self.second_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:second, suffix, suffix1, suffix2, order, number)
end
short_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Short Rule. (-1)

# File lib/english/infinitive.rb, line 616
def self.short_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:short, suffix, suffix1, suffix2, order, number)
end
special_rule(suffix, suffix1, suffix2, order, number) click to toggle source

Special rule. (-3)

# File lib/english/infinitive.rb, line 626
def self.special_rule(suffix, suffix1, suffix2, order, number)
  RULES[suffix] = Rule.new(:special, suffix, suffix1, suffix2, order, number)
end