Module: Treat::Workers::Inflectors::Declensors::English::Inflect
- Defined in:
- lib/treat/workers/inflectors/declensors/english/inflect.rb
Overview
This class comes from the Inflect module; it has been copied from the unmaintained ‘english’ ruby gem, created by Thomas Sawyer.
Released under the MIT License.
http://english.rubyforge.org
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.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 174 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 152 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
119 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 119 def plural_of ; @plural_of ; end |
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
72 73 74 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 72 def plural_rule(singular, plural) @plural_rules << [singular, plural] end |
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
55 56 57 58 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 55 def plural_word(singular, plural) @plural_of[singular] = plural @plural_of[singular.capitalize] = plural.capitalize end |
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
97 98 99 100 101 102 103 104 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 97 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.
61 62 63 64 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 61 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 126 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
116 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 116 def singular_of ; @singular_of ; end |
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
67 68 69 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 67 def singular_rule(singular, plural) @singular_rules << [singular, plural] end |
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
49 50 51 52 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 49 def singular_word(singular, plural) @singular_of[plural] = singular @singular_of[plural.capitalize] = singular.capitalize end |
.singularization_rules ⇒ Object
Read prepared singularization rules.
77 78 79 80 81 82 83 84 85 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 77 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).
41 42 43 44 45 46 |
# File 'lib/treat/workers/inflectors/declensors/english/inflect.rb', line 41 def word(singular, plural=nil) plural = singular unless plural singular_word(singular, plural) plural_word(singular, plural) rule(singular, plural) end |