spanish_pluralizer 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +53 -3
- data/lib/spanish_pluralizer.rb +58 -77
- metadata +1 -1
data/README.md
CHANGED
@@ -1,4 +1,54 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Spanish Pluralizer
|
2
|
+
=========
|
3
3
|
|
4
|
-
|
4
|
+
Spanish Pluralizer is a gem for converting singular spanish nouns into plural.
|
5
|
+
It works by adding the following methods to String: [:pluralize_spanish, :spanish_pluralizer_acute, :spanish_pluralizer_unacute, :spanish_pluralizer_is_acute?].
|
6
|
+
It can handle diphthong, hiatus and adding or removing acutes, but fails with monosyllables and rule exceptions like "régimen" => "regímenes" and "espécimen => especímenes".
|
7
|
+
|
8
|
+
Usage
|
9
|
+
------------
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'spanish_pluralizer' #not needed if using Gemfile
|
13
|
+
|
14
|
+
'camión'.pluralize_spanish
|
15
|
+
=> 'camiones'
|
16
|
+
|
17
|
+
'caries'.pluralize_spanish
|
18
|
+
=> 'caries'
|
19
|
+
|
20
|
+
'imagen'.pluralize_spanish
|
21
|
+
=> 'imágenes'
|
22
|
+
|
23
|
+
'arroz'.pluralize_spanish
|
24
|
+
=> 'arroces'
|
25
|
+
```
|
26
|
+
|
27
|
+
Installation
|
28
|
+
------------
|
29
|
+
|
30
|
+
Spanish Pluralizer is distributed as a gem.
|
31
|
+
|
32
|
+
You can install it by running
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem install spanish_pluralizer
|
36
|
+
```
|
37
|
+
|
38
|
+
or including the gem in your Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem spanish_pluralizer
|
42
|
+
```
|
43
|
+
|
44
|
+
Credits
|
45
|
+
-------
|
46
|
+
|
47
|
+
Pablo Bianciotto.
|
48
|
+
|
49
|
+
License
|
50
|
+
-------
|
51
|
+
|
52
|
+
Copyright © 2013 Pablo Bianciotto.
|
53
|
+
|
54
|
+
Spanish Pluralizer is a free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|
data/lib/spanish_pluralizer.rb
CHANGED
@@ -5,94 +5,75 @@ end
|
|
5
5
|
|
6
6
|
class String
|
7
7
|
|
8
|
-
|
8
|
+
clashing_methods = self.instance_methods(true) & [:pluralize_spanish, :spanish_pluralizer_acute, :spanish_pluralizer_unacute, :spanish_pluralizer_is_acute?]
|
9
|
+
raise AlreadyDefinedMethod.new("The following instance methods for String have already been defined: #{clashing_methods * ', '}. Are you calling load more than once?") unless clashing_methods.empty?
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
when 'u'
|
21
|
-
'ú'
|
22
|
-
else self
|
23
|
-
end
|
11
|
+
SPANISH_PLURALIZER_ACUTES = {
|
12
|
+
'a' => 'á',
|
13
|
+
'e' => 'é',
|
14
|
+
'i' => 'í',
|
15
|
+
'o' => 'ó',
|
16
|
+
'u' => 'ú'
|
17
|
+
}
|
18
|
+
|
19
|
+
def spanish_pluralizer_acute
|
20
|
+
SPANISH_PLURALIZER_ACUTES.fetch(self, self)
|
24
21
|
end
|
25
22
|
|
26
|
-
def
|
27
|
-
|
28
|
-
when 'á'
|
29
|
-
'a'
|
30
|
-
when 'é'
|
31
|
-
'e'
|
32
|
-
when 'í'
|
33
|
-
'i'
|
34
|
-
when 'ó'
|
35
|
-
'o'
|
36
|
-
when 'ú'
|
37
|
-
'u'
|
38
|
-
else self
|
39
|
-
end
|
23
|
+
def spanish_pluralizer_unacute
|
24
|
+
SPANISH_PLURALIZER_ACUTES.invert.fetch(self, self)
|
40
25
|
end
|
41
26
|
|
42
|
-
def
|
43
|
-
|
27
|
+
def spanish_pluralizer_is_acute?
|
28
|
+
SPANISH_PLURALIZER_ACUTES.values.include? self
|
44
29
|
end
|
45
30
|
|
46
31
|
def pluralize_spanish
|
47
32
|
#orden por probabilidad de ocurrencia para optimizar
|
48
33
|
case self
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
#análisis => análisis
|
69
|
-
self
|
70
|
-
else
|
71
|
-
#imagen => imágenes
|
72
|
-
#palebren => palébrenes
|
73
|
-
#origen => orígenes
|
74
|
-
#orden => órdenes
|
75
|
-
#resumen => resúmenes
|
76
|
-
ending = $~[:last_vowel] + 'nes'
|
77
|
-
$~[:pre].sub(/(a|e|i|o|u)(?=[^aeiou]*$)/, &:acute) + ending
|
78
|
-
end
|
79
|
-
end
|
80
|
-
when /.*z$/
|
81
|
-
#arroz => arroces
|
82
|
-
self[0..-2] + 'ces'
|
34
|
+
#caja => cajas
|
35
|
+
#puente => puentes
|
36
|
+
#papi => papis
|
37
|
+
#mono => monos
|
38
|
+
#río => ríos
|
39
|
+
#palabru => palabrus
|
40
|
+
#sofá => sofás
|
41
|
+
#palabré => palabrés
|
42
|
+
#palabró => palabrós
|
43
|
+
when /.*[aeiouáéó]$/ then self + 's'
|
44
|
+
when /(?<pre>.*)(?<last_vowel>.)(?<ending>[ns])$/
|
45
|
+
if $~[:last_vowel].spanish_pluralizer_is_acute?
|
46
|
+
new_str = self
|
47
|
+
#aguda con tilde => grave sin tilde, a menos que el tilde se mantenga por existir hiato
|
48
|
+
new_str[-2] = new_str[-2].spanish_pluralizer_unacute unless new_str[-3..-1].match(/(a|e|o)(í|ú)/)
|
49
|
+
new_str + 'es'
|
50
|
+
elsif $~[:ending] == 's'
|
51
|
+
#análisis => análisis
|
52
|
+
self
|
83
53
|
else
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
54
|
+
#imagen => imágenes
|
55
|
+
#palebren => palébrenes
|
56
|
+
#origen => orígenes
|
57
|
+
#orden => órdenes
|
58
|
+
#resumen => resúmenes
|
59
|
+
ending = $~[:last_vowel] + 'nes'
|
60
|
+
$~[:pre].sub(/(a|e|i|o|u)(?=[^aeiou]*$)/, &:spanish_pluralizer_acute) + ending
|
61
|
+
end
|
62
|
+
#arroz => arroces
|
63
|
+
when /.*z$/ then self[0..-2] + 'ces'
|
64
|
+
#delantal => delantales
|
65
|
+
#mantel => manteles
|
66
|
+
#mandril => mandriles
|
67
|
+
#tambor => tambores
|
68
|
+
#gandul => gandules
|
69
|
+
#maní => maníes
|
70
|
+
#menú => menúes
|
71
|
+
#rey => reyes
|
72
|
+
#fax => faxes
|
73
|
+
#árbol => árboles
|
74
|
+
#laúd => laúdes
|
75
|
+
else
|
76
|
+
self + 'es'
|
96
77
|
end
|
97
78
|
end
|
98
79
|
end
|