scorerb 0.0.3a → 0.1.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/README.md CHANGED
@@ -26,8 +26,18 @@ It's simple:
26
26
  > "Abc".score "A" # without fuzzines
27
27
  >
28
28
  > "Abc".score "azb" 0.2 # with fuzzines (0..1)
29
+ >
30
+ > ["abc","AB","a"].score "A"
29
31
  >
30
32
 
33
+ ## Options
34
+
35
+ ### String
36
+ "Hello".score "He", fuzzines = 0
37
+
38
+ ### Array
39
+ [1,2,3].score "1", :fuzz => 0, :to_s => false
40
+
31
41
  ## Credits
32
42
 
33
43
  It's based on the [string_score](https://github.com/joshaven/string_score) from [Joshaven Potter](https://github.com/joshaven/).
data/lib/scorerb.rb CHANGED
@@ -1,68 +1,10 @@
1
- module Scorerb
2
-
3
- def index_anycase(match)
4
- self.index(match.upcase) || self.index(match.downcase)
5
- end
6
-
7
-
8
- def score(abrv, fuzzines = 0)
9
-
10
- # If strings are equal return exact one
11
- return 1.0 if self == abrv
12
-
13
- total_char_score = 0
14
- fuzzies = 1
15
- bonus_starting_string = false
16
- abrv_size = abrv.size
17
- word_size = self.size
18
- word = self
19
- final_score = 0
20
-
21
- (0..abrv_size-1).each do |i|
22
- char = abrv[i]
23
-
24
- index_in_string = word.index_anycase(char)
25
-
26
- if index_in_string.nil?
27
- if fuzzines > 0
28
- fuzzies += 1 - fuzzines
29
- next
30
- else
31
- return 0
32
- end
33
- else
34
- char_score = 0.1
35
- end
36
-
37
-
38
- char_score += 0.1 if word[index_in_string] == char
39
-
40
- if index_in_string == 0
41
- char_score += 0.6
42
- bonus_starting_string = i == 0
43
- else
44
- char_score += 0.8 if word[index_in_string - 1] == " "
45
- end
46
-
47
- word = word.slice index_in_string + 1, word.size
48
-
49
- total_char_score += char_score
50
- end
51
-
52
- abrv_score = total_char_score / abrv_size
53
-
54
- #Reduce penalty for longer words
55
- final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2
56
-
57
- #Reduce using fuzzies
58
- final_score = final_score / fuzzies
59
-
60
- final_score += 0.15 if bonus_starting_string && final_score <= 0.85
61
- final_score
62
- end
1
+ require "scorerb/string"
2
+ require "scorerb/array"
63
3
 
4
+ class String
5
+ include Scorerb::String
64
6
  end
65
7
 
66
- class String
67
- include Scorerb
8
+ class Array
9
+ include Scorerb::Array
68
10
  end
@@ -0,0 +1,18 @@
1
+ module Scorerb
2
+ module Array
3
+ OPTIONS = {:fuzz => 0, :to_S => false}
4
+
5
+ def score(string, options = {})
6
+ return nil if empty?
7
+ optional = OPTIONS.merge(options)
8
+ matches = {}
9
+ each do |against|
10
+ against = against.to_s if optional[:to_s]
11
+ current_score = { against => string.score(against, optional[:fuzz]) }
12
+ matches.update(current_score)
13
+ end
14
+ matches
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ module Scorerb
2
+ module String
3
+ def index_anycase(match)
4
+ self.index(match.upcase) || self.index(match.downcase)
5
+ end
6
+
7
+
8
+ def score(abrv, fuzzines = 0)
9
+
10
+ # If strings are equal return exact one
11
+ return 1.0 if self == abrv
12
+
13
+ total_char_score = 0
14
+ fuzzies = 1
15
+ bonus_starting_string = false
16
+ abrv_size = abrv.size
17
+ word_size = self.size
18
+ word = self
19
+ final_score = 0
20
+
21
+ (0..abrv_size-1).each do |i|
22
+ char = abrv[i]
23
+
24
+ index_in_string = word.index_anycase(char)
25
+
26
+ if index_in_string.nil?
27
+ if fuzzines > 0
28
+ fuzzies += 1 - fuzzines
29
+ next
30
+ else
31
+ return 0
32
+ end
33
+ else
34
+ char_score = 0.1
35
+ end
36
+
37
+ #The current char is in the same position?
38
+ char_score += 0.1 if word[index_in_string] == char
39
+
40
+ #If starting the word
41
+ if index_in_string == 0
42
+ char_score += 0.6
43
+ bonus_starting_string = i == 0
44
+ else
45
+ char_score += 0.8 if word[index_in_string - 1] == " "
46
+ end
47
+
48
+ word = word.slice index_in_string + 1, word.size
49
+
50
+ total_char_score += char_score
51
+ end
52
+
53
+ abrv_score = total_char_score / abrv_size
54
+
55
+ #Reduce penalty for longer words
56
+ final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2
57
+
58
+ #Reduce using fuzzies
59
+ final_score = final_score / fuzzies
60
+
61
+ final_score += 0.15 if bonus_starting_string && final_score <= 0.85
62
+ final_score
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module Scorerb
2
- VERSION = "0.0.3a"
2
+ VERSION = "0.1.0"
3
3
  end
data/scorerb.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Bruno Tavares"]
10
10
  s.email = ["bruno.exz@gmail.com"]
11
11
  s.homepage = ""
12
- s.summary = %q{Simple fuzzy search for your strings}
13
- s.description = %q{With that gem you have the ability to check one string against other and get the score}
12
+ s.summary = %q{Simple fuzzy search for your strings and arrays}
13
+ s.description = %q{With that gem you have the ability to check one string against other, string against an array and get the score for the matches}
14
14
 
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Scorerb::Array do
4
+
5
+ it "should add score method to arrays" do
6
+ [].should respond_to :score
7
+ end
8
+
9
+ it "should return an hash for score arrays" do
10
+ result = { "Hello" => 1, "Hel" => 0.4000000000000001, "Hi" => 0 }
11
+ ["Hello", "Hel", "Hi"].score("Hello").should be == result
12
+ end
13
+
14
+ it "should accept fuzzines" do
15
+ result = { "Hello" => 1, "Hel" => 0.4000000000000001, "Hi" => 0.2833333333333333 }
16
+ ["Hello", "Hel", "Hi"].score("Hello", :fuzz => 0.5 ).should be == result
17
+ end
18
+
19
+ it "should return nil for empty arrays" do
20
+ [].score("Hello").should be == nil
21
+ end
22
+
23
+ it "should accept option to convert everything to string" do
24
+ result = {"1" => 1 , "2" => 0, "3" => 0}
25
+ [1,2,3].score("1", :to_s => true).should be == result
26
+ end
27
+
28
+ it "should accept all the options together" do
29
+ result = {"1" => 1 , "12" => 0.55, "113" => 0.41666666666666663 }
30
+ [1,12,113].score("1", :to_s => true, :fuzz => 0.5 ).should be == result
31
+ end
32
+ end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Scorerb do
3
+ describe Scorerb::String do
4
4
  it "should add score method to strings" do
5
5
  "".should respond_to :score
6
6
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scorerb
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: 5
5
- version: 0.0.3a
4
+ prerelease:
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bruno Tavares
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-09 00:00:00 -03:00
14
- default_executable:
13
+ date: 2011-05-05 00:00:00 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: rspec
@@ -24,7 +23,7 @@ dependencies:
24
23
  version: 2.5.0
25
24
  type: :development
26
25
  version_requirements: *id001
27
- description: With that gem you have the ability to check one string against other and get the score
26
+ description: With that gem you have the ability to check one string against other, string against an array and get the score for the matches
28
27
  email:
29
28
  - bruno.exz@gmail.com
30
29
  executables: []
@@ -40,11 +39,13 @@ files:
40
39
  - README.md
41
40
  - Rakefile
42
41
  - lib/scorerb.rb
42
+ - lib/scorerb/array.rb
43
+ - lib/scorerb/string.rb
43
44
  - lib/scorerb/version.rb
44
45
  - scorerb.gemspec
45
- - spec/scorerb/scorerb_spec.rb
46
+ - spec/scorerb/scorerb_array_spec.rb
47
+ - spec/scorerb/scorerb_string_spec.rb
46
48
  - spec/spec_helper.rb
47
- has_rdoc: true
48
49
  homepage: ""
49
50
  licenses: []
50
51
 
@@ -62,16 +63,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
63
  required_rubygems_version: !ruby/object:Gem::Requirement
63
64
  none: false
64
65
  requirements:
65
- - - ">"
66
+ - - ">="
66
67
  - !ruby/object:Gem::Version
67
- version: 1.3.1
68
+ version: "0"
68
69
  requirements: []
69
70
 
70
71
  rubyforge_project:
71
- rubygems_version: 1.6.2
72
+ rubygems_version: 1.7.2
72
73
  signing_key:
73
74
  specification_version: 3
74
- summary: Simple fuzzy search for your strings
75
+ summary: Simple fuzzy search for your strings and arrays
75
76
  test_files:
76
- - spec/scorerb/scorerb_spec.rb
77
+ - spec/scorerb/scorerb_array_spec.rb
78
+ - spec/scorerb/scorerb_string_spec.rb
77
79
  - spec/spec_helper.rb
80
+ has_rdoc: