Skip to content

Commit 8363d0e

Browse files
committed
2 parents be0a55f + 57ffabd commit 8363d0e

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ private static double dotProduct(
113113

114114
double agg = 0;
115115
for (Map.Entry<String, Integer> entry : small_profile.entrySet()) {
116-
if (!large_profile.containsKey(entry.getKey())) {
116+
Integer i=large_profile.get(entry.getKey());
117+
if (i==null) {
117118
continue;
118119
}
119-
agg += 1.0 * entry.getValue() * large_profile.get(entry.getKey());
120+
agg += 1.0 * entry.getValue() * i;
120121
}
121122

122123
return agg;

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ public final double distance(final String s1, final String s2) {
6262
HashMap<Character, Integer> da = new HashMap<Character, Integer>();
6363

6464
for (int d = 0; d < s1.length(); d++) {
65-
if (!da.containsKey(s1.charAt(d))) {
66-
da.put(s1.charAt(d), 0);
67-
}
65+
da.put(s1.charAt(d), 0);
6866
}
6967

7068
for (int d = 0; d < s2.length(); d++) {
71-
if (!da.containsKey(s2.charAt(d))) {
72-
da.put(s2.charAt(d), 0);
73-
}
69+
da.put(s2.charAt(d), 0);
7470
}
7571

7672
// Create the distance matrix H[0 .. s1.length+1][0 .. s2.length+1]

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ public final double distance(final String s1, final String s2) {
6767
for (String key : union) {
6868
int v1 = 0;
6969
int v2 = 0;
70-
if (profile1.containsKey(key)) {
71-
v1 = profile1.get(key);
70+
Integer iv1 = profile1.get(key);
71+
if (iv1!=null){
72+
v1=iv1.intValue();
7273
}
7374

74-
75-
if (profile2.containsKey(key)) {
76-
v2 = profile2.get(key);
75+
Integer iv2=profile2.get(key);
76+
if (iv2!=null) {
77+
v2 = iv2.intValue();
7778
}
7879
agg += Math.abs(v1 - v2);
7980
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,11 @@ public final Map<String, Integer> getProfile(final String string) {
106106
String string_no_space = SPACE_REG.matcher(string).replaceAll(" ");
107107
for (int i = 0; i < (string_no_space.length() - k + 1); i++) {
108108
String shingle = string_no_space.substring(i, i + k);
109-
110-
if (shingles.containsKey(shingle)) {
111-
shingles.put(shingle, shingles.get(shingle) + 1);
112-
109+
Integer old = shingles.get(shingle);
110+
if (old!=null) {
111+
shingles.put(shingle, old + 1);
113112
} else {
114113
shingles.put(shingle, 1);
115-
116114
}
117115
}
118116

0 commit comments

Comments
 (0)