Module: Pluralizr
- Included in:
- String
- Defined in:
- lib/pluralizr.rb,
lib/pluralizr/lists.rb,
lib/pluralizr/rules.rb,
lib/pluralizr/version.rb,
lib/pluralizr/exceptions.rb
Overview
A module that takes a single word and returns the pluralized version of that word. It attempts to take into account various rules—like, handling words that end in ‘y’ where the ‘y’ is preceded by a vowel or consonant (i.e., day
-> days
versus baby
-> babies
)—as well as words of French, Greek and Latin origins.
Defined Under Namespace
Classes: InvalidStringError, InvalidTypeError, TooManyWordsError
Constant Summary collapse
- EXCEPTIONS_LIST =
List of words that are exceptions to the rules in the RULES constant variable hash.
{ # SXZCHSH_EXCEPTIONS 'quiz' => 'quizzes', 'stomach' => 'stomachs', 'epoch' => 'epochs', # FE_F_EXCEPTIONS 'chef' => 'chefs', 'dwarf' => 'dwarfs', 'gulf' => 'gulfs', 'hoof' => 'hooves', 'safe' => 'safes', 'surf' => 'surfs', 'turf' => 'turfs', # EX_EXCEPTIONS 'annex' => 'annexes', 'complex' => 'complexes', 'duplex' => 'duplexes', 'hex' => 'hexes', 'index' => 'indexes', 'sex' => 'sexes', # O_EXCEPTIONS 'albino' => 'albinos', 'armadillo' => 'armadillos', 'auto' => 'autos', 'cello' => 'cellos', 'combo' => 'combos', 'ego' => 'egos', 'halo' => 'halos', 'inferno' => 'infernos', 'lasso' => 'lassos', 'memento' => 'mementos', 'memo' => 'memos', 'piano' => 'pianos', 'photo' => 'photos', 'pro' => 'pros', 'silo' => 'silos', 'solo' => 'solos', 'taco' => 'tacos', 'tuxedo' => 'tuxedos', 'typo' => 'typos', # UM_EXCEPTIONS 'album' => 'albums', 'stadium' => 'stadiums', 'minimum' => 'minimums', 'maximum' => 'maximums', 'premium' => 'premiums', 'vacuum' => 'vacuums', # STRICT_LATIN_ORIGINS 'radius' => 'radii', 'alumnus' => 'alumni', 'cactus' => 'cacti', 'fungus' => 'fungi', 'nucleus' => 'nuclei', 'genus' => 'genera', 'syllabus' => 'syllabi', 'alga' => 'algae', 'vertebra' => 'vertebrae', 'larva' => 'larvae', # STRICT_GREEK_ORIGINS 'automaton' => 'automata', 'criterion' => 'criteria', 'phenomenon' => 'phenomena', # ITALIAN_ORIGINS 'espresso' => 'espressos', 'pizza' => 'pizzas', 'risotto' => 'risottos', 'paparazzo' => 'paparazzi', 'spaghetto' => 'spaghetti' }
- IRREGULAR_WORDS =
List of words whose plural form doesn’t adhere to a rule in the RULES hash, but instead, often mutates the original word. For example,
mouse
=>mice
. Or,foot
=>feet
. { 'child' => 'children', 'die' => 'dice', 'foot' => 'feet', 'goose' => 'geese', 'louse' => 'lice', 'man' => 'men', 'mouse' => 'mice', 'ox' => 'oxen', 'person' => 'people', 'that' => 'those', 'this' => 'these', 'tooth' => 'teeth', 'woman' => 'women' }
- SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME =
List of words whose plural form is the same as its singular form. For example,
deer
is used in both singular and plural contexts. %w(advice aircraft bison corn deer equipment evidence gold information jewelry kin legislation luck luggage moose music offspring sheep silver swine trousers trout wheat)
- PLURAL_ONLY_WORDS =
List of words that only have a plural form. There is no singular form of the word. For example, the word
tweezers
has no singular form. The word tweezer is actually a verb and not a noun. %w(barracks bellows cattle deer dregs eyeglasses gallows headquarters mathematics means measles mumps news oats pants pliers pajamas scissors series shears shorts species tongs tweezers vespers)
- RULES =
Hash containing the various rules for specific word endings in the English language. – These endings are stored in the hash as keys. Each key has a set of properties as its value. These properties specify the word’s plural form ending, how the plural form should be applied to the original word, and if there are any words considered exceptions to the particular rule.
For example, given a word that ends in ‘a’, the pluralize() method will check for errors (see exceptions.rb) and special cases (see lists.rb), and then iterate through the RULES hash below. Since the word ends in ‘a’, it matches with the RULES key, and since this list contains an exceptions list, it checks if the word is on the list. If so, it returns the plural version of that word from the exceptions list. If not, it adds an ‘s’ to the end of the original word and returns it.
{ 'ia' => { with_ending: 's', offset: -1 }, 'a' => { with_ending: 's', offset: -1, has_exception_list: true }, 'ffe' => { with_ending: 's', offset: -1 }, 'fe' => { with_ending: 'ves', offset: -3, has_exception_list: true }, 'ff' => { with_ending: 's', offset: -1 }, 'f' => { with_ending: 'ves', offset: -2, has_exception_list: true }, 'ch' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'sh' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'um' => { with_ending: 'a', offset: -3, has_exception_list: true }, 'tion' => { with_ending: 's', offset: -1 }, 'on' => { with_ending: 's', offset: -1, has_exception_list: true }, 'o' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'is' => { with_ending: 'es', offset: -3 }, 'us' => { with_ending: 'es', offset: -1, has_exception_list: true }, 's' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'eau' => { with_ending: 's', offset: -1 }, 'ex' => { with_ending: 'ices', offset: -3, has_exception_list: true }, 'x' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'y' => { with_ending: 'ies', offset: -2 }, 'z' => { with_ending: 'es', offset: -1, has_exception_list: true }, 'irregular' => { offset: -1, has_exception_list: true }, 'default' => { with_ending: 's', offset: -1 } }
- VERSION =
Current gem version.
"0.2.0"
Class Method Summary collapse
-
.pluralize(word) ⇒ Object
Pluralizes the object passed in or returns an error if an exception is raised.
Instance Method Summary collapse
-
#ends_with_double_vowel_and_f? ⇒ Boolean
Returns
true
if the word (the caller) ends in a double vowel plus ‘f’. -
#ends_with_vowel_and_y_or_o? ⇒ Boolean
Returns
true
if the word (the caller) ends in a vowel plus either a ‘y’ or an ‘o’. -
#has_same_singular_and_plural_form? ⇒ Boolean
Returns
true
if the word (the caller) is found in the SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME hash. -
#is_irregular? ⇒ Boolean
Returns
true
if the word (the caller) is found in the IRREGULAR_WORDS hash. -
#only_has_plural_form? ⇒ Boolean
Returns
true
if the word (the caller) is found in the PLURAL_ONLY_WORDS hash.
Class Method Details
.pluralize(word) ⇒ Object
Pluralizes the object passed in or returns an error if an exception is raised.
Parameters:
- word
-
The object passed in.
Returns:
Either an error message, if any exceptions are raised via Pluralizr.check_for_errors method, or the appropriate plural version of the word passed into the method.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pluralizr.rb', line 25 def self.pluralize(word) check_for_errors(word) case when word.is_irregular? then return IRREGULAR_WORDS[word] when word.has_same_singular_and_plural_form? || word.only_has_plural_form? return word when word.ends_with_vowel_and_y_or_o? || word.ends_with_double_vowel_and_f? return word.insert(-1, 's') else RULES.each do |rule, props| return find_plural_of(word, props) if word.end_with?(rule) end end return find_plural_of(word, RULES['default']) end |
Instance Method Details
#ends_with_double_vowel_and_f? ⇒ Boolean
Returns true
if the word (the caller) ends in a double vowel plus ‘f’. For example, proof
or handkerchief
.
54 55 56 |
# File 'lib/pluralizr.rb', line 54 def ends_with_double_vowel_and_f? self.match(/[aeiouy]+[f]$/i) end |
#ends_with_vowel_and_y_or_o? ⇒ Boolean
Returns true
if the word (the caller) ends in a vowel plus either a ‘y’ or an ‘o’. For example, day
and valley
or stereo
and tattoo
.
47 48 49 |
# File 'lib/pluralizr.rb', line 47 def ends_with_vowel_and_y_or_o? self.match(/[aeiouy][oy]$/i) end |
#has_same_singular_and_plural_form? ⇒ Boolean
Returns true
if the word (the caller) is found in the SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME hash.
75 76 77 |
# File 'lib/pluralizr.rb', line 75 def has_same_singular_and_plural_form? SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME.include?(self) end |
#is_irregular? ⇒ Boolean
Returns true
if the word (the caller) is found in the IRREGULAR_WORDS hash.
61 62 63 |
# File 'lib/pluralizr.rb', line 61 def is_irregular? IRREGULAR_WORDS.include?(self) end |
#only_has_plural_form? ⇒ Boolean
Returns true
if the word (the caller) is found in the PLURAL_ONLY_WORDS hash.
68 69 70 |
# File 'lib/pluralizr.rb', line 68 def only_has_plural_form? PLURAL_ONLY_WORDS.include?(self) end |