Module: English::Inflect

Defined in:
lib/strokedb/util/inflect.rb

Overview

English Nouns Number Inflection.

This module provides english singular <-> plural noun inflections.

Class Method Summary collapse

Class Method Details

.plural(word) ⇒ Object Also known as: pluralize

Convert an English word from singular to plurel.

"boy".plural     #=> boys
"tomato".plural  #=> tomatoes


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/strokedb/util/inflect.rb', line 106

def plural(word)
  if result = plural_of[word]
    return result.dup
  end
  #return self.dup if /s$/ =~ self # ???
  result = word.dup
  pluralization_rules.each do |(match, replacement)|
    break if result.gsub!(match, replacement)
  end
  return result
end

.plural_ofObject



72
73
74
# File 'lib/strokedb/util/inflect.rb', line 72

def plural_of
  @plural_of
end

.plural_rule(singular, plural) ⇒ Object

Define a plurualization rule.



49
50
51
# File 'lib/strokedb/util/inflect.rb', line 49

def plural_rule(singular, plural)
  @plural_rules << [singular, plural]
end

.plural_word(singular, plural) ⇒ Object

Define a pluralization exception.



33
34
35
# File 'lib/strokedb/util/inflect.rb', line 33

def plural_word(singular, plural)
  @plural_of[singular] = plural
end

.pluralization_rulesObject

Read prepared pluralization rules.



63
64
65
66
67
68
69
# File 'lib/strokedb/util/inflect.rb', line 63

def pluralization_rules
  return @pluralization_rules if @pluralization_rules
  sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
  @pluralization_rules = sorted.collect do |s, p|
    [ /#{s}$/, "#{p}" ]
  end
end

.rule(singular, plural) ⇒ Object

Define a general rule.



38
39
40
41
# File 'lib/strokedb/util/inflect.rb', line 38

def rule(singular, plural)
  singular_rule(singular, plural)
  plural_rule(singular, plural)
end

.singular(word) ⇒ Object Also known as: singularize

Convert an English word from plurel to singular.

"boys".singular      #=> boy
"tomatoes".singular  #=> tomato


86
87
88
89
90
91
92
93
94
95
# File 'lib/strokedb/util/inflect.rb', line 86

def singular(word)
  if result = singular_of[word]
    return result.dup
  end
  result = word.dup
  singularization_rules.each do |(match, replacement)|
    break if result.gsub!(match, replacement)
  end
  return result
end

.singular_ofObject



77
78
79
# File 'lib/strokedb/util/inflect.rb', line 77

def singular_of
  @singular_of
end

.singular_rule(singular, plural) ⇒ Object

Define a singularization rule.



44
45
46
# File 'lib/strokedb/util/inflect.rb', line 44

def singular_rule(singular, plural)
  @singular_rules << [singular, plural]
end

.singular_word(singular, plural) ⇒ Object

Define a singularization exception.



28
29
30
# File 'lib/strokedb/util/inflect.rb', line 28

def singular_word(singular, plural)
  @singular_of[plural] = singular
end

.singularization_rulesObject

Read prepared singularization rules.



54
55
56
57
58
59
60
# File 'lib/strokedb/util/inflect.rb', line 54

def singularization_rules
  return @singularization_rules if @singularization_rules
  sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
  @singularization_rules = sorted.collect do |s, p|
    [ /#{p}$/, "#{s}" ]
  end
end

.word(singular, plural = nil) ⇒ Object

Define a general exception.



21
22
23
24
25
# File 'lib/strokedb/util/inflect.rb', line 21

def word(singular, plural=nil)
  plural = singular unless plural
  singular_word(singular, plural)
  plural_word(singular, plural)
end