soundcord 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -1,110 +1,99 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require 'soundcord/integrations/string'
4
+ require 'soundcord/integrations/array'
5
+
3
6
  class SoundCord
4
- def self.phonetize text, use_vogals=false
5
- return handle_text(text, use_vogals)
7
+ def self.phonetize text, options = { :use_vogals => false }
8
+ return handle_text(text, options)
6
9
  end
7
10
 
8
- def self.compare term_1, term_2, use_vogals=false
9
- homophone? term_1, term_2, use_vogals=false
11
+ def self.compare term_1, term_2, options = { :use_vogals => false }
12
+ homophone? term_1, term_2, options
10
13
  end
11
14
 
12
- def self.homophone? term_1, term_2, use_vogals=false
13
- handle_text(term_1, use_vogals) == handle_text(term_2, use_vogals)
15
+ def self.homophone? term_1, term_2, options = { :use_vogals => false }
16
+ phonetize(term_1, options) == phonetize(term_2, options)
14
17
  end
15
18
 
16
19
  private
17
- def self.handle_text text, use_vogals
18
- text.downcase!
19
-
20
- handle_special_chars text
21
- handle_unusual_chars text
22
- handle_unusual_combinations text
23
- handle_terminations text
24
- remove_vogals(text) unless use_vogals
25
- remove_unwanted_chars text
20
+ def self.handle_text text, options = { :use_vogals => false }
21
+ text = text.downcase
22
+
26
23
  text = remove_duplicity text
24
+ text = handle_special_chars text
25
+ text = handle_unusual_chars text
26
+ text = handle_unusual_combinations text
27
+ text = handle_terminations text
28
+ text = remove_vogals(text) unless options[:use_vogals]
29
+ text = remove_unwanted_chars text
27
30
 
28
31
  text.upcase
29
32
  end
30
33
 
31
34
  def self.handle_special_chars text
32
- text.gsub! /(á|à|â|ã)/, 'a'
33
- text.gsub! /(é|è|ê)/, 'e'
34
- text.gsub! /(í|ì|î)/, 'i'
35
- text.gsub! /(ó|ò|ô|õ)/, 'o'
36
- text.gsub! /(ú|ù|û)/, 'u'
35
+ text = text.gsub /(á|à|â|ã)/, 'a'
36
+ text = text.gsub /(é|è|ê)/, 'e'
37
+ text = text.gsub /(í|ì|î)/, 'i'
38
+ text = text.gsub /(ó|ò|ô|õ)/, 'o'
39
+ text = text.gsub /(ú|ù|û)/, 'u'
37
40
  end
38
41
 
39
42
  def self.handle_unusual_chars text
40
- text.gsub! /y/, 'i'
43
+ text = text.gsub /y/, 'i'
41
44
  end
42
45
 
43
46
  def self.handle_unusual_combinations text
44
- text.gsub! /(br|bl)/, 'b'
47
+ text = text.gsub /(br|bl)/, 'b'
45
48
 
46
- text.gsub! /ph/, 'f'
49
+ text = text.gsub /ph/, 'f'
47
50
 
48
- text.gsub! /(gr|mg|ng|rg|gl)/, 'g'
51
+ text = text.gsub /(gr|mg|ng|rg|gl)/, 'g'
49
52
 
50
- text.gsub! /(ge|gi|rj|mj|nj)/, 'j'
53
+ text = text.gsub /(ge|gi|rj|mj|nj)/, 'j'
51
54
 
52
- text.gsub! /(ce|ci|ch|cs)/, 's'
55
+ text = text.gsub /(ce|ci|ch|cs)/, 's'
53
56
 
54
- text.gsub! /ct/, 't'
57
+ text = text.gsub /ct/, 't'
55
58
 
56
- text.gsub! /(q|ca|co|cu|ck|c)/, 'k'
59
+ text = text.gsub /(q|ca|co|cu|ck|c)/, 'k'
57
60
 
58
- text.gsub! /lh/, 'l'
61
+ text = text.gsub /lh/, 'l'
59
62
 
60
- text.gsub! /rm/, 'sm'
63
+ text = text.gsub /rm/, 'sm'
61
64
 
62
- text.gsub! /n/, 'm'
65
+ text = text.gsub /(rm|gm|md|sm|ao\b)/, 'm'
63
66
 
64
- text.gsub! /(rm|gm|md|sm|ao\b)/, 'm'
67
+ text = text.gsub /n/, 'm'
65
68
 
66
- text.gsub! /ao\b/, 'm'
69
+ text = text.gsub /ao\b/, 'm'
67
70
 
68
- text.gsub! /nh/, 'n'
71
+ text = text.gsub /nh/, 'n'
69
72
 
70
- text.gsub! /pr/, 'p'
73
+ text = text.gsub /pr/, 'p'
71
74
 
72
- text.gsub! /(ç|x|ts|c|z|rs)/, 's'
75
+ text = text.gsub /(ç|x|ts|c|z|rs)/, 's'
73
76
 
74
- text.gsub! /(tr|tl|lt|rt|st)/, 's'
77
+ text = text.gsub /(tr|tl|lt|rt|st)/, 's'
75
78
 
76
- text.gsub! /w/, 'v'
79
+ text = text.gsub /w/, 'v'
77
80
  end
78
81
 
79
82
  def self.handle_terminations text
80
- text.gsub! /(s|z|r|m|n|ao|l)\b/, ''
83
+ text = text.gsub /(s|z|r|m|n|ao|l)\b/, ''
81
84
  end
82
85
 
83
86
  def self.remove_vogals text
84
- text.gsub! /(a|e|i|o|u)/, ''
87
+ text = text.gsub /(a|e|i|o|u)/, ''
85
88
  end
86
89
 
87
90
  def self.remove_unwanted_chars text
88
- text.gsub! /h/, ''
91
+ text = text.gsub /h/, ''
89
92
  end
90
93
 
91
94
  def self.remove_duplicity text
92
- text.split(//).uniq.inject("") { |s,n| s+n }
95
+ text.split(//).inject("") do |s,n|
96
+ s + ((s[s.length-1..s.length-1] === n) ? '' : n )
97
+ end
93
98
  end
94
99
  end
95
-
96
-
97
- class String
98
- def phonetize options = { :use_vogals => false }
99
- SoundCord.phonetize self, options[:use_vogals]
100
- end
101
- def compare_phntc compared
102
- SoundCord.compare self, compared
103
- end
104
- def compare_phonetically compared
105
- compare_phntc compared
106
- end
107
- def homophone? compared
108
- SoundCord.homophone? self, compared
109
- end
110
- end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def homophones value
3
+ self.select { |i| i.homophone? value }
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ class String
2
+ def phonetize options = { :use_vogals => false }
3
+ SoundCord.phonetize self, options
4
+ end
5
+
6
+ def self.phonetize value
7
+ value.phonetize
8
+ end
9
+
10
+ # DEPRECATED: Please use homophone? instead.
11
+ def compare_phntc compared
12
+ warn "[DEPRECATION] `compare_phntc` is deprecated. Please use `homophone?` instead."
13
+ self.homophone? compared
14
+ end
15
+ # DEPRECATED: Please use homophone? instead.
16
+ def compare_phonetically compared
17
+ warn "[DEPRECATION] `compare_phonetically` is deprecated. Please use `homophone?` instead."
18
+ self.homophone? compared
19
+ end
20
+
21
+ def homophone? compared
22
+ SoundCord.homophone? self, compared
23
+ end
24
+ end
@@ -0,0 +1,8 @@
1
+ module SoundCord
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 3
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{soundcord}
3
+ s.author = 'Lukas Alexandre'
4
+ s.email = 'lukeskytm@gmail.com'
5
+ s.homepage = 'https://github.com/lukasalexandre/soundcord'
6
+ s.version = "0.1.1"
7
+ s.date = Date.today
8
+ s.summary = %q{A phonetic algorithm implementation}
9
+ s.description = "A phonetic algorithm to make comparison by phonetically similar terms easier."
10
+ s.files = Dir["{lib/**/*.rb,README.rdoc,test/**/*.rb,Rakefile,*.gemspec}"]
11
+ s.require_paths = ["lib"]
12
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'soundcord/integrations/array'
5
+
6
+ class SoundCordTest < Test::Unit::TestCase
7
+ def test_homophones_method_existance
8
+ assert_equal Array.new.respond_to?(:homophones), true
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'soundcord'
5
+
6
+ class SoundCordTest < Test::Unit::TestCase
7
+ def test_simple_words
8
+ assert_equal "João".phonetize, "J"
9
+ assert_equal "Maria".phonetize, "MR"
10
+ assert_equal "Helena".phonetize, "LM"
11
+ assert_equal "Valmir".phonetize, "VLM"
12
+ assert_equal "Walmir".phonetize, "VLM"
13
+ end
14
+ def test_simple_comparations
15
+ assert_equal true, "Joao".homophone?("João")
16
+ assert_equal true, "Helena".homophone?("Elena")
17
+ assert_equal true, "Walmir".homophone?("Valmir")
18
+ assert_equal true, "Marria".homophone?("Maria")
19
+ assert_equal true, "Wagner".homophone?("Vagner")
20
+ assert_equal true, "Mirela".homophone?("Mirella")
21
+ assert_equal true, "Artur".homophone?("Arthur")
22
+ assert_equal true, "Diego".homophone?("Dyego")
23
+ assert_equal true, "Felipe".homophone?("Phelipe")
24
+ assert_equal true, "Filipe".homophone?("Felipe")
25
+ assert_equal true, "Phelipe".homophone?("Filipe")
26
+ assert_equal true, "Philippe".homophone?("Felipe")
27
+ end
28
+ def test_use_vogals_option
29
+ assert_equal "ELEMA", "Helena".phonetize(:use_vogals => true)
30
+ end
31
+ def test_find_in_collection
32
+ list = %w( saola paulo saulo ricardo sallo )
33
+ expected = %w( saola saulo sallo )
34
+ assert_equal expected, list.homophones("saulo")
35
+ list = %w( leonardo lucene rodrigo luciana lussene )
36
+ expected = %w( lucene luciana lussene )
37
+ assert_equal expected, list.homophones("lucene")
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'test/unit'
4
+ require 'soundcord/integrations/string'
5
+
6
+ class SoundCordTest < Test::Unit::TestCase
7
+ def test_phonetize_method_existance_in_instance
8
+ assert_equal String.new.respond_to?(:phonetize), true
9
+ end
10
+ def test_phonetize_method_existance_in_class
11
+ assert_equal String.respond_to?(:phonetize), true
12
+ end
13
+ def test_homophone_method_existance
14
+ assert_equal String.new.respond_to?(:homophone?), true
15
+ end
16
+ end
metadata CHANGED
@@ -1,46 +1,75 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: soundcord
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Lukas Alexandre
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-06-13 00:00:00.000000000 Z
17
+
18
+ date: 2012-06-18 00:00:00 -03:00
19
+ default_executable:
13
20
  dependencies: []
14
- description: A phonetic algorithm to make comparison by phonetically similar terms
15
- easier.
21
+
22
+ description: A phonetic algorithm to make comparison by phonetically similar terms easier.
16
23
  email: lukeskytm@gmail.com
17
24
  executables: []
25
+
18
26
  extensions: []
27
+
19
28
  extra_rdoc_files: []
20
- files:
29
+
30
+ files:
31
+ - lib/soundcord/integrations/array.rb
32
+ - lib/soundcord/integrations/string.rb
33
+ - lib/soundcord/version.rb
21
34
  - lib/soundcord.rb
35
+ - test/test_array.rb
36
+ - test/test_soundcord.rb
37
+ - test/test_string.rb
38
+ - Rakefile
39
+ - soundcord.gemspec
40
+ has_rdoc: true
22
41
  homepage: https://github.com/lukasalexandre/soundcord
23
42
  licenses: []
43
+
24
44
  post_install_message:
25
45
  rdoc_options: []
26
- require_paths:
46
+
47
+ require_paths:
27
48
  - lib
28
- required_ruby_version: !ruby/object:Gem::Requirement
49
+ required_ruby_version: !ruby/object:Gem::Requirement
29
50
  none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
59
  none: false
36
- requirements:
37
- - - ! '>='
38
- - !ruby/object:Gem::Version
39
- version: '0'
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
40
67
  requirements: []
68
+
41
69
  rubyforge_project:
42
- rubygems_version: 1.8.24
70
+ rubygems_version: 1.5.3
43
71
  signing_key:
44
72
  specification_version: 3
45
73
  summary: A phonetic algorithm implementation
46
74
  test_files: []
75
+