fuzzy_string 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +10 -7
- data/lib/fuzzy_string/adjusted_score.rb +15 -12
- data/lib/fuzzy_string/version.rb +1 -1
- data/test/fuzzy_string_test.rb +3 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5d68f53a424798e82ac70ebee25bcf4dcf53c8d
|
4
|
+
data.tar.gz: 8fea39a1b1b91cced44e8cb9989f654e71b22322
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9b072391bbf522beacbded105d568abe1769a2dbb25b7bcebb6162f91bf4b07be4ec22f1294c1c484b9fce2716f5a072e7ec7305b98980137252a26f44b9461
|
7
|
+
data.tar.gz: 0a40442e1950284d8751415df9277c25d6b339c285f1fb0b3407fe226b9c9bbdbebfe7e01df550f985d446cbe8769d1facb741dd89fe7a6bc3b7371efb372cdf
|
data/Gemfile.lock
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
fuzzy_string (
|
4
|
+
fuzzy_string (1.1.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
9
|
ansi (1.5.0)
|
10
|
-
awesome_print (1.
|
11
|
-
builder (3.2.
|
12
|
-
minitest (5.
|
13
|
-
minitest-reporters (1.
|
10
|
+
awesome_print (1.7.0)
|
11
|
+
builder (3.2.3)
|
12
|
+
minitest (5.10.1)
|
13
|
+
minitest-reporters (1.1.14)
|
14
14
|
ansi
|
15
15
|
builder
|
16
16
|
minitest (>= 5.0)
|
17
17
|
ruby-progressbar
|
18
|
-
rake (10.
|
19
|
-
ruby-progressbar (1.
|
18
|
+
rake (10.5.0)
|
19
|
+
ruby-progressbar (1.8.1)
|
20
20
|
|
21
21
|
PLATFORMS
|
22
22
|
ruby
|
@@ -28,3 +28,6 @@ DEPENDENCIES
|
|
28
28
|
minitest
|
29
29
|
minitest-reporters (>= 1.0.1)
|
30
30
|
rake (~> 10.0)
|
31
|
+
|
32
|
+
BUNDLED WITH
|
33
|
+
1.14.6
|
@@ -1,24 +1,27 @@
|
|
1
1
|
class FuzzyString::AdjustedScore
|
2
2
|
def self.rank(first,second) new(first,second).rank end
|
3
3
|
def initialize(first,second)
|
4
|
-
@
|
5
|
-
@
|
4
|
+
@long = first.length > second.length ? first : second
|
5
|
+
@short = first.length > second.length ? second : first
|
6
6
|
end
|
7
7
|
def rank
|
8
|
-
return 0
|
9
|
-
return @
|
10
|
-
return @
|
8
|
+
return 0 if (@long == @short)
|
9
|
+
return @short.length if (@long.length == 0)
|
10
|
+
return @long.length if (@short.length == 0)
|
11
11
|
adjusted_levenschtein_distance
|
12
12
|
end
|
13
13
|
def adjusted_levenschtein_distance
|
14
14
|
special_chars = [")", "(", "]", "[", "}", "{", ".", "?", "+", "*"]
|
15
|
-
regex = /#{@
|
16
|
-
pieces = @
|
17
|
-
score
|
18
|
-
score
|
19
|
-
|
20
|
-
score
|
21
|
-
score
|
15
|
+
regex = /#{@short.chars.to_a.map{|el| special_chars.include?(el) ? "\\"+el : el}.join('(.*?)')}/i
|
16
|
+
pieces = @long.split(regex)
|
17
|
+
score = FuzzyString::Levenshtein.distance(@short,@long)
|
18
|
+
score *= pieces[0..@short.length - 1].uniq == [''] ? 0.75 : 1
|
19
|
+
letter_ratio = score.to_f / @long.length
|
20
|
+
score -= pieces[0][-1] == ' ' ? letter_ratio * 0.75 : 0
|
21
|
+
score -= (pieces[@short.length] || [])[0] == ' ' ? letter_ratio * @short.length * 0.75 : 0
|
22
|
+
score -= letter_ratio * cost(pieces.shift,0.5)
|
23
|
+
score -= letter_ratio * cost(pieces.pop, 0.9)
|
24
|
+
score -= letter_ratio * cost(pieces.join, 0.1)
|
22
25
|
end
|
23
26
|
private
|
24
27
|
def cost(piece,multiplier) piece.length * multiplier rescue(0) end
|
data/lib/fuzzy_string/version.rb
CHANGED
data/test/fuzzy_string_test.rb
CHANGED
@@ -39,5 +39,7 @@ class FuzzyStringTest < MiniTest::Unit::TestCase
|
|
39
39
|
def test_prioritizes_string_starting_with_match
|
40
40
|
assert 'escreens are' ^ 'escreen' < 'I am a n escreen' ^ 'escreen'
|
41
41
|
end
|
42
|
-
|
42
|
+
def test_prioritizes_exact_sub_strings
|
43
|
+
assert 'ACME' ^ 'ACME Utility' < 'ACME' ^ 'ACT Me 2'
|
44
|
+
end
|
43
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fuzzy_string
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Moody
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.10
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Relative ranking system for strings.
|