Skip to content

Commit 02aa51e

Browse files
committed
Checked, marked as Immutable and code clean
1 parent edd77a4 commit 02aa51e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/main/java/info/debatty/java/stringsimilarity/WeightedLevenshtein.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,34 @@
2424
package info.debatty.java.stringsimilarity;
2525

2626
import info.debatty.java.stringsimilarity.interfaces.StringDistance;
27+
import net.jcip.annotations.Immutable;
2728

2829
/**
2930
* Implementation of Levenshtein that allows to define different weights for
3031
* different character substitutions.
3132
*
3233
* @author Thibault Debatty
3334
*/
35+
@Immutable
3436
public class WeightedLevenshtein implements StringDistance {
3537

3638
private final CharacterSubstitutionInterface charsub;
3739

38-
public WeightedLevenshtein(CharacterSubstitutionInterface charsub) {
40+
/**
41+
* Instatiate with provided character substitution.
42+
* @param charsub
43+
*/
44+
public WeightedLevenshtein(final CharacterSubstitutionInterface charsub) {
3945
this.charsub = charsub;
4046
}
4147

42-
public double distance(String s1, String s2) {
48+
/**
49+
* Compute Levenshtein distance using provided weights for substitution.
50+
* @param s1
51+
* @param s2
52+
* @return
53+
*/
54+
public final double distance(final String s1, final String s2) {
4355
if (s1.equals(s2)) {
4456
return 0;
4557
}
@@ -72,7 +84,10 @@ public double distance(String s1, String s2) {
7284

7385
// use formula to fill in the rest of the row
7486
for (int j = 0; j < s2.length(); j++) {
75-
double cost = (s1.charAt(i) == s2.charAt(j)) ? 0 : charsub.cost(s1.charAt(i), s2.charAt(j));
87+
double cost = 0;
88+
if (s1.charAt(i) != s2.charAt(j)) {
89+
cost = charsub.cost(s1.charAt(i), s2.charAt(j));
90+
}
7691
v1[j + 1] = Math.min(
7792
v1[j] + 1, // Cost of insertion
7893
Math.min(

0 commit comments

Comments
 (0)