Module: Inflection
- Defined in:
- lib/inflection.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 plural.
-
.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 plural 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.
163 164 165 |
# File 'lib/inflection.rb', line 163 def plural_of @plural_of end |
.singular_of ⇒ Object (readonly)
Returns the value of attribute singular_of.
163 164 165 |
# File 'lib/inflection.rb', line 163 def singular_of @singular_of end |
Class Method Details
.clear(type = :all) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/inflection.rb', line 39 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 plural.
"boy".plural #=> boys
"tomato".plural #=> tomatoes
Parameters
- word<String>
-
word to pluralize
Returns
- <String>
-
pluralized form of word
Notes
Aliased as pluralize (a Railism)
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/inflection.rb', line 206 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: English::Inflect.singular_rule ‘fe’, ‘ves’
You can see the following results: irb> “wife”.plural
> wives
138 139 140 |
# File 'lib/inflection.rb', line 138 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
72 73 74 75 |
# File 'lib/inflection.rb', line 72 def plural_word(singular, plural) @plural_of[singular] = plural @plural_of[singular.capitalize] = plural.capitalize end |
.pluralization_rules ⇒ Object
Read prepared pluralization rules.
154 155 156 157 158 159 160 161 |
# File 'lib/inflection.rb', line 154 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: English::Inflect.rule ‘y’, ‘ies’
You can see the following results: irb> “fly”.plural
> flies
irb> “cry”.plural
> cries
Define a general rule.
98 99 100 101 102 |
# File 'lib/inflection.rb', line 98 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 plural 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)
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/inflection.rb', line 178 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: English::Inflect.singular_rule ‘o’, ‘oes’
You can see the following results: irb> “heroes”.singular
> hero
119 120 121 |
# File 'lib/inflection.rb', line 119 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
60 61 62 63 |
# File 'lib/inflection.rb', line 60 def singular_word(singular, plural) @singular_of[plural] = singular @singular_of[plural.capitalize] = singular.capitalize end |
.singularization_rules ⇒ Object
Read prepared singularization rules.
143 144 145 146 147 148 149 150 151 |
# File 'lib/inflection.rb', line 143 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:
English::Inflect.word “erratum”, “errata”
In case singular and plural forms are the same omit second argument on call:
English::Inflect.word ‘information’
33 34 35 36 37 |
# File 'lib/inflection.rb', line 33 def word(singular, plural=nil) plural = singular unless plural singular_word(singular, plural) plural_word(singular, plural) end |