Created
January 25, 2011 19:12
-
-
Save manveru/795444 to your computer and use it in GitHub Desktop.
simple pluralization framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Pluralize | |
module_function | |
def pluralize(string) | |
string = string.dup | |
@plural.each do |pattern, options| | |
string.gsub!(pattern, options[:replace]) | |
end | |
string | |
end | |
def singularize(string) | |
string = string.dup | |
@singular.each do |pattern, options| | |
string.gsub!(pattern, options[:replace]) | |
end | |
string | |
end | |
@singular, @plural = {}, {} | |
def singular(pattern, options) | |
@singular[pattern] = options | |
end | |
def plural(pattern, options) | |
@plural[pattern] = options | |
end | |
## Regular Plurals | |
# | |
# The plural morpheme in English is suffixed to the end of most nouns. | |
# Regular English plurals fall into three classes, depending upon the sound | |
# that ends the singular form: | |
# | |
# Where a singular noun ends in a sibilant sound—/s/, /z/, /ʃ/, /ʒ/, /tʃ/,or | |
# /dʒ/—the plural is formed by adding /ɨz/. The spelling adds -es, or -s if | |
# the singular already ends in -e: | |
# | |
# kiss kisses /ˈkɪsɨz/ | |
# phase phases /ˈfeɪzɨz/ | |
# dish dishes /ˈdɪʃɨz/ | |
# massage massages /məˈsɑːʒɨz/ or /ˈmæsɑːʒɨz/ | |
# witch witches /ˈwɪtʃɨz/ | |
# judge judges /ˈdʒʌdʒɨz/ | |
plural /ss$/, replace: 'sses' | |
plural /se$/, replace: 'ses' | |
plural /sh$/, replace: 'shes' | |
plural /ge$/, replace: 'ges' | |
plural /ch$/, replace: 'ches' | |
plural /ge$/, replace: 'ges' | |
# When the singular form ends in a voiceless consonant (other than a sibilant) — /p/, /t/, /k/, /f/ or /θ/, — the plural is formed by adding /s/. The spelling adds -s. Examples: | |
# lap laps /læps/ | |
# cat cats /kæts/ | |
# clock clocks /klɒks/ | |
# cuff cuffs /kʌfs/ | |
# death deaths /dɛθs/ | |
plural /ap$/, replace: 'aps' | |
plural /at$/, replace: 'ats' | |
plural /ck$/, replace: 'cks' | |
plural /ff$/, replace: 'ffs' | |
plural /th$/, replace: 'ths' | |
# For all other words (i.e. words ending in vowels or voiced non-sibilants) the regular plural adds /z/, represented orthographically by -s: | |
# boy boys /bɔɪz/ | |
# girl girls /ɡɜrlz/ | |
# chair chairs /tʃɛərz/ | |
plural /y$/, replace: 'ys' | |
plural /l$/, replace: 'ls' | |
plural /r$/, replace: 'rs' | |
end | |
require 'bacon' | |
Bacon.summary_on_exit | |
describe Pluralize do | |
def pluralize(string) | |
Pluralize.pluralize(string) | |
end | |
it 'pluralizes words ending with sibilant sound' do | |
pluralize("kiss").should == "kisses" | |
pluralize("phase").should == "phases" | |
pluralize("dish").should == "dishes" | |
pluralize("message").should == "messages" | |
pluralize("witch").should == "witches" | |
pluralize("judge").should == "judges" | |
end | |
it 'pluralizes words ending with consonant sound' do | |
pluralize('lap').should == 'laps' | |
pluralize('cat').should == 'cats' | |
pluralize('clock').should == 'clocks' | |
pluralize('cuff').should == 'cuffs' | |
pluralize('death').should == 'deaths' | |
end | |
it 'pluralizes words ending in vowels or voiced non-sibilants' do | |
pluralize('boy').should == 'boys' | |
pluralize('girl').should == 'girls' | |
pluralize('chair').should == 'chairs' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment