Module: Language::English::Inflect
- Defined in:
- lib/merb-core/vendor/facets/inflect.rb
Overview
English Nouns Number Inflection.
This module provides english singular <-> plural noun inflections.
Class Attribute Summary collapse
-
.plural_of ⇒ Object
readonly
Returns the value of attribute plural_of.
-
.singular_of ⇒ Object
readonly
Returns the value of attribute singular_of.
Class Method Summary collapse
- .clear(type = :all) ⇒ Object
-
.plural(word) ⇒ Object
(also: pluralize)
Convert an English word from singular to plurel.
-
.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, whole_word = false) ⇒ Object
Define a general rule.
-
.singular(word) ⇒ Object
(also: singularize)
Convert an English word from plurel to singular.
-
.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
Defines a general inflection exception case.
Class Attribute Details
.plural_of ⇒ Object (readonly)
Returns the value of attribute plural_of.
165 166 167 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 165 def plural_of @plural_of end |
.singular_of ⇒ Object (readonly)
Returns the value of attribute singular_of.
165 166 167 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 165 def singular_of @singular_of end |
Class Method Details
.clear(type = :all) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 41 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
Parameters
- word<String>
-
word to pluralize
Returns
- <String>
-
pluralized form of word
Notes
Aliased as pluralize (a Railism)
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 208 def plural(word) # special exceptions return "" if word == "" if result = plural_of[word] return result.dup end result = word.dup regex, hash = pluralization_rules result.sub!(regex) {|m| hash[m]} plural_of[word] = result return result end |
.plural_rule(singular, plural) ⇒ Object
Define a plurualization rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
Examples
Once the following rule is defined: Language::English::Inflector.singular_rule ‘fe’, ‘ves’
You can see the following results: irb> “wife”.plural
> wives
140 141 142 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 140 def plural_rule(singular, plural) @plural_rules << [singular, plural] end |
.plural_word(singular, plural) ⇒ Object
Define a pluralization exception.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
74 75 76 77 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 74 def plural_word(singular, plural) @plural_of[singular] = plural @plural_of[singular.capitalize] = plural.capitalize end |
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
156 157 158 159 160 161 162 163 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 156 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, whole_word = false) ⇒ Object
Define a general rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
- whole_word<Boolean>
-
for capitalization, since words can be capitalized (Man => Men) #
Examples
Once the following rule is defined: Language::English::Inflector.rule ‘y’, ‘ies’
You can see the following results: irb> “fly”.plural
> flies
irb> “cry”.plural
> cries
Define a general rule.
100 101 102 103 104 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 100 def rule(singular, plural, whole_word = false) singular_rule(singular, plural) plural_rule(singular, plural) word(singular, plural) if whole_word end |
.singular(word) ⇒ Object Also known as: singularize
Convert an English word from plurel to singular.
"boys".singular #=> boy
"tomatoes".singular #=> tomato
Parameters
- word<String>
-
word to singularize
Returns
- <String>
-
singularized form of word
Notes
Aliased as singularize (a Railism)
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 180 def singular(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 end |
.singular_rule(singular, plural) ⇒ Object
Define a singularization rule.
Parameters
- singular<String>
-
ending of the word in singular form
- plural<String>
-
ending of the word in plural form
Examples
Once the following rule is defined: Language::English::Inflector.singular_rule ‘o’, ‘oes’
You can see the following results: irb> “heroes”.singular
> hero
121 122 123 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 121 def singular_rule(singular, plural) @singular_rules << [singular, plural] end |
.singular_word(singular, plural) ⇒ Object
Define a singularization exception.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
62 63 64 65 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 62 def singular_word(singular, plural) @singular_of[plural] = singular @singular_of[plural.capitalize] = singular.capitalize end |
.singularization_rules ⇒ Object
Read prepared singularization rules.
145 146 147 148 149 150 151 152 153 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 145 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
Defines a general inflection exception case.
Parameters
- singular<String>
-
singular form of the word
- plural<String>
-
plural form of the word
Examples
Here we define erratum/errata exception case:
Language::English::Inflector.word “erratum”, “errata”
In case singular and plural forms are the same omit second argument on call:
Language::English::Inflector.word ‘information’
35 36 37 38 39 |
# File 'lib/merb-core/vendor/facets/inflect.rb', line 35 def word(singular, plural=nil) plural = singular unless plural singular_word(singular, plural) plural_word(singular, plural) end |