|
| 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 | +} |
0 commit comments