pg_fuzzy_string_matcher 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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Alejandro Juarez
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/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = Description
2
+
3
+ The pg_fuzzy_string_matcher gem provides a simple interface to use the set of functions of contrib/fuzzystringmatching for PostgreSQL.
4
+
5
+ == How to install It
6
+
7
+ Edit your Gemfile and insert the next line:
8
+
9
+ gem "pg_fuzzy_string_matcher"
10
+
11
+ Then use the bundler to install It:
12
+
13
+ $ bundle install
14
+
15
+ Then execute the rails generator:
16
+
17
+ $ ./script/rails g pg_fuzzy_string_matcher AddPgFuzzyStringMatcher
18
+
19
+ And finally run the rails migrations to add the functions for PostgreSQL:
20
+
21
+ $ rake db:migrate
22
+
23
+ == How to use It
24
+
25
+ # scope_by_soundex_matcher
26
+
27
+ class Person < ActiveRecord::Base
28
+ scope_by_soundex_matcher find_by_fullname, :fields => [:firstname, :lastname]
29
+ end
30
+
31
+ Person.find_by_fullname 'Alejandro' => Returns a collection of active_record objects
32
+
33
+
34
+ # scope_by_difference_matcher
35
+
36
+ class Person < ActiveRecord::Base
37
+ scope_by_difference_matcher find_by_fullname, :fields => [:firstname, :lastname]
38
+ end
39
+
40
+ Person.find_by_fullname 'Alejandro' => Returns a collection of active_record objects
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2010 Alejandro Juarez. See LICENSE for details.
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'pg_fuzzy_string_matcher'
@@ -0,0 +1,26 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class PgFuzzyStringMatcherGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
5
+
6
+ desc "Generates a migration file to add Fuzzy String Matching functions to your PostgreSQL database."
7
+
8
+ def self.source_root
9
+ @_pg_fuzzy_string_matcher_source_root ||= File.expand_path("../templates", __FILE__)
10
+ end
11
+
12
+ def self.orm_has_migration?
13
+ Rails::Generators.options[:rails][:orm] == :active_record
14
+ end
15
+
16
+ def self.next_migration_number(path)
17
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
18
+ end
19
+
20
+ class_option :migration, :type => :boolean, :default => orm_has_migration?
21
+
22
+ def copy_migration_template
23
+ return unless options.migration?
24
+ migration_template "migration.rb", "db/migrate/add_pg_fuzzy_string_matcher"
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ class AddPgFuzzyStringMatcher < ActiveRecord::Migration
2
+
3
+ def self.up
4
+ execute "SET search_path = public;"
5
+
6
+ execute "CREATE OR REPLACE FUNCTION levenshtein (text,text) RETURNS int AS '$libdir/fuzzystrmatch','levenshtein' LANGUAGE C IMMUTABLE STRICT;"
7
+
8
+ execute "CREATE OR REPLACE FUNCTION levenshtein (text,text,int,int,int) RETURNS int AS '$libdir/fuzzystrmatch','levenshtein_with_costs' LANGUAGE C IMMUTABLE STRICT;"
9
+
10
+ execute "CREATE OR REPLACE FUNCTION metaphone (text,int) RETURNS text AS '$libdir/fuzzystrmatch','metaphone'LANGUAGE C IMMUTABLE STRICT;"
11
+
12
+ execute "CREATE OR REPLACE FUNCTION soundex(text) RETURNS text AS '$libdir/fuzzystrmatch', 'soundex' LANGUAGE C IMMUTABLE STRICT;"
13
+
14
+ execute "CREATE OR REPLACE FUNCTION text_soundex(text) RETURNS text AS '$libdir/fuzzystrmatch', 'soundex' LANGUAGE C IMMUTABLE STRICT;"
15
+
16
+ execute "CREATE OR REPLACE FUNCTION difference(text,text) RETURNS int AS '$libdir/fuzzystrmatch', 'difference' LANGUAGE C IMMUTABLE STRICT;"
17
+
18
+ execute "CREATE OR REPLACE FUNCTION dmetaphone (text) RETURNS text AS '$libdir/fuzzystrmatch', 'dmetaphone' LANGUAGE C IMMUTABLE STRICT;"
19
+
20
+ execute "CREATE OR REPLACE FUNCTION dmetaphone_alt (text) RETURNS text AS '$libdir/fuzzystrmatch', 'dmetaphone_alt' LANGUAGE C IMMUTABLE STRICT;"
21
+ end
22
+
23
+ def self.down
24
+ execute "SET search_path = public;"
25
+
26
+ execute "DROP FUNCTION dmetaphone_alt (text);"
27
+
28
+ execute "DROP FUNCTION dmetaphone (text);"
29
+
30
+ execute "DROP FUNCTION difference(text,text);"
31
+
32
+ execute "DROP FUNCTION text_soundex(text);"
33
+
34
+ execute "DROP FUNCTION soundex(text);"
35
+
36
+ execute "DROP FUNCTION metaphone (text,int);"
37
+
38
+ execute "DROP FUNCTION levenshtein (text,text,int,int,int);"
39
+
40
+ execute "DROP FUNCTION levenshtein (text,text);"
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ module PgFuzzyStringMatcher
2
+ module Finder
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ # scope_by_soundex_matcher
10
+ #
11
+ # Example:
12
+ # class Person < ActiveRecord::Base
13
+ # scope_by_soundex_matcher find_by_fullname, :fields => [:firstname, :lastname]
14
+ # end
15
+ # Person.find_by_fullname 'Alejandro'
16
+ def scope_by_soundex_matcher(method_name, options={})
17
+ class_eval do
18
+ sql = options[:fields].collect { |field| "soundex(#{field}) = soundex(:q)"}.join(' OR ')
19
+ scope method_name.to_sym, lambda {|string| where(sql, :q => string) }
20
+ end
21
+ end
22
+
23
+ # scope_by_difference_matcher
24
+ #
25
+ # Example:
26
+ # class Person < ActiveRecord::Base
27
+ # scope_by_difference_matcher find_by_fullname, :fields => [:firstname, :lastname]
28
+ # OR
29
+ # scope_by_difference_matcher find_by_fullname, :fields => [:firstname, :lastname], :position => 4, :operator => '>'
30
+ # OR
31
+ # scope_by_difference_matcher find_by_fullname, :by_sql => 'difference(firstname, :q) > 3 OR difference(:lastname, :q) > 4"
32
+ # end
33
+ # Person.find_by_fullname 'Alejandro'
34
+ def scope_by_difference_matcher(method_name, options={})
35
+ class_eval do
36
+ if options[:by_sql].nil?
37
+ options[:position] ||= 3
38
+ options[:operator] = '>'
39
+ sql = options[:fields].collect { |field|"difference(#{field}, :q) #{options[:operator]} #{options[:position]}" }.join(' OR ')
40
+ else
41
+ sql = options[:by_sql]
42
+ end
43
+ scope method_name.to_sym, lambda {|string| where(sql, :q => string) }
44
+ end
45
+ end
46
+
47
+ # TODO: The following PostgreSQL functions are not implemented: dmetaphone_alt, dmetaphone, text_soundex, metaphone and levenshtein.
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module PgFuzzyStringMatcher
2
+
3
+ def enable_activerecord
4
+ return if ActiveRecord::Base.respond_to? :scope_by_soundex_matcher and ActiveRecord::Base.respond_to? :scope_by_difference_matcher
5
+ require 'pg_fuzzy_string_matcher/finder'
6
+ ActiveRecord::Base.send :include, PgFuzzyStringMatcher::Finder
7
+ end
8
+ end
9
+
10
+ if defined? Rails
11
+ include PgFuzzyStringMatcher
12
+ PgFuzzyStringMatcher.enable_activerecord if defined? ActiveRecord
13
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "FuzzyStringMatcher" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'fuzzy_string_matcher'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_fuzzy_string_matcher
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Alejandro Juarez
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-31 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: The pg_fuzzy_string_matcher gem uses the set of functions of contrib/fuzzystringmatching for PostgreSQL.
22
+ email: alex@monsterlabs.com.mx
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - LICENSE
32
+ - init.rb
33
+ - lib/generators/pg_fuzzy_string_matcher/pg_fuzzy_string_matcher_generator.rb
34
+ - lib/generators/pg_fuzzy_string_matcher/templates/migration.rb
35
+ - lib/pg_fuzzy_string_matcher.rb
36
+ - lib/pg_fuzzy_string_matcher/finder.rb
37
+ - README.rdoc
38
+ has_rdoc: true
39
+ homepage: http://github.com/alecz/pg_fuzzy_string_matcher
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.6
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: The pg_fuzzy_string_matcher gem provides several functions to determine similarities and distance between strings.
68
+ test_files:
69
+ - spec/fuzzy_string_matcher_spec.rb
70
+ - spec/spec_helper.rb