ruby-string-match-scorer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ tmp
2
+ .idea
3
+ .DS_Store
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bodaniel Jeanes
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.
@@ -0,0 +1,58 @@
1
+ # Usage
2
+
3
+ Just require `string_scorer` into your Ruby code and use the `String#score`
4
+ method to find out the score for a matching string. Especially useful for
5
+ sorting arrays of strings in search results etc...
6
+
7
+ require 'string_scorer'
8
+
9
+ string = "Hello, World"
10
+
11
+ puts string.score("xyz") # => 0.0
12
+ puts string.score("Hello World") # => 0.915384615384615
13
+ puts string.score("H,W") # => 0.642307692307692
14
+ puts string.score("orl") # => 0.353846153846154
15
+ puts string.score("ll") # => 0.707692307692308
16
+
17
+ puts string.score("xyz", :quicksilver) # => 0.0
18
+ puts string.score("Hello World", :quicksilver) # => 0.708333333333333
19
+ puts string.score("H,W", :quicksilver) # => 0.568402777777778
20
+ puts string.score("orl", :quicksilver) # => 0.75
21
+ puts string.score("ll", :quicksilver) # => 0.766666666666667
22
+
23
+ # Other Stuff
24
+
25
+ You can use different scoring algorithms. Out of the box this comes with a
26
+ direct port of the [QuickSilver](http://is.gd/CUqC) algorithm and the adapted
27
+ [LiquidMetal](http://github.com/rmm5t/liquidmetal) one.
28
+
29
+ LiquidMetal scorer is faster with longer strings and also handles case
30
+ differences better.
31
+
32
+ It's easy to create other scoring algorithms, just look at any of the files
33
+ in `lib/string_scorer/scorers` directory
34
+
35
+ # License
36
+
37
+ Copyright (c) 2009 Bodaniel Jeanes
38
+
39
+ Permission is hereby granted, free of charge, to any person
40
+ obtaining a copy of this software and associated documentation
41
+ files (the "Software"), to deal in the Software without
42
+ restriction, including without limitation the rights to use,
43
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
44
+ copies of the Software, and to permit persons to whom the
45
+ Software is furnished to do so, subject to the following
46
+ conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
53
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
55
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
56
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
57
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
58
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ruby-string-match-scorer"
8
+ gem.summary = "Quicksilver-like string match scoring"
9
+ gem.description = "Flex matching short abbreviations against longer strings is a boon in productivity for typists. Applications like Quicksilver, LaunchBar, and Launchy have made this method of keyboard entry a popular one."
10
+ gem.email = "me@bjeanes.com"
11
+ gem.homepage = "http://github.com/bjeanes/ruby-string-match-scorer"
12
+ gem.authors = ["Bodaniel Jeanes"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "ruby-string-match-scorer #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ require 'string_scorer/scorer'
2
+ require 'string_scorer/string_ext'
@@ -0,0 +1,25 @@
1
+ module StringScorer
2
+ class << self
3
+ def register(id, klass)
4
+ scorers[id.to_sym] = klass
5
+ end
6
+
7
+ def []=(id, klass)
8
+ register(id, klass)
9
+ end
10
+
11
+ def [](id)
12
+ scorers[id.to_sym] || raise("No such scoring algorithm")
13
+ end
14
+
15
+ private
16
+ def scorers
17
+ @scorers ||= {}
18
+ end
19
+ end
20
+ end
21
+
22
+ scorers = File.expand_path(File.join(File.dirname(__FILE__), 'scorers', '*.rb'))
23
+ Dir[scorers].each do |scorer|
24
+ load(scorer)
25
+ end
@@ -0,0 +1,76 @@
1
+ class String
2
+ protected
3
+
4
+ # Algorithm from http://github.com/rmm5t/liquidmetal/blob/master/liquidmetal.js
5
+ module LiquidMetal
6
+ StringScorer.register(:liquid_metal, self)
7
+
8
+ class << self
9
+ SCORE_NO_MATCH = 0.0;
10
+ SCORE_MATCH = 1.0;
11
+ SCORE_TRAILING = 0.8;
12
+ SCORE_TRAILING_BUT_STARTED = 0.9;
13
+ SCORE_BUFFER = 0.85;
14
+
15
+ def score(string, abbreviation)
16
+ # Short circuits
17
+ return SCORE_TRAILING if abbreviation.length == 0
18
+ return SCORE_NO_MATCH if abbreviation.length > string.length
19
+ return SCORE_MATCH if abbreviation == string
20
+
21
+ scores = score_array(string, abbreviation);
22
+ scores.inject(0) {|v,m| v + m} / scores.size
23
+ end
24
+
25
+ def score_array(string, abbreviation)
26
+ scores = Array.new(string.length)
27
+ lower = string.downcase
28
+ chars = abbreviation.downcase.split("")
29
+
30
+ last_index = -1
31
+ started = false
32
+ chars.each do |c|
33
+ index = lower.index(c, last_index + 1)
34
+
35
+ return fill_array(scores, SCORE_NO_MATCH) if index.nil?
36
+ started = true if index == 0
37
+
38
+ if new_word?(string, index)
39
+ scores[index-1] = 1;
40
+ fill_array(scores, SCORE_BUFFER, last_index + 1, index - 1)
41
+ elsif uppercase?(string, index)
42
+ fill_array(scores, SCORE_BUFFER, last_index + 1, index)
43
+ else
44
+ fill_array(scores, SCORE_NO_MATCH, last_index + 1, index)
45
+ end
46
+
47
+ scores[index] = SCORE_MATCH
48
+ last_index = index
49
+ end
50
+
51
+ trailing_score = (started ? SCORE_TRAILING_BUT_STARTED : SCORE_TRAILING)
52
+ fill_array(scores, trailing_score, last_index + 1)
53
+ scores
54
+ end
55
+
56
+ def new_word?(string, index)
57
+ c = string[index - 1]
58
+ (index == 0 || c==32 || c == 9)
59
+ end
60
+
61
+ def uppercase?(string, index)
62
+ c = string[index]
63
+ (65..90).include?(c)
64
+ end
65
+
66
+ def fill_array(array, value, from=0, to=array.size)
67
+ from = 0 if from < 0
68
+ to = 0 if to < 0
69
+ size = 1 + (to - from)
70
+
71
+ array[from..to] = Array.new(size, value)
72
+ array
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,58 @@
1
+ class String
2
+ protected
3
+
4
+ # Algorithm from http://is.gd/CUqC
5
+ module Quicksilver
6
+ StringScorer.register(:quicksilver, self)
7
+
8
+ class << self
9
+ def score(string, abbreviation, offset = 0)
10
+ length = abbreviation.length
11
+
12
+ return 0.9 if length == 0
13
+ return 0.0 if length > string.length
14
+
15
+ abbreviation.length.downto(1) do |i|
16
+ sub = abbreviation[0,i]
17
+ index = string.index(sub)
18
+
19
+ next if index.nil?
20
+ next if index + length > string.length + offset
21
+
22
+ next_string = string[(index+sub.length)..-1]
23
+ next_abbreviation = (i >= abbreviation.length) ? '' : abbreviation[i..-1]
24
+
25
+ remaining_score = score(string, next_abbreviation, offset + index)
26
+
27
+ if remaining_score > 0
28
+ score = string.length - next_string.length
29
+
30
+ if index != 0
31
+ c = string[index-1]
32
+
33
+ if (c==32 || c == 9) # space or tab
34
+ (index-2).downto(0) do |j|
35
+ c = string[j]
36
+ score -= ((c == 32 || c == 9) ? 1 : 0.15)
37
+ end
38
+ elsif c >= 65 && c <= 90 # capital letter
39
+ (index-1).downto(0) do |j|
40
+ c = string[j]
41
+ score -= (c >= 65 && c <= 90 ? 1 : 0.15)
42
+ end
43
+ else
44
+ score -= index
45
+ end
46
+ end
47
+
48
+ score += remaining_score * next_string.length
49
+ score /= string.length
50
+ return score
51
+ end
52
+ end
53
+
54
+ 0.0
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ class String
2
+
3
+ def score(abbreviation, method = :liquid_metal)
4
+ StringScorer[method].score(self, abbreviation)
5
+ end
6
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-string-match-scorer}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bodaniel Jeanes"]
12
+ s.date = %q{2009-12-10}
13
+ s.description = %q{Flex matching short abbreviations against longer strings is a boon in productivity for typists. Applications like Quicksilver, LaunchBar, and Launchy have made this method of keyboard entry a popular one.}
14
+ s.email = %q{me@bjeanes.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/string_scorer.rb",
26
+ "lib/string_scorer/scorer.rb",
27
+ "lib/string_scorer/scorers/liquidmetal.rb",
28
+ "lib/string_scorer/scorers/quicksilver.rb",
29
+ "lib/string_scorer/string_ext.rb",
30
+ "ruby-string-match-scorer.gemspec",
31
+ "test/helper.rb",
32
+ "test/test_ruby-string-match-scorer.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/bjeanes/ruby-string-match-scorer}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Quicksilver-like string match scoring}
39
+ s.test_files = [
40
+ "test/test_ruby-string-match-scorer.rb",
41
+ "test/helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'ruby-string-match-scorer'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRubyStringMatchScorer < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-string-match-scorer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bodaniel Jeanes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-11 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Flex matching short abbreviations against longer strings is a boon in productivity for typists. Applications like Quicksilver, LaunchBar, and Launchy have made this method of keyboard entry a popular one.
26
+ email: me@bjeanes.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - .gitignore
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - VERSION
40
+ - lib/string_scorer.rb
41
+ - lib/string_scorer/scorer.rb
42
+ - lib/string_scorer/scorers/liquidmetal.rb
43
+ - lib/string_scorer/scorers/quicksilver.rb
44
+ - lib/string_scorer/string_ext.rb
45
+ - ruby-string-match-scorer.gemspec
46
+ - test/helper.rb
47
+ - test/test_ruby-string-match-scorer.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/bjeanes/ruby-string-match-scorer
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Quicksilver-like string match scoring
76
+ test_files:
77
+ - test/helper.rb
78
+ - test/test_ruby-string-match-scorer.rb