Skip to content

Commit

Permalink
Use comparison operators rather than predicate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrouch committed May 17, 2020
1 parent bb7ced3 commit 1fc1ca0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Metrics/BlockLength:
- edits.gemspec
- "tasks/**/*.rake"

Style/NumericPredicate:
Enabled: false
Style/StringLiterals:
EnforcedStyle: double_quotes
Layout/AlignParameters:
Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation
Layout/MultilineMethodCallIndentation:
EnforcedStyle: indented
Expand Down
6 changes: 3 additions & 3 deletions lib/edits/damerau_levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def self.distance(seq1, seq2)

rows = seq1.length
cols = seq2.length
return cols if rows.zero?
return rows if cols.zero?
return cols if rows == 0
return rows if cols == 0

# 'infinite' edit distance for padding cost matrix.
# Can be any value > max[rows, cols]
Expand Down Expand Up @@ -78,7 +78,7 @@ def self.distance(seq1, seq2)

matrix[row + 1][col + 1] = cost

last_match_col = col if sub_cost.zero?
last_match_col = col if sub_cost == 0
end

item_history[seq1_item] = row
Expand Down
4 changes: 2 additions & 2 deletions lib/edits/jaro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.similarity(seq1, seq2)
seq2 = seq2.codepoints if seq2.is_a? String

m, t = jaro_matches(seq1, seq2)
return 0.0 if m.zero?
return 0.0 if m == 0

m = m.to_f
((m / seq1.length) + (m / seq2.length) + ((m - t) / m)) / 3
Expand Down Expand Up @@ -78,7 +78,7 @@ def self.jaro_matches(seq1, seq2)
end
end

return [0, 0] if matches.zero?
return [0, 0] if matches == 0

transposes = 0
j = 0
Expand Down
10 changes: 5 additions & 5 deletions lib/edits/levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def self.distance(seq1, seq2)

rows = seq1.length
cols = seq2.length
return cols if rows.zero?
return rows if cols.zero?
return cols if rows == 0
return rows if cols == 0

# Initialize first row of cost matrix.
# The full initial state where cols=3, rows=2 would be:
Expand Down Expand Up @@ -82,8 +82,8 @@ def self.distance_with_max(seq1, seq2, max)

rows = seq1.length
cols = seq2.length
return cols > max ? max : cols if rows.zero?
return rows > max ? max : rows if cols.zero?
return cols > max ? max : cols if rows == 0
return rows > max ? max : rows if cols == 0
return max if (cols - rows) >= max

# array of codepoints outperforms String
Expand All @@ -103,7 +103,7 @@ def self.distance_with_max(seq1, seq2, max)
max_col = row + max
max_col = cols - 1 if max_col > cols - 1

prev_col_cost = min_col.zero? ? row + 1 : inf
prev_col_cost = min_col == 0 ? row + 1 : inf
seq1_item = seq1[row]
diagonal = cols - rows + row

Expand Down
18 changes: 9 additions & 9 deletions lib/edits/restricted_edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def self.distance(seq1, seq2)

rows = seq1.length
cols = seq2.length
return cols if rows.zero?
return rows if cols.zero?
return cols if rows == 0
return rows if cols == 0

# retain previous two rows of cost matrix
lastlast_row = []
Expand All @@ -57,8 +57,8 @@ def self.distance(seq1, seq2)

cols.times do |col|
sub_cost = seq1_item == seq2[col] ? 0 : 1
is_swap = sub_cost.positive? &&
row.positive? && col.positive? &&
is_swap = sub_cost > 0 &&
row > 0 && col > 0 &&
seq1_item == seq2[col - 1] &&
seq1[row - 1] == seq2[col]

Expand Down Expand Up @@ -102,8 +102,8 @@ def self.distance_with_max(seq1, seq2, max)

rows = seq1.length
cols = seq2.length
return cols > max ? max : cols if rows.zero?
return rows > max ? max : rows if cols.zero?
return cols > max ? max : cols if rows == 0
return rows > max ? max : rows if cols == 0
return max if (cols - rows) >= max

# array of codepoints outperforms String
Expand All @@ -129,16 +129,16 @@ def self.distance_with_max(seq1, seq2, max)
max_col = row + max
max_col = cols - 1 if max_col > cols - 1

curr_row[min_col] = min_col.zero? ? row + 1 : inf
curr_row[min_col] = min_col == 0 ? row + 1 : inf
seq1_item = seq1[row]
diagonal = cols - rows + row

min_col.upto(max_col) do |col|
return max if diagonal == col && last_row[col] >= max

sub_cost = seq1_item == seq2[col] ? 0 : 1
is_swap = sub_cost.positive? &&
row.positive? && col.positive? &&
is_swap = sub_cost > 0 &&
row > 0 && col > 0 &&
seq1_item == seq2[col - 1] &&
seq1[row - 1] == seq2[col]

Expand Down

0 comments on commit 1fc1ca0

Please sign in to comment.