Module: English::Inflect
- Defined in:
- lib/gems/english-0.3.1/lib/english/inflect.rb
Overview
English Nouns Number Inflection.
This module provides english singular <-> plural noun inflections.
Class Method Summary collapse
-
.clear(type = :all) ⇒ Object
Clear all rules.
-
.plural(word) ⇒ Object
(also: pluralize)
Convert an English word from singular to plurel.
- .plural_of ⇒ Object
-
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
-
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
-
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
-
.rule(singular, plural) ⇒ Object
Define a general rule.
-
.singular(word) ⇒ Object
(also: singularize)
Convert an English word from plurel to singular.
- .singular_of ⇒ Object
-
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
-
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
-
.singularization_rules ⇒ Object
Read prepared singularization rules.
-
.word(singular, plural = nil) ⇒ Object
Define a general two-way exception.
Class Method Details
.clear(type = :all) ⇒ Object
Clear all rules.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 172 def clear(type = :all) if type == :singular || type == :all @singular_of = {} @singular_rules = [] @singularization_rules, @singularization_regex = nil, nil end if type == :plural || type == :all @singular_of = {} @singular_rules = [] @singularization_rules, @singularization_regex = nil, nil end end |
.plural(word) ⇒ Object Also known as: pluralize
Convert an English word from singular to plurel.
"boy".plural #=> boys
"tomato".plural #=> tomatoes
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 150 def plural(word) return "" if word == "" if result = plural_of[word] return result.dup end #return self.dup if /s$/ =~ self # ??? result = word.dup regex, hash = pluralization_rules result.sub!(regex) {|m| hash[m]} plural_of[word] = result return result #pluralization_rules.each do |(match, replacement)| # break if result.gsub!(match, replacement) #end #return result end |
.plural_of ⇒ Object
117 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 117 def plural_of ; @plural_of ; end |
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
70 71 72 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 70 def plural_rule(singular, plural) @plural_rules << [singular, plural] end |
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
53 54 55 56 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 53 def plural_word(singular, plural) @plural_of[singular] = plural @plural_of[singular.capitalize] = plural.capitalize end |
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
95 96 97 98 99 100 101 102 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 95 def pluralization_rules if defined?(@pluralization_regex) && @pluralization_regex return [@pluralization_regex, @pluralization_hash] end @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i") @pluralization_hash = Hash[*@plural_rules.flatten] [@pluralization_regex, @pluralization_hash] end |
.rule(singular, plural) ⇒ Object
Define a general rule.
59 60 61 62 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 59 def rule(singular, plural) singular_rule(singular, plural) plural_rule(singular, plural) end |
.singular(word) ⇒ Object Also known as: singularize
Convert an English word from plurel to singular.
"boys".singular #=> boy
"tomatoes".singular #=> tomato
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 124 def singular(word) return "" if word == "" if result = singular_of[word] return result.dup end result = word.dup regex, hash = singularization_rules result.sub!(regex) {|m| hash[m]} singular_of[word] = result return result #singularization_rules.each do |(match, replacement)| # break if result.gsub!(match, replacement) #end #return result end |
.singular_of ⇒ Object
114 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 114 def singular_of ; @singular_of ; end |
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
65 66 67 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 65 def singular_rule(singular, plural) @singular_rules << [singular, plural] end |
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
47 48 49 50 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 47 def singular_word(singular, plural) @singular_of[plural] = singular @singular_of[plural.capitalize] = singular.capitalize end |
.singularization_rules ⇒ Object
Read prepared singularization rules.
75 76 77 78 79 80 81 82 83 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 75 def singularization_rules if defined?(@singularization_regex) && @singularization_regex return [@singularization_regex, @singularization_hash] end # No sorting needed: Regexen match on longest string @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i") @singularization_hash = Hash[*@singular_rules.flatten].invert [@singularization_regex, @singularization_hash] end |
.word(singular, plural = nil) ⇒ Object
Define a general two-way exception.
This also defines a general rule, so foo_child will correctly become foo_children.
Whole words also work if they are capitalized (Goose => Geese).
39 40 41 42 43 44 |
# File 'lib/gems/english-0.3.1/lib/english/inflect.rb', line 39 def word(singular, plural=nil) plural = singular unless plural singular_word(singular, plural) plural_word(singular, plural) rule(singular, plural) end |