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 +1 -0
- data/lib/scorerb.rb +53 -47
- data/lib/scorerb/version.rb +1 -1
- data/spec/scorerb/scorerb_spec.rb +14 -0
- metadata +2 -2
data/.gitignore
CHANGED
data/lib/scorerb.rb
CHANGED
|
@@ -1,62 +1,68 @@
|
|
|
1
1
|
module Scorerb
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
def index_anycase(match)
|
|
4
|
+
self.index(match.upcase) || self.index(match.downcase)
|
|
5
|
+
end
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
+
index_in_string = word.index_anycase(char)
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
38
|
+
char_score += 0.1 if word[index_in_string] == char
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
47
|
+
word = word.slice index_in_string + 1, word.size
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
total_char_score += char_score
|
|
50
|
+
end
|
|
49
51
|
|
|
50
|
-
|
|
52
|
+
abrv_score = total_char_score / abrv_size
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
#Reduce penalty for longer words
|
|
55
|
+
final_score = ((abrv_score * (abrv_size/word_size)) + abrv_score) / 2
|
|
54
56
|
|
|
55
|
-
|
|
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
|
|
63
|
+
|
|
64
|
+
end
|
|
57
65
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
end
|
|
61
|
-
end
|
|
66
|
+
class String
|
|
67
|
+
include Scorerb
|
|
62
68
|
end
|
data/lib/scorerb/version.rb
CHANGED
|
@@ -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.
|
|
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-
|
|
13
|
+
date: 2011-04-09 00:00:00 -03:00
|
|
14
14
|
default_executable:
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|