acts_as_fuzzy_search 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/acts_as_fuzzy_search.gemspec +22 -0
- data/lib/acts_as_fuzzy_search.rb +8 -0
- data/lib/acts_as_fuzzy_search/fuzzy.rb +90 -0
- data/lib/acts_as_fuzzy_search/version.rb +3 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jordan Babe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ActsAsFuzzySearch
|
2
|
+
|
3
|
+
Return Activerecord records that match a search query. Ideally used for small sets of data and simple search terms.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'acts_as_fuzzy_search'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install acts_as_fuzzy_search
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
# app/models/foobar.rb
|
22
|
+
class Foobar < ActiveRecord::Base
|
23
|
+
acts_as_fuzzy_search
|
24
|
+
|
25
|
+
# Or pass it some options:
|
26
|
+
# acts_as_fuzzy_search(:scope => :your_scope_name, :search_algorithm => :white_similarity)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Elsewhere in your code
|
30
|
+
Foobar.find_by_fuzzy_search 'smthing'
|
31
|
+
|
32
|
+
# Or pass it some options
|
33
|
+
Foobar.find_by_fuzzy_search 'smthing', {:date_format => "%B %d %Y", :search_algorithm => :levenshtein_distance})
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/acts_as_fuzzy_search/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jordan Babe"]
|
6
|
+
gem.email = ["jorbabe@gmail.com"]
|
7
|
+
gem.description = %q{ Return Activerecord records that match a search query. Ideally used for small sets of data and simple search terms.}
|
8
|
+
gem.summary = %q{ AR find based on word matching }
|
9
|
+
gem.homepage = "http://github.com/jbabe/acts_as_fuzzy_search"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "acts_as_fuzzy_search"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActsAsFuzzySearch::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "text", "~> 1.0.0"
|
19
|
+
# gem.add_dependency "fuzzy-string-match", "~> 0.9.0" Drop this until we
|
20
|
+
|
21
|
+
gem.add_development_dependency "minitest"
|
22
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module ActsAsFuzzySearch
|
2
|
+
module Fuzzy
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
DEFAULT_ALGORITHM = :white_similarity
|
12
|
+
MATCH_SCORE = 0.8 # used by jarow-winkler and white-similarity
|
13
|
+
MIN_LEVENSHTEIN_DISTANCE = 3 # number of changes to get the strings to match
|
14
|
+
|
15
|
+
DATE_FORMAT = "%B %d %Y"
|
16
|
+
SCOPE = "all"
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
attr_accessor :config
|
21
|
+
|
22
|
+
def acts_as_fuzzy_search(options = {})
|
23
|
+
@config = { :date_format => DATE_FORMAT,
|
24
|
+
:scope => "all",
|
25
|
+
:search_algorithm => DEFAULT_ALGORITHM,
|
26
|
+
:jarow_score => MATCH_SCORE,
|
27
|
+
:white_similarity_score => MATCH_SCORE,
|
28
|
+
:min_levenshtein_distance => MIN_LEVENSHTEIN_DISTANCE,
|
29
|
+
:debug => false }
|
30
|
+
|
31
|
+
@config.merge!(options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_by_fuzzy_search(search_term, options = {})
|
35
|
+
|
36
|
+
@config.merge!(options)
|
37
|
+
|
38
|
+
search_term = search_term.strip.chomp
|
39
|
+
records = []
|
40
|
+
|
41
|
+
send(config[:scope]).each do |record|
|
42
|
+
|
43
|
+
attrs = record.attributes.values
|
44
|
+
|
45
|
+
# convert dates to ones humans will likely search on
|
46
|
+
attrs.collect! {|a| ([Date,Time,DateTime].include? a.class) ? a.strftime(config[:date_format]) : a }
|
47
|
+
|
48
|
+
# 'create' an html page
|
49
|
+
markup = Nokogiri::HTML(attrs.join(" "))
|
50
|
+
|
51
|
+
# strip all the tags and whitespace
|
52
|
+
no_tags = markup.text.gsub(/\s+|\n/, ' ')
|
53
|
+
|
54
|
+
# Match each word in the document against each search term
|
55
|
+
no_tags.split(" ").each do |word|
|
56
|
+
search_term.split(" ").each do |term|
|
57
|
+
records << record if matches?(term, word)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
return records.uniq
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def matches?(word1, word2)
|
67
|
+
# mostly for the regex - the other matches are probably smart enough
|
68
|
+
word1 = word1.downcase
|
69
|
+
word2 = word2.downcase
|
70
|
+
|
71
|
+
case config[:search_algorithm]
|
72
|
+
when :jarow_winkler
|
73
|
+
# Drop for now cause we're using 1.8
|
74
|
+
raise "Jarow-Winkler not supported" #return FuzzyStringMatch::JaroWinkler.create(:native).getDistance(word1, word2) >= config[:jarow_score]
|
75
|
+
when :white_similarity
|
76
|
+
return Text::WhiteSimilarity.new.similarity(word1,word2) >= config[:white_similarity_score]
|
77
|
+
when :levenshtein_distance
|
78
|
+
return Text::Levenshtein.distance(word1,word2) <= config[:min_levenshtein_distance]
|
79
|
+
else
|
80
|
+
return word1.match(word2).present? # simple regex match
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
ActiveRecord::Base.send :include, ActsAsFuzzySearch::Fuzzy
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_fuzzy_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jordan Babe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: text
|
16
|
+
requirement: &2160283960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2160283960
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
requirement: &2160278160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2160278160
|
36
|
+
description: ! ' Return Activerecord records that match a search query. Ideally used
|
37
|
+
for small sets of data and simple search terms.'
|
38
|
+
email:
|
39
|
+
- jorbabe@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- acts_as_fuzzy_search.gemspec
|
50
|
+
- lib/acts_as_fuzzy_search.rb
|
51
|
+
- lib/acts_as_fuzzy_search/fuzzy.rb
|
52
|
+
- lib/acts_as_fuzzy_search/version.rb
|
53
|
+
homepage: http://github.com/jbabe/acts_as_fuzzy_search
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.8
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: AR find based on word matching
|
77
|
+
test_files: []
|