fonetica 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in soundex.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fonetica (0.1.0)
5
+ activerecord (>= 3.0.0)
6
+ activesupport (>= 3.0.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.1)
12
+ activesupport (= 3.0.1)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.4.1)
15
+ activerecord (3.0.1)
16
+ activemodel (= 3.0.1)
17
+ activesupport (= 3.0.1)
18
+ arel (~> 1.0.0)
19
+ tzinfo (~> 0.3.23)
20
+ activesupport (3.0.1)
21
+ arel (1.0.1)
22
+ activesupport (~> 3.0.0)
23
+ builder (2.1.2)
24
+ diff-lcs (1.1.2)
25
+ i18n (0.4.1)
26
+ rspec (2.0.0)
27
+ rspec-core (= 2.0.0)
28
+ rspec-expectations (= 2.0.0)
29
+ rspec-mocks (= 2.0.0)
30
+ rspec-core (2.0.0)
31
+ rspec-expectations (2.0.0)
32
+ diff-lcs (>= 1.1.2)
33
+ rspec-mocks (2.0.0)
34
+ rspec-core (= 2.0.0)
35
+ rspec-expectations (= 2.0.0)
36
+ tzinfo (0.3.23)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ activerecord (>= 3.0.0)
43
+ activesupport (>= 3.0.0)
44
+ fonetica!
45
+ rspec (~> 2.0.0)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Gabriel Sobrinho
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
data/fonetica.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fonetica/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fonetica"
7
+ s.version = Fonetica::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Gabriel Sobrinho", "Wenderson Malheiros"]
10
+ s.email = ["gabriel.sobrinho@gmail.com", "wmalheiros@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/soundex"
12
+ s.summary = %q{Soundex finder for ActiveRecord}
13
+ s.description = %q{Search records using soundex algorithm}
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
19
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
20
+ s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
21
+ end
data/lib/fonetica.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'fonetica/string'
2
+
3
+ module Fonetica
4
+ end
5
+
6
+ if defined?(::Rails::Railtie)
7
+ require 'fonetica/railtie'
8
+ end
@@ -0,0 +1,28 @@
1
+ module Fonetica
2
+ module ActiveRecord
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def has_fonetica_for(column)
7
+ cattr_accessor :fonetica_column, :fonetica_cache_column
8
+
9
+ self.fonetica_column = column
10
+ self.fonetica_cache_column = "#{column}_fonetica"
11
+
12
+ scope :fonetica, lambda { |fonetica|
13
+ {
14
+ :conditions => ["#{quoted_table_name}.#{fonetica_cache_column} LIKE ?", "#{fonetica.to_s.fonetica}%"]
15
+ }
16
+ }
17
+
18
+ before_save :update_fonetica
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+ def update_fonetica
24
+ write_attribute(fonetica_cache_column, read_attribute(fonetica_column).to_s.fonetica)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Fonetica
2
+ class Railtie < Rails::Railtie
3
+ initializer 'fonetica.active_record' do
4
+ ActiveSupport.on_load :active_record do
5
+ require 'fonetica/active_record'
6
+ include Fonetica::ActiveRecord
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ class String
2
+ FONETICA = [
3
+ [/Y/, 'I'],
4
+ [/B[RL]/, 'B'],
5
+ [/PH/, 'F'],
6
+ [/[MNR]G/, 'G'],
7
+ [/G[EI]|[RMN]J/, 'J'],
8
+ [/G[RL]/, 'G'],
9
+ [/C[EIH]/, 'S'],
10
+ [/CT/, 'T'],
11
+ [/CS/, 'S'],
12
+ [/Q|C[AOUK]?/, 'K'],
13
+ [/LH/, 'L'],
14
+ [/RM/, 'SM'],
15
+ [/N/, 'M'],
16
+ [/MD|GM|AO\b/, 'M'],
17
+ [/NH/, 'N'],
18
+ [/PR/, 'P'],
19
+ [/X|TS|C|Z|RS/, 'S'],
20
+ [/T[RL]/, 'T'],
21
+ [/[LRS]T/, 'T'],
22
+ [/W/, 'V'],
23
+ [/[SZRMNL]\b/, ''],
24
+ [/L/, 'R'],
25
+ [/[AEIOUH]/, '']
26
+ ]
27
+
28
+ def fonetica
29
+ word = self.gsub(/ç/i, 's') # special case
30
+ word = I18n.transliterate(word).upcase
31
+
32
+ FONETICA.each do |search, replace|
33
+ word.gsub!(search, replace)
34
+ end
35
+
36
+ word.squeeze
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ module Fonetica
2
+ module Version #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'fonetica'
@@ -0,0 +1,237 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+ context 'BR = BL = B' do
5
+ it '"broco" and "bloco" should fonetica to "BK"' do
6
+ 'broco'.fonetica.should == 'BK'
7
+ 'bloco'.fonetica.should == 'BK'
8
+ end
9
+ end
10
+
11
+ context 'CA = CO = CU = CK = K' do
12
+ it '"casa" and "kasa" should fonetica to "KS"' do
13
+ 'casa'.fonetica.should == 'KS'
14
+ 'kasa'.fonetica.should == 'KS'
15
+ end
16
+
17
+ it '"coroar" and "koroar" should fonetica to "KR"' do
18
+ 'coroar'.fonetica.should == 'KR'
19
+ 'koroar'.fonetica.should == 'KR'
20
+ end
21
+
22
+ it '"cuba" and "kuba" should fonetica to "KB"' do
23
+ 'cuba'.fonetica.should == 'KB'
24
+ 'kuba'.fonetica.should == 'KB'
25
+ end
26
+ end
27
+
28
+ context 'CE = CI = Ç = S' do
29
+ it '"cela" and "sela" should fonetica to "SR"' do
30
+ 'cela'.fonetica.should == 'SR'
31
+ 'sela'.fonetica.should == 'SR'
32
+ end
33
+
34
+ it '"circo" and "sirco" should fonetica to "SRK"' do
35
+ 'circo'.fonetica.should == 'SRK'
36
+ 'sirco'.fonetica.should == 'SRK'
37
+ end
38
+
39
+ it '"roça" and "rosa" should fonetica to "RS"' do
40
+ 'roça'.fonetica.should == 'RS'
41
+ 'rosa'.fonetica.should == 'RS'
42
+ end
43
+ end
44
+
45
+ context 'CH = X = S' do
46
+ it '"ameixa" and "ameicha" should fonetica to "MS"' do
47
+ 'ameixa'.fonetica.should == 'MS'
48
+ 'ameicha'.fonetica.should == 'MS'
49
+ end
50
+ end
51
+
52
+ context 'CS = S' do
53
+ it '"toracs" and "torax" should fonetica to "TR"' do
54
+ 'toracs'.fonetica.should == 'TR'
55
+ 'torax'.fonetica.should == 'TR'
56
+ end
57
+ end
58
+
59
+ context 'CT = T' do
60
+ it '"compactar" and "compatar" should fonetica to "KMPT"' do
61
+ 'compactar'.fonetica.should == 'KMPT'
62
+ 'compatar'.fonetica.should == 'KMPT'
63
+ end
64
+ end
65
+
66
+ context 'GA = GO = GU = GL = GR = G' do
67
+ it '"gana" should fonetica to "KMPT"' do
68
+ 'gana'.fonetica.should == 'GM'
69
+ end
70
+
71
+ it '"gostar" should fonetica to "GT"' do
72
+ 'gostar'.fonetica.should == 'GT'
73
+ end
74
+
75
+ it '"guabiru" should fonetica to "GBR"' do
76
+ 'guabiru'.fonetica.should == 'GBR'
77
+ end
78
+
79
+ it '"negro" and "nego" should fonetica to "MG"' do
80
+ 'negro'.fonetica.should == 'MG'
81
+ 'nego'.fonetica.should == 'MG'
82
+ end
83
+
84
+ it '"hieróglifo" and "hierógrifo" should fonetica to "RGF"' do
85
+ 'hieróglifo'.fonetica.should == 'RGF'
86
+ 'hierógrifo'.fonetica.should == 'RGF'
87
+ end
88
+ end
89
+
90
+ context 'GE = GI = J' do
91
+ it '"gene" should fonetica to "JM"' do
92
+ 'gene'.fonetica.should == 'JM'
93
+ end
94
+
95
+ it '"gibi" should fonetica to "JB"' do
96
+ 'gibi'.fonetica.should == 'JB'
97
+ end
98
+ end
99
+
100
+ context 'GM = M' do
101
+ it '"fleugma" should fonetica to "FRM"' do
102
+ 'fleugma'.fonetica.should == 'FRM'
103
+ end
104
+ end
105
+
106
+ context 'L = R' do
107
+ it '"luminar" and "ruminar" should fonetica to "RM"' do
108
+ 'luminar'.fonetica.should == 'RM'
109
+ 'ruminar'.fonetica.should == 'RM'
110
+ end
111
+ end
112
+
113
+ context 'N = M' do
114
+ it '"mudez" and "nudez" should fonetica to "MD"' do
115
+ 'mudez'.fonetica.should == 'MD'
116
+ 'nudez'.fonetica.should == 'MD'
117
+ end
118
+ end
119
+
120
+ context 'MD = D' do
121
+ it '"comendo" and "comeno" should fonetica to "KM"' do
122
+ 'comendo'.fonetica.should == 'KM'
123
+ 'comeno'.fonetica.should == 'KM'
124
+ end
125
+ end
126
+
127
+ context 'MG = G and MJ = J' do
128
+ it '"bunginganga" and "bugiganga" should fonetica to "BJG"' do
129
+ 'bunginganga'.fonetica.should == 'BJG'
130
+ 'bugiganga'.fonetica.should == 'BJG'
131
+ end
132
+ end
133
+
134
+ context 'PH = F and PR = P' do
135
+ it '"philipe" and "felipe" should fonetica to "FRP"' do
136
+ 'philipe'.fonetica.should == 'FRP'
137
+ 'felipe'.fonetica.should == 'FRP'
138
+ end
139
+ end
140
+
141
+ context 'PR = P' do
142
+ it '"estupro" and "estrupo" should fonetica to "TP"' do
143
+ 'estupro'.fonetica.should == 'TP'
144
+ 'estrupo'.fonetica.should == 'TP'
145
+ end
146
+ end
147
+
148
+ context 'Q = K' do
149
+ it '"queijo" and "keijo" should fonetica to "KJ"' do
150
+ 'queijo'.fonetica.should == 'KJ'
151
+ 'keijo'.fonetica.should == 'KJ'
152
+ end
153
+ end
154
+
155
+ context 'RG = G, RS = S, and RT = T' do
156
+ it '"lagarto" and "largato" should fonetica to "RGT"' do
157
+ 'lagarto'.fonetica.should == 'RGT'
158
+ 'largato'.fonetica.should == 'RGT'
159
+ end
160
+
161
+ it '"perspectiva" and "pespectiva" should fonetica to "PSPTV"' do
162
+ 'perspectiva'.fonetica.should == 'PSPTV'
163
+ 'pespectiva'.fonetica.should == 'PSPTV'
164
+ end
165
+
166
+ it '"lagartixa" and "largatixa" should fonetica to "RGTS"' do
167
+ 'lagartixa'.fonetica.should == 'RGTS'
168
+ 'largatixa'.fonetica.should == 'RGTS'
169
+ end
170
+ end
171
+
172
+ context 'RM = SM' do
173
+ it '"mesmo" and "mermo" should fonetica to "MSM"' do
174
+ 'mesmo'.fonetica.should == 'MSM'
175
+ 'mermo'.fonetica.should == 'MSM'
176
+ end
177
+ end
178
+
179
+ context 'RJ = J' do
180
+ it '"virgem" and "vige" should fonetica to "VJ"' do
181
+ 'virgem'.fonetica.should == 'VJ'
182
+ 'vige'.fonetica.should == 'VJ'
183
+ end
184
+ end
185
+
186
+ context 'ST = T' do
187
+ it '"superstição" and "supertição" should fonetica to "SPTS"' do
188
+ 'superstição'.fonetica.should == 'SPTS'
189
+ 'supertição'.fonetica.should == 'SPTS'
190
+ end
191
+ end
192
+
193
+ context 'TR = T, TL = T, and TS = T' do
194
+ it '"estupro" and "estrupo" should fonetica to "TP"' do
195
+ 'estupro'.fonetica.should == 'TP'
196
+ 'estrupo'.fonetica.should == 'TP'
197
+ end
198
+
199
+ it '"contrato" and "contlato" should fonetica to "KMT"' do
200
+ 'contrato'.fonetica.should == 'KMT'
201
+ 'contlato'.fonetica.should == 'KMT'
202
+ end
203
+
204
+ it '"kubitscheck" and "kubixeque" should fonetica to "KBSK"' do
205
+ 'kubitscheck'.fonetica.should == 'KBSK'
206
+ 'kubixeque'.fonetica.should == 'KBSK'
207
+ end
208
+ end
209
+
210
+ context 'W = V' do
211
+ it '"walter" and "valter" should fonetica to "VT"' do
212
+ 'walter'.fonetica.should == 'VT'
213
+ 'valter'.fonetica.should == 'VT'
214
+ end
215
+ end
216
+
217
+ context 'X = S' do
218
+ it '"exceder" and "esceder" should fonetica to "SD"' do
219
+ 'exceder'.fonetica.should == 'SD'
220
+ 'esceder'.fonetica.should == 'SD'
221
+ end
222
+ end
223
+
224
+ context 'Y = I' do
225
+ it '"yara" and "iara" should fonetica to "R"' do
226
+ 'yara'.fonetica.should == 'R'
227
+ 'iara'.fonetica.should == 'R'
228
+ end
229
+ end
230
+
231
+ context 'Z = S' do
232
+ it '"casa" and "caza" should fonetica to "KS"' do
233
+ 'casa'.fonetica.should == 'KS'
234
+ 'caza'.fonetica.should == 'KS'
235
+ end
236
+ end
237
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fonetica
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Gabriel Sobrinho
14
+ - Wenderson Malheiros
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-16 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: activesupport
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: activerecord
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 7
47
+ segments:
48
+ - 3
49
+ - 0
50
+ - 0
51
+ version: 3.0.0
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 15
63
+ segments:
64
+ - 2
65
+ - 0
66
+ - 0
67
+ version: 2.0.0
68
+ type: :development
69
+ version_requirements: *id003
70
+ description: Search records using soundex algorithm
71
+ email:
72
+ - gabriel.sobrinho@gmail.com
73
+ - wmalheiros@gmail.com
74
+ executables: []
75
+
76
+ extensions: []
77
+
78
+ extra_rdoc_files: []
79
+
80
+ files:
81
+ - .gitignore
82
+ - .rspec
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - MIT-LICENSE
86
+ - Rakefile
87
+ - autotest/discover.rb
88
+ - fonetica.gemspec
89
+ - lib/fonetica.rb
90
+ - lib/fonetica/active_record.rb
91
+ - lib/fonetica/railtie.rb
92
+ - lib/fonetica/string.rb
93
+ - lib/fonetica/version.rb
94
+ - spec/spec_helper.rb
95
+ - spec/string_spec.rb
96
+ has_rdoc: true
97
+ homepage: http://rubygems.org/gems/soundex
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.3.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Soundex finder for ActiveRecord
130
+ test_files:
131
+ - spec/spec_helper.rb
132
+ - spec/string_spec.rb