Skip to content

Commit

Permalink
style: normalize coding styles with rubocop
Browse files Browse the repository at this point in the history
rubocop -a lib
  • Loading branch information
tonytonyjan committed Sep 30, 2017
1 parent 8babd4f commit 3864897
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
3 changes: 2 additions & 1 deletion lib/jaro_winkler.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'jaro_winkler/version'

case RUBY_PLATFORM
Expand All @@ -6,4 +8,3 @@
else
require 'jaro_winkler/jaro_winkler_ext'
end

16 changes: 9 additions & 7 deletions lib/jaro_winkler/adjusting_table.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

module JaroWinkler
DEFAULT_ADJ_TABLE = Hash.new{|h,k| h[k] = Hash.new(&h.default_proc)}
DEFAULT_ADJ_TABLE = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
[
['A', 'E'], ['A', 'I'], ['A', 'O'], ['A', 'U'], ['B', 'V'], ['E', 'I'], ['E', 'O'], ['E', 'U'], ['I', 'O'],
['I', 'U'], ['O', 'U'], ['I', 'Y'], ['E', 'Y'], ['C', 'G'], ['E', 'F'], ['W', 'U'], ['W', 'V'], ['X', 'K'],
['S', 'Z'], ['X', 'S'], ['Q', 'C'], ['U', 'V'], ['M', 'N'], ['L', 'I'], ['Q', 'O'], ['P', 'R'], ['I', 'J'],
['2', 'Z'], ['5', 'S'], ['8', 'B'], ['1', 'I'], ['1', 'L'], ['0', 'O'], ['0', 'Q'], ['C', 'K'], ['G', 'J'],
%w[A E], %w[A I], %w[A O], %w[A U], %w[B V], %w[E I], %w[E O], %w[E U], %w[I O],
%w[I U], %w[O U], %w[I Y], %w[E Y], %w[C G], %w[E F], %w[W U], %w[W V], %w[X K],
%w[S Z], %w[X S], %w[Q C], %w[U V], %w[M N], %w[L I], %w[Q O], %w[P R], %w[I J],
%w[2 Z], %w[5 S], %w[8 B], %w[1 I], %w[1 L], %w[0 O], %w[0 Q], %w[C K], %w[G J],
['E', ' '], ['Y', ' '], ['S', ' ']
].each { |s1, s2|
].each do |s1, s2|
DEFAULT_ADJ_TABLE[s1][s2] = DEFAULT_ADJ_TABLE[s2][s1] = true
}
end
end
57 changes: 30 additions & 27 deletions lib/jaro_winkler/jaro_winkler_pure.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'jaro_winkler/adjusting_table'
module JaroWinkler
class Error < RuntimeError; end
Expand All @@ -6,62 +8,63 @@ class InvalidWeightError < Error; end
DEFAULT_WEIGHT = 0.1
DEFAULT_THRESHOLD = 0.7
DEFAULT_OPTIONS = {
jaro: {adj_table: false, ignore_case: false},
jaro_winkler: {weight: DEFAULT_WEIGHT, threshold: DEFAULT_THRESHOLD}
}
jaro: { adj_table: false, ignore_case: false },
jaro_winkler: { weight: DEFAULT_WEIGHT, threshold: DEFAULT_THRESHOLD }
}.freeze

class << self
def distance str1, str2, options={}
def distance(str1, str2, options = {})
_distance str1.codepoints.to_a, str2.codepoints.to_a, options
end

def jaro_distance str1, str2, options={}
def jaro_distance(str1, str2, options = {})
_jaro_distance str1.codepoints.to_a, str2.codepoints.to_a, options
end

private

def _distance codes1, codes2, options={}
def _distance(codes1, codes2, options = {})
options = DEFAULT_OPTIONS[:jaro_winkler].merge options
raise InvalidWeightError if options[:weight] > 0.25
jaro_distance = _jaro_distance(codes1, codes2, options);
jaro_distance = _jaro_distance(codes1, codes2, options)

if jaro_distance < options[:threshold]
jaro_distance
else
codes1, codes2 = codes2, codes1 if codes1.length > codes2.length
len1, len2 = codes1.length, codes2.length
len1 = codes1.length
len2 = codes2.length
max_4 = len1 > 4 ? 4 : len1
prefix = 0
while prefix < max_4 && codes1[prefix] == codes2[prefix]
prefix += 1
end
prefix += 1 while prefix < max_4 && codes1[prefix] == codes2[prefix]
jaro_distance + prefix * options[:weight] * (1 - jaro_distance)
end
end

def _jaro_distance codes1, codes2, options={}
def _jaro_distance(codes1, codes2, options = {})
options = DEFAULT_OPTIONS[:jaro].merge options

codes1, codes2 = codes2, codes1 if codes1.length > codes2.length
len1, len2 = codes1.length, codes2.length
len1 = codes1.length
len2 = codes2.length
return 0.0 if len1 == 0 || len2 == 0

if options[:ignore_case]
codes1.map!{ |c| c >= 97 && c <= 122 ? c -= 32 : c }
codes2.map!{ |c| c >= 97 && c <= 122 ? c -= 32 : c }
codes1.map! { |c| c >= 97 && c <= 122 ? c -= 32 : c }
codes2.map! { |c| c >= 97 && c <= 122 ? c -= 32 : c }
end

window = len2/2 - 1
window = 0 if(window < 0)
flags1, flags2 = 0, 0
window = len2 / 2 - 1
window = 0 if window < 0
flags1 = 0
flags2 = 0

# // count number of matching characters
match_count = 0;
match_count = 0
i = 0
while i < len1
left = (i >= window) ? i - window : 0
right = (i + window <= len2 - 1) ? (i + window) : (len2 - 1)
left = i >= window ? i - window : 0
right = i + window <= len2 - 1 ? (i + window) : (len2 - 1)
right = len2 - 1 if right > len2 - 1
j = left
while j <= right
Expand All @@ -71,7 +74,7 @@ def _jaro_distance codes1, codes2, options={}
match_count += 1
break
end
j +=1
j += 1
end
i += 1
end
Expand All @@ -86,8 +89,8 @@ def _jaro_distance codes1, codes2, options={}
j = k
while j < len2
if flags2[j] == 1
k = j + 1;
break;
k = j + 1
break
end
j += 1
end
Expand Down Expand Up @@ -118,9 +121,9 @@ def _jaro_distance codes1, codes2, options={}
end

m = match_count.to_f
t = transposition_count/2
m = similar_count/10.0 + m if options[:adj_table]
(m/len1 + m/len2 + (m-t)/m) / 3
t = transposition_count / 2
m = similar_count / 10.0 + m if options[:adj_table]
(m / len1 + m / len2 + (m - t) / m) / 3
end
end
end
2 changes: 2 additions & 0 deletions lib/jaro_winkler/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JaroWinkler
VERSION = '1.4.0'
end

0 comments on commit 3864897

Please sign in to comment.