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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd20e09ff3e7e38a255c27b9780e08f0c2022e96
4
- data.tar.gz: b15e19a162affbf1b33c3b2d70488d938fcdc35b
3
+ metadata.gz: a5d68f53a424798e82ac70ebee25bcf4dcf53c8d
4
+ data.tar.gz: 8fea39a1b1b91cced44e8cb9989f654e71b22322
5
5
  SHA512:
6
- metadata.gz: a87e478b2ca14088b9b6878097f30e843a24e24edb2a30766774b6e60e9342b2ecdf3a6faa816a22d1065f4af9b0c1b8349a311bcafe0a8dd951ff6249a7300f
7
- data.tar.gz: 32fb2af62beb4459061dca962cc40c437642a1b838c7e30dfc63e1daa908004786c6e06df5981426f2470528acc74f1b4d7e6eb2861e8ec16a69b49eee15de93
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 (0.0.2)
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.6.1)
11
- builder (3.2.2)
12
- minitest (5.6.1)
13
- minitest-reporters (1.0.14)
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.4.2)
19
- ruby-progressbar (1.7.5)
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
- @first = first.length < second.length ? first : second
5
- @second = first.length < second.length ? second : first
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 if (@first == @second)
9
- return @second.length if (@first.length == 0)
10
- return @first.length if (@second.length == 0)
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 = /#{@second.chars.to_a.map{|el| special_chars.includes?(el) ? "\\"+el : el}.join('(.*?)')}/i
16
- pieces = @first.split(regex)
17
- score = pieces[0][-1] == ' ' ? -1 : 0
18
- score += pieces[0..@second.length - 1].uniq == [''] ? -@first.length.to_f / 2 : 0
19
- score += (pieces[@second.length] || [])[0] == ' ' ? -@first.length.to_f / 2 : 0
20
- score += cost(pieces.shift,0.5) + cost(pieces.pop,0.25) + cost(pieces.join,1)
21
- score += FuzzyString::Levenshtein.distance(@first,@second)
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
@@ -1,3 +1,3 @@
1
1
  module FuzzyString
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -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.0.0
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-06 00:00:00.000000000 Z
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.4.3
125
+ rubygems_version: 2.6.10
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: Relative ranking system for strings.