scorerb 0.0.1 → 0.0.2

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 CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  tmp/
6
+ tmp/*
@@ -1,62 +1,68 @@
1
1
  module Scorerb
2
- class ::String
3
- def score(abrv, fuzzines = 0)
2
+
3
+ def index_anycase(match)
4
+ self.index(match.upcase) || self.index(match.downcase)
5
+ end
4
6
 
5
- # If strings are equal return exact one
6
- return 1.0 if self == abrv
7
+
8
+ def score(abrv, fuzzines = 0)
9
+
10
+ # If strings are equal return exact one
11
+ return 1.0 if self == abrv
7
12
 
8
- total_char_score = 0
9
- fuzzies = 1
10
- bonus_starting_string = false
11
- abrv_size = abrv.size
12
- word_size = self.size
13
- word = self
14
- final_score = 0
15
-
16
- (0..abrv_size-1).each do |i|
17
- char = abrv[i]
18
- index_char_upper = word.index(char.upcase) || -1
19
- index_char_lower = word.index(char.downcase) || -1
20
- min_index = [index_char_upper, index_char_lower].min
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]
21
23
 
22
- index_in_string = min_index > -1 ? min_index : [index_char_upper, index_char_lower].max
24
+ index_in_string = word.index_anycase(char)
23
25
 
24
- if index_in_string == -1
25
- if fuzzines > 0
26
- fuzzies += 1 - fuzzines
27
- next
28
- else
29
- return 0
30
- end
31
- else
32
- char_score = 0.1
33
- end
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
34
36
 
35
37
 
36
- char_score += 0.1 if word[index_in_string] == char
38
+ char_score += 0.1 if word[index_in_string] == char
37
39
 
38
- if index_in_string == 0
39
- char_score += 0.6
40
- bonus_starting_string = i == 0
41
- else
42
- char_score += 0.8 if word[index_in_string - 1] == " "
43
- end
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
44
46
 
45
- word = word.slice index_in_string + 1, word.size
47
+ word = word.slice index_in_string + 1, word.size
46
48
 
47
- total_char_score += char_score
48
- end
49
+ total_char_score += char_score
50
+ end
49
51
 
50
- abrv_score = total_char_score / abrv_size
52
+ abrv_score = total_char_score / abrv_size
51
53
 
52
- #Reduce penalty for longer words
53
- final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2
54
+ #Reduce penalty for longer words
55
+ final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2
54
56
 
55
- #Reduce using fuzzies
56
- final_score = final_score / fuzzies
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
63
+
64
+ end
57
65
 
58
- final_score += 0.15 if bonus_starting_string && final_score <= 0.85
59
- final_score
60
- end
61
- end
66
+ class String
67
+ include Scorerb
62
68
  end
@@ -1,3 +1,3 @@
1
1
  module Scorerb
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -32,4 +32,18 @@ describe Scorerb do
32
32
  it "should drop score with lower fuzzines" do
33
33
  "Hi there".score("Hi thi", 0.1).should be < "Hi there".score("Hi thi", 0.8)
34
34
  end
35
+
36
+ describe "finding first match unconsidering letters case" do
37
+ it "should find the first match given a lowercase" do
38
+ "Abc".index_anycase("a").should be == 0
39
+ end
40
+ it "should find the first match given a uppercase" do
41
+ "Abc".index_anycase("A").should be == 0
42
+ end
43
+
44
+ it "should return nothing if no matches" do
45
+ "Abc".index_anycase("Z").should be == nil
46
+ end
47
+ end
48
+
35
49
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: scorerb
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Bruno Tavares
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-17 00:00:00 -03:00
13
+ date: 2011-04-09 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency