portuguese_pluralizer 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/portuguese_pluralizer.rb +139 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24fe77b3747fa45bf5d0b78a5f3bd54fb23d0ce0
4
+ data.tar.gz: 19a14ffae831d8431813041c0b889aca26a5087d
5
+ SHA512:
6
+ metadata.gz: f4e0016b9c351e146e195ecb64a7def81e433949250a9c41c9fe668d3adf8587697e01b856e1d678a7604f896c81557f45863bd21f3a31990fb2913f12c11750
7
+ data.tar.gz: 4ccfa40ff1bb16fe293921c443b5b1836be51ace7b45533bf39af97dcd124eaef470e51fa7ca6b03677b1468d8e5aced662b7af1ef6a22695757419361eb6825
@@ -0,0 +1,139 @@
1
+ # encoding: utf-8
2
+ class String
3
+
4
+ PORTUGUESE_CIRCUMFLEX_VOWELS = "êô"
5
+ PORTUGUESE_TILDE_VOWELS = "ãõ"
6
+ PORTUGUESE_ACUTE_VOWELS = "áéíó"
7
+ PORTUGUESE_ACCENTED_VOWELS = PORTUGUESE_ACUTE_VOWELS + PORTUGUESE_CIRCUMFLEX_VOWELS + PORTUGUESE_TILDE_VOWELS
8
+ PORTUGUESE_NON_ACCENTED_VOWELS = "aeiou"
9
+ PORTUGUESE_VOWELS = PORTUGUESE_NON_ACCENTED_VOWELS + PORTUGUESE_ACCENTED_VOWELS
10
+ PORTUGUESE_CONSONANTS = "bcçdfghjlmnpqrsvxz"
11
+
12
+ PORTUGUESE_ACUTE_VOWEL_MAP = {
13
+ 'a' => 'á',
14
+ 'e' => 'é',
15
+ 'i' => 'í',
16
+ 'o' => 'ó',
17
+ 'u' => 'ú'
18
+ }
19
+
20
+ PORTUGUESE_CIRCUMFLEX_VOWEL_MAP = {
21
+ 'e' => 'ê',
22
+ 'o' => 'ô'
23
+ }
24
+
25
+ PORTUGUESE_TILDE_VOWEL_MAP = {
26
+ 'a' => 'ã',
27
+ 'o' => 'õ'
28
+ }
29
+
30
+ PORTUGUESE_UNACCENT_VOWEL_MAP = {}
31
+ PORTUGUESE_UNACCENT_VOWEL_MAP.merge!(PORTUGUESE_ACUTE_VOWEL_MAP.invert)
32
+ PORTUGUESE_UNACCENT_VOWEL_MAP.merge!(PORTUGUESE_CIRCUMFLEX_VOWEL_MAP.invert)
33
+ PORTUGUESE_UNACCENT_VOWEL_MAP.merge!(PORTUGUESE_TILDE_VOWEL_MAP.invert)
34
+
35
+ def portuguese_pluralizer_unaccent
36
+ PORTUGUESE_UNACCENT_VOWEL_MAP.fetch(self,self)
37
+ end
38
+
39
+ def portuguese_pluralizer_has_accent?
40
+ if m = match(/["#{PORTUGUESE_ACCENTED_VOWELS}"]/)
41
+ true
42
+ else
43
+ false
44
+ end
45
+ end
46
+
47
+ def portuguese_pluralizer_last_vowel
48
+ m = match(/\w*(["#{PORTUGUESE_VOWELS}"])["#{PORTUGUESE_CONSONANTS}"]?$/)
49
+ m[1].to_s
50
+ end
51
+
52
+ def portuguese_pluralizer_acute
53
+ PORTUGUESE_ACUTE_VOWEL_MAP.fetch(self,self)
54
+ end
55
+
56
+ def portuguese_pluralizer_unacute
57
+ PORTUGUESE_ACUTE_VOWEL_MAP.invert.fetch(self,self)
58
+ end
59
+
60
+ def portuguese_pluralizer_circumflex
61
+ PORTUGUESE_CIRCUMFLEX_MAP.fetch(self,self)
62
+ end
63
+
64
+ def portuguese_pluralizer_uncircumflex
65
+ PORTUGUESE_CIRCUMFLEX_MAP.invert.fetch(self,self)
66
+ end
67
+
68
+ def portuguese_pluralizer_tilde
69
+ PORTUGUESE_TILDE_VOWEL_MAP.fetch(self,self)
70
+ end
71
+
72
+ def portuguese_pluralizer_untilde
73
+ PORTUGUESE_TILDE_VOWEL_MAP.invert.fetch(self,self)
74
+ end
75
+
76
+ def portuguese_pluralizer_last_consonant
77
+ m = match(/\w*["#{PORTUGUESE_VOWELS}"]?(["#{PORTUGUESE_CONSONANTS}"])$/)
78
+ m[1].to_s
79
+ end
80
+
81
+ def portuguese_pluralizer_last_letter
82
+ self[-1]
83
+ end
84
+
85
+ def pluralize_portuguese
86
+ case self
87
+ # If the singular ends in -n just add -s
88
+ when /\w*["#{PORTUGUESE_VOWELS}"]n$/ then self + 's'
89
+ # If the singular ends in -m, drop -m and add -ns
90
+ when /\w*["#{PORTUGUESE_VOWELS}"]m$/ then gsub(/m$/,'ns')
91
+ # If the singular ends in ãe just add -s
92
+ when /\w*ãe$/ then self + 's'
93
+ # If the singular ends with -ção or -são add -ões
94
+ when /\w*[çs]ão$/ then gsub(/ão/,"ões")
95
+ # If the singular ends with r,s,z or x...
96
+ when /\w*[rszx]$/
97
+ # If the singular ends with s or x
98
+ if portuguese_pluralizer_last_letter == "s" or portuguese_pluralizer_last_letter == "x"
99
+ # And the last vowel is accented, unaccent it before adding -es
100
+ if portuguese_pluralizer_last_vowel.portuguese_pluralizer_has_accent?
101
+ gsub!(/#{portuguese_pluralizer_last_vowel}/,portuguese_pluralizer_last_vowel.portuguese_pluralizer_unaccent)
102
+ self + 'es'
103
+ # But if the last vowel isn't accented and ends in s just return singular as is for plural
104
+ else
105
+ self
106
+ end
107
+ # If it doesn't end in s or x but r or z just add -es
108
+ else
109
+ self + 'es'
110
+ end
111
+ # If ends with an unaccented vowel and l
112
+ when /\w*["#{PORTUGUESE_NON_ACCENTED_VOWELS}"]l$/
113
+ # And the last vowel is i or e
114
+ if portuguese_pluralizer_last_vowel == 'i' or portuguese_pluralizer_last_vowel == 'e'
115
+ # And if it has an accent swap the last vowel + l with -eis
116
+ if portuguese_pluralizer_has_accent?
117
+ gsub(/#{portuguese_pluralizer_last_vowel}l$/,"eis")
118
+ # And if it doesn't have an acccent and the last vowel is i, swap the last vowel + l with -is
119
+ elsif portuguese_pluralizer_last_vowel == 'i'
120
+ gsub(/#{portuguese_pluralizer_last_vowel}l$/,"is")
121
+ # Otherwise if it doesn't have an accent and the the last vowel is e, give the last vowel an acute accent and add -is
122
+ elsif portuguese_pluralizer_last_vowel == 'e'
123
+ gsub(/#{portuguese_pluralizer_last_vowel}l$/,portuguese_pluralizer_last_vowel.portuguese_pluralizer_acute + "is")
124
+ end
125
+ # If the last vowel is o and
126
+ elsif portuguese_pluralizer_last_vowel == 'o'
127
+ # and has an accent just turn the -l to -is
128
+ if portuguese_pluralizer_has_accent?
129
+ gsub(/l$/,"is")
130
+ else
131
+ gsub(/#{portuguese_pluralizer_last_vowel}l$/,portuguese_pluralizer_last_vowel.portuguese_pluralizer_acute + "is")
132
+ end
133
+ else
134
+ gsub(/l$/,"is")
135
+ end
136
+ end
137
+ end
138
+
139
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: portuguese_pluralizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Martinez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem that extends the String class to allow pluralization of
14
+ Portuguese nouns
15
+ email:
16
+ - rubyproblemsolving@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/portuguese_pluralizer.rb
22
+ homepage: http://rubygems.org/gems/portuguese_pluralizer
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Portuguese Pluralizer
46
+ test_files: []