PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/utilities/inflect.rb

http://waves.googlecode.com/
Ruby | 194 lines | 138 code | 23 blank | 33 comment | 6 complexity | 0d9466ca087896a7a629ef98577dcb49 MD5 | raw file
  1. # Much love to Facets (more specifically English) for this module
  2. # http://english.rubyforge.org/
  3. # changed slightly in the hopes of one day implementing a different set
  4. # of rules for different languages
  5. # NOTE: this is NOT implemented yet.
  6. # plural and singular work directly with the English class
  7. module Inflect
  8. module InflectorMethods
  9. # Define a general exception.
  10. def word(singular, plural=nil)
  11. plural = singular unless plural
  12. singular_word(singular, plural)
  13. plural_word(singular, plural)
  14. end
  15. # Define a singularization exception.
  16. def singular_word(singular, plural)
  17. @singular_of[plural] = singular
  18. end
  19. # Define a pluralization exception.
  20. def plural_word(singular, plural)
  21. @plural_of[singular] = plural
  22. end
  23. # Define a general rule.
  24. def rule(singular, plural)
  25. singular_rule(singular, plural)
  26. plural_rule(singular, plural)
  27. end
  28. # Define a singularization rule.
  29. def singular_rule(singular, plural)
  30. @singular_rules << [singular, plural]
  31. end
  32. # Define a plurualization rule.
  33. def plural_rule(singular, plural)
  34. @plural_rules << [singular, plural]
  35. end
  36. # Read prepared singularization rules.
  37. def singularization_rules
  38. return @singularization_rules if @singularization_rules
  39. sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
  40. @singularization_rules = sorted.collect do |s, p|
  41. [ /#{p}$/, "#{s}" ]
  42. end
  43. end
  44. # Read prepared pluralization rules.
  45. def pluralization_rules
  46. return @pluralization_rules if @pluralization_rules
  47. sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
  48. @pluralization_rules = sorted.collect do |s, p|
  49. [ /#{s}$/, "#{p}" ]
  50. end
  51. end
  52. #
  53. def plural_of
  54. @plural_of
  55. end
  56. #
  57. def singular_of
  58. @singular_of
  59. end
  60. # Convert an English word from plurel to singular.
  61. #
  62. # "boys".singular #=> boy
  63. # "tomatoes".singular #=> tomato
  64. #
  65. def singular(word)
  66. if result = singular_of[word]
  67. return result.dup
  68. end
  69. result = word.dup
  70. singularization_rules.each do |(match, replacement)|
  71. break if result.gsub!(match, replacement)
  72. end
  73. return result
  74. end
  75. # Convert an English word from singular to plurel.
  76. #
  77. # "boy".plural #=> boys
  78. # "tomato".plural #=> tomatoes
  79. #
  80. def plural(word)
  81. if result = plural_of[word]
  82. return result.dup
  83. end
  84. #return self.dup if /s$/ =~ self # ???
  85. result = word.dup
  86. pluralization_rules.each do |(match, replacement)|
  87. break if result.gsub!(match, replacement)
  88. end
  89. return result
  90. end
  91. end
  92. class English
  93. @singular_of = {}
  94. @plural_of = {}
  95. @singular_rules = []
  96. @plural_rules = []
  97. class << self
  98. include InflectorMethods
  99. end
  100. # One argument means singular and plural are the same.
  101. word 'equipment'
  102. word 'information'
  103. word 'money'
  104. word 'species'
  105. word 'series'
  106. word 'fish'
  107. word 'sheep'
  108. word 'moose'
  109. word 'hovercraft'
  110. # Two arguments defines a singular and plural exception.
  111. word 'Swiss' , 'Swiss'
  112. word 'life' , 'lives'
  113. word 'wife' , 'wives'
  114. word 'virus' , 'viri'
  115. word 'octopus' , 'octopi'
  116. word 'cactus' , 'cacti'
  117. word 'goose' , 'geese'
  118. word 'criterion' , 'criteria'
  119. word 'alias' , 'aliases'
  120. word 'status' , 'statuses'
  121. word 'axis' , 'axes'
  122. word 'crisis' , 'crises'
  123. word 'testis' , 'testes'
  124. word 'child' , 'children'
  125. word 'person' , 'people'
  126. word 'potato' , 'potatoes'
  127. word 'tomato' , 'tomatoes'
  128. word 'buffalo' , 'buffaloes'
  129. word 'torpedo' , 'torpedoes'
  130. word 'quiz' , 'quizes'
  131. word 'matrix' , 'matrices'
  132. word 'vertex' , 'vetices'
  133. word 'index' , 'indices'
  134. word 'ox' , 'oxen'
  135. word 'mouse' , 'mice'
  136. word 'louse' , 'lice'
  137. word 'thesis' , 'theses'
  138. word 'thief' , 'thieves'
  139. word 'analysis' , 'analyses'
  140. # One-way singularization exception (convert plural to singular).
  141. singular_word 'cactus', 'cacti'
  142. # General rules.
  143. rule 'hive' , 'hives'
  144. rule 'rf' , 'rves'
  145. rule 'af' , 'aves'
  146. rule 'ero' , 'eroes'
  147. rule 'man' , 'men'
  148. rule 'ch' , 'ches'
  149. rule 'sh' , 'shes'
  150. rule 'ss' , 'sses'
  151. rule 'ta' , 'tum'
  152. rule 'ia' , 'ium'
  153. rule 'ra' , 'rum'
  154. rule 'ay' , 'ays'
  155. rule 'ey' , 'eys'
  156. rule 'oy' , 'oys'
  157. rule 'uy' , 'uys'
  158. rule 'y' , 'ies'
  159. rule 'x' , 'xes'
  160. rule 'lf' , 'lves'
  161. rule 'us' , 'uses'
  162. rule '' , 's'
  163. # One-way singular rules.
  164. singular_rule 'of' , 'ofs' # proof
  165. singular_rule 'o' , 'oes' # hero, heroes
  166. singular_rule 'f' , 'ves'
  167. # One-way plural rules.
  168. plural_rule 'fe' , 'ves' # safe, wife
  169. plural_rule 's' , 'ses'
  170. end
  171. end