Functions for deriving the infinitive forms of conjugated English words.
Irregular word => infinitive form
Suffix Rules
List of rule suffixes in processing order.
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
Returns the infinative and any acceptable alternatives.
# File lib/english/infinitive.rb, line 712 def self.infinitives(word) information(word)[1..-1] end
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
Returns the affinative and any acceptable alternatives.
# File lib/english/infinitive.rb, line 707 def self.inifinitive(word) information(word)[1] end
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 (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
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
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
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. (-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. (-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