Guest User

Untitled

a guest
Jul 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. module Pluralize
  2. module_function
  3.  
  4. def pluralize(string)
  5. string = string.dup
  6.  
  7. @plural.each do |pattern, options|
  8. string.gsub!(pattern, options[:replace])
  9. end
  10.  
  11. string
  12. end
  13.  
  14. def singularize(string)
  15. string = string.dup
  16.  
  17. @singular.each do |pattern, options|
  18. string.gsub!(pattern, options[:replace])
  19. end
  20.  
  21. string
  22. end
  23.  
  24. @singular, @plural = {}, {}
  25.  
  26. def singular(pattern, options)
  27. @singular[pattern] = options
  28. end
  29.  
  30. def plural(pattern, options)
  31. @plural[pattern] = options
  32. end
  33.  
  34. ## Regular Plurals
  35. #
  36. # The plural morpheme in English is suffixed to the end of most nouns.
  37. # Regular English plurals fall into three classes, depending upon the sound
  38. # that ends the singular form:
  39. #
  40. # Where a singular noun ends in a sibilant sound—/s/, /z/, /ʃ/, /ʒ/, /tʃ/,or
  41. # /dʒ/—the plural is formed by adding /ɨz/. The spelling adds -es, or -s if
  42. # the singular already ends in -e:
  43. #
  44. # kiss kisses /ˈkɪsɨz/
  45. # phase phases /ˈfeɪzɨz/
  46. # dish dishes /ˈdɪʃɨz/
  47. # massage massages /məˈsɑːʒɨz/ or /ˈmæsɑːʒɨz/
  48. # witch witches /ˈwɪtʃɨz/
  49. # judge judges /ˈdʒʌdʒɨz/
  50.  
  51. plural /ss$/, replace: 'sses'
  52. plural /se$/, replace: 'ses'
  53. plural /sh$/, replace: 'shes'
  54. plural /ge$/, replace: 'ges'
  55. plural /ch$/, replace: 'ches'
  56. plural /ge$/, replace: 'ges'
  57.  
  58. # 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:
  59. # lap laps /læps/
  60. # cat cats /kæts/
  61. # clock clocks /klɒks/
  62. # cuff cuffs /kʌfs/
  63. # death deaths /dɛθs/
  64. plural /ap$/, replace: 'aps'
  65. plural /at$/, replace: 'ats'
  66. plural /ck$/, replace: 'cks'
  67. plural /ff$/, replace: 'ffs'
  68. plural /th$/, replace: 'ths'
  69.  
  70. # For all other words (i.e. words ending in vowels or voiced non-sibilants) the regular plural adds /z/, represented orthographically by -s:
  71. # boy boys /bɔɪz/
  72. # girl girls /ɡɜrlz/
  73. # chair chairs /tʃɛərz/
  74. plural /y$/, replace: 'ys'
  75. plural /l$/, replace: 'ls'
  76. plural /r$/, replace: 'rs'
  77. end
  78.  
  79. require 'bacon'
  80. Bacon.summary_on_exit
  81.  
  82. describe Pluralize do
  83. def pluralize(string)
  84. Pluralize.pluralize(string)
  85. end
  86.  
  87. it 'pluralizes words ending with sibilant sound' do
  88. pluralize("kiss").should == "kisses"
  89. pluralize("phase").should == "phases"
  90. pluralize("dish").should == "dishes"
  91. pluralize("message").should == "messages"
  92. pluralize("witch").should == "witches"
  93. pluralize("judge").should == "judges"
  94. end
  95.  
  96. it 'pluralizes words ending with consonant sound' do
  97. pluralize('lap').should == 'laps'
  98. pluralize('cat').should == 'cats'
  99. pluralize('clock').should == 'clocks'
  100. pluralize('cuff').should == 'cuffs'
  101. pluralize('death').should == 'deaths'
  102. end
  103.  
  104. it 'pluralizes words ending in vowels or voiced non-sibilants' do
  105. pluralize('boy').should == 'boys'
  106. pluralize('girl').should == 'girls'
  107. pluralize('chair').should == 'chairs'
  108. end
  109. end
Add Comment
Please, Sign In to add comment