spanish_pluralizer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +53 -3
  2. data/lib/spanish_pluralizer.rb +58 -77
  3. metadata +1 -1
data/README.md CHANGED
@@ -1,4 +1,54 @@
1
- spanish_pluralizer
2
- ==================
1
+ Spanish Pluralizer
2
+ =========
3
3
 
4
- An alpha version of a pluralizer
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.
@@ -5,94 +5,75 @@ end
5
5
 
6
6
  class String
7
7
 
8
- raise AlreadyDefinedMethod.new unless (self.instance_methods(true) & [:acute, :unacute, :pluralize_spanish, :is_acute?]).empty?
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
- def acute
11
- case self
12
- when 'a'
13
- 'á'
14
- when 'e'
15
- 'é'
16
- when 'i'
17
- 'í'
18
- when 'o'
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 unacute
27
- case self
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 is_acute?
43
- ['á','é','í','ó','ú'].include? self
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
- when /.*[aeiouáéó]$/
50
- #caja => cajas
51
- #puente => puentes
52
- #papi => papis
53
- #mono => monos
54
- #río => ríos
55
- #palabru => palabrus
56
- #sofá => sofás
57
- #palabré => palabrés
58
- #palabró => palabrós
59
- self + 's'
60
- when /(?<pre>.*)(?<last_vowel>.)(?<ending>[ns])$/
61
- if $~[:last_vowel].is_acute?
62
- new_str = self
63
- #aguda con tilde => grave sin tilde, a menos que el tilde se mantenga por existir hiato
64
- new_str[-2] = new_str[-2].unacute unless new_str[-3..-1].match(/(a|e|o)(í|ú)/)
65
- new_str + 'es'
66
- else
67
- if $~[:ending] == 's'
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
- #delantal => delantales
85
- #mantel => manteles
86
- #mandril => mandriles
87
- #tambor => tambores
88
- #gandul => gandules
89
- #maní => maníes
90
- #menú => menúes
91
- #rey => reyes
92
- #fax => faxes
93
- #árbol => árboles
94
- #laúd => laúdes
95
- self + 'es'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanish_pluralizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: