Skip to content

Commit c3fc727

Browse files
committed
Initial implementation of OptimalStringAlignment, with example
1 parent b1eaf3f commit c3fc727

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2016 Thibault Debatty.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package info.debatty.java.stringsimilarity;
25+
26+
import info.debatty.java.stringsimilarity.interfaces.StringDistance;
27+
import net.jcip.annotations.Immutable;
28+
29+
/**
30+
* Implementation of the the Optimal String Alignment (sometimes called the
31+
* restricted edit distance) variant of the Damerau-Levenshtein distance.
32+
*
33+
* The difference between the two algorithms consists in that the Optimal String
34+
* Alignment algorithm computes the number of edit operations needed to make the
35+
* strings equal under the condition that no substring is edited more than once,
36+
* whereas Damerau-Levenshtein presents no such restriction.
37+
*
38+
* @author Michail Bogdanos
39+
*/
40+
@Immutable
41+
public final class OptimalStringAlignment implements StringDistance {
42+
43+
/**
44+
* Compute the distance between strings: the minimum number of operations
45+
* needed to transform one string into the other (insertion, deletion,
46+
* substitution of a single character, or a transposition of two adjacent
47+
* characters) while no substring is edited more than once.
48+
*
49+
* @param s1 the first input string
50+
* @param s2 the second input string
51+
* @return the OSA distance
52+
*/
53+
public final double distance(final String s1, final String s2) {
54+
55+
// Create the distance matrix H[0 .. s1.length+1][0 .. s2.length+1]
56+
int[][] d = new int[s1.length() + 2][s2.length() + 2];
57+
58+
//@TODO: check special cases
59+
//initialize top row and leftmost column
60+
for (int i = 0; i <= s1.length(); i++) {
61+
d[i][0] = i;
62+
}
63+
for (int j = 0; j <= s2.length(); j++) {
64+
d[0][j] = j;
65+
}
66+
67+
//fill the distance matrix
68+
int cost;
69+
70+
for (int i = 1; i <= s1.length(); i++) {
71+
for (int j = 1; j <= s2.length(); j++) {
72+
73+
//if s1[i - 1] = s2[j - 1] then cost = 0, else cost = 1
74+
cost = (s1.charAt(i - 1) == s2.charAt(j - 1)) ? 0 : 1;
75+
76+
d[i][j] = min(
77+
d[i - 1][j - 1] + cost, // substitution
78+
d[i][j - 1] + 1, // insertion
79+
d[i - 1][j] + 1 // deletion
80+
);
81+
82+
//transposition check
83+
if (i > 1 && j > 1 && s1.charAt(i-1) == s2.charAt(j-2) && s1.charAt(i-2) == s2.charAt(j-1)){
84+
d[i][j] = Math.min(d[i][j], d[i-2][j-2] + cost);
85+
}
86+
}
87+
}
88+
89+
return d[s1.length() + 1][s2.length() + 1];
90+
}
91+
92+
private static int min(
93+
final int a, final int b, final int c) {
94+
return Math.min(a, Math.min(b, c));
95+
}
96+
}

src/main/java/info/debatty/java/stringsimilarity/examples/Examples.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import info.debatty.java.stringsimilarity.CharacterSubstitutionInterface;
2727
import info.debatty.java.stringsimilarity.Cosine;
2828
import info.debatty.java.stringsimilarity.Damerau;
29+
import info.debatty.java.stringsimilarity.OptimalStringAlignment;
2930
import info.debatty.java.stringsimilarity.Jaccard;
3031
import info.debatty.java.stringsimilarity.JaroWinkler;
3132
import info.debatty.java.stringsimilarity.KShingling;
@@ -109,6 +110,25 @@ public static void main(String[] args) {
109110

110111
// All different
111112
System.out.println(damerau.distance("ABCDEF", "POIU"));
113+
114+
// OptimalStringAlignment
115+
// =======
116+
OptimalStringAlignment osa = new OptimalStringAlignment();
117+
118+
// 1 substitution
119+
System.out.println(osa.distance("ABCDEF", "ABDCEF"));
120+
121+
// 2 substitutions
122+
System.out.println(osa.distance("ABCDEF", "BACDFE"));
123+
124+
// 1 deletion
125+
System.out.println(osa.distance("ABCDEF", "ABCDE"));
126+
System.out.println(osa.distance("ABCDEF", "BCDEF"));
127+
128+
System.out.println(osa.distance("ABCDEF", "ABCGDEF"));
129+
130+
// All different
131+
System.out.println(osa.distance("ABCDEF", "POIU"));
112132

113133
// Longest Common Subsequence
114134
// ==========================

0 commit comments

Comments
 (0)