File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed
src/main/java/info/debatty/java/stringsimilarity Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 24
24
package info .debatty .java .stringsimilarity ;
25
25
26
26
import info .debatty .java .stringsimilarity .interfaces .StringDistance ;
27
+ import net .jcip .annotations .Immutable ;
27
28
28
29
/**
29
30
* Implementation of Levenshtein that allows to define different weights for
30
31
* different character substitutions.
31
32
*
32
33
* @author Thibault Debatty
33
34
*/
35
+ @ Immutable
34
36
public class WeightedLevenshtein implements StringDistance {
35
37
36
38
private final CharacterSubstitutionInterface charsub ;
37
39
38
- public WeightedLevenshtein (CharacterSubstitutionInterface charsub ) {
40
+ /**
41
+ * Instatiate with provided character substitution.
42
+ * @param charsub
43
+ */
44
+ public WeightedLevenshtein (final CharacterSubstitutionInterface charsub ) {
39
45
this .charsub = charsub ;
40
46
}
41
47
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 ) {
43
55
if (s1 .equals (s2 )) {
44
56
return 0 ;
45
57
}
@@ -72,7 +84,10 @@ public double distance(String s1, String s2) {
72
84
73
85
// use formula to fill in the rest of the row
74
86
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
+ }
76
91
v1 [j + 1 ] = Math .min (
77
92
v1 [j ] + 1 , // Cost of insertion
78
93
Math .min (
You can’t perform that action at this time.
0 commit comments