-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from denmase/master
Implementation of Ratcliff-Obershelp algorithm
- Loading branch information
Showing
3 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/main/java/info/debatty/java/stringsimilarity/RatcliffObershelp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2015 Thibault Debatty. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package info.debatty.java.stringsimilarity; | ||
|
||
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringSimilarity; | ||
import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
||
import net.jcip.annotations.Immutable; | ||
|
||
/** | ||
* Ratcliff/Obershelp pattern recognition | ||
* The Ratcliff/Obershelp algorithm computes the similarity of two strings a | ||
* the doubled number of matching characters divided by the total number of | ||
* characters in the two strings. Matching characters are those in the longest | ||
* common subsequence plus, recursively, matching characters in the unmatched | ||
* region on either side of the longest common subsequence. | ||
* The Ratcliff/Obershelp distance is computed as 1 - Ratcliff/Obershelp | ||
* similarity. | ||
* | ||
* @author Ligi https://github.com/dxpux (as a patch for fuzzystring) | ||
* Ported to java from .net by denmase | ||
*/ | ||
@Immutable | ||
public class RatcliffObershelp implements | ||
NormalizedStringSimilarity, NormalizedStringDistance { | ||
|
||
/** | ||
* Compute the Ratcliff-Obershelp similarity between strings. | ||
* | ||
* @param s1 The first string to compare. | ||
* @param s2 The second string to compare. | ||
* @return The RatcliffObershelp similarity in the range [0, 1] | ||
* @throws NullPointerException if s1 or s2 is null. | ||
*/ | ||
public final double similarity(final String s1, final String s2) { | ||
if (s1 == null) { | ||
throw new NullPointerException("s1 must not be null"); | ||
} | ||
|
||
if (s2 == null) { | ||
throw new NullPointerException("s2 must not be null"); | ||
} | ||
|
||
if (s1.equals(s2)) { | ||
return 1.0d; | ||
} | ||
|
||
List<String> matches = getMatchList(s1, s2); | ||
int sumofmatches = 0; | ||
Iterator it = matches.iterator(); | ||
|
||
while (it.hasNext()) { | ||
String element = it.next().toString(); | ||
sumofmatches += element.length(); | ||
} | ||
|
||
return 2.0d * sumofmatches / (s1.length() + s2.length()); | ||
} | ||
|
||
/** | ||
* Return 1 - similarity. | ||
* | ||
* @param s1 The first string to compare. | ||
* @param s2 The second string to compare. | ||
* @return 1 - similarity | ||
* @throws NullPointerException if s1 or s2 is null. | ||
*/ | ||
public final double distance(final String s1, final String s2) { | ||
return 1.0d - similarity(s1, s2); | ||
} | ||
|
||
private static List<String> getMatchList(final String s1, final String s2) { | ||
List<String> list = new ArrayList<String>(); | ||
String match = frontMaxMatch(s1, s2); | ||
|
||
if (match.length() > 0) { | ||
String frontsource = s1.substring(0, s1.indexOf(match)); | ||
String fronttarget = s2.substring(0, s2.indexOf(match)); | ||
List<String> frontqueue = getMatchList(frontsource, fronttarget); | ||
|
||
String endsource = s1.substring(s1.indexOf(match) + match.length()); | ||
String endtarget = s2.substring(s2.indexOf(match) + match.length()); | ||
List<String> endqueue = getMatchList(endsource, endtarget); | ||
|
||
list.add(match); | ||
list.addAll(frontqueue); | ||
list.addAll(endqueue); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private static String frontMaxMatch(final String s1, final String s2) { | ||
int longest = 0; | ||
String longestsubstring = ""; | ||
|
||
for (int i = 0; i < s1.length(); ++i) { | ||
for (int j = i + 1; j <= s1.length(); ++j) { | ||
String substring = s1.substring(i, j); | ||
if (s2.contains(substring) && substring.length() > longest) { | ||
longest = substring.length(); | ||
longestsubstring = substring; | ||
} | ||
} | ||
} | ||
|
||
return longestsubstring; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/test/java/info/debatty/java/stringsimilarity/RatcliffObershelpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2015 Thibault Debatty. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package info.debatty.java.stringsimilarity; | ||
|
||
import info.debatty.java.stringsimilarity.testutil.NullEmptyTests; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* | ||
* @author Agung Nugroho | ||
*/ | ||
public class RatcliffObershelpTest { | ||
|
||
|
||
/** | ||
* Test of similarity method, of class RatcliffObershelp. | ||
*/ | ||
@Test | ||
public final void testSimilarity() { | ||
System.out.println("similarity"); | ||
RatcliffObershelp instance = new RatcliffObershelp(); | ||
|
||
// test data from other algorithms | ||
// "My string" vs "My tsring" | ||
// Substrings: | ||
// "ring" ==> 4, "My s" ==> 3, "s" ==> 1 | ||
// Ratcliff-Obershelp = 2*(sum of substrings)/(length of s1 + length of s2) | ||
// = 2*(4 + 3 + 1) / (9 + 9) | ||
// = 16/18 | ||
// = 0.888888 | ||
assertEquals( | ||
0.888888, | ||
instance.similarity("My string", "My tsring"), | ||
0.000001); | ||
|
||
// test data from other algorithms | ||
// "My string" vs "My tsring" | ||
// Substrings: | ||
// "My " ==> 3, "tri" ==> 3, "g" ==> 1 | ||
// Ratcliff-Obershelp = 2*(sum of substrings)/(length of s1 + length of s2) | ||
// = 2*(3 + 3 + 1) / (9 + 9) | ||
// = 14/18 | ||
// = 0.777778 | ||
assertEquals( | ||
0.777778, | ||
instance.similarity("My string", "My ntrisg"), | ||
0.000001); | ||
|
||
// test data from essay by Ilya Ilyankou | ||
// "Comparison of Jaro-Winkler and Ratcliff/Obershelp algorithms | ||
// in spell check" | ||
// https://ilyankou.files.wordpress.com/2015/06/ib-extended-essay.pdf | ||
// p13, expected result is 0.857 | ||
assertEquals( | ||
0.857, | ||
instance.similarity("MATEMATICA", "MATHEMATICS"), | ||
0.001); | ||
|
||
// test data from stringmetric | ||
// https://github.com/rockymadden/stringmetric | ||
// expected output is 0.7368421052631579 | ||
assertEquals( | ||
0.736842, | ||
instance.similarity("aleksander", "alexandre"), | ||
0.000001); | ||
|
||
// test data from stringmetric | ||
// https://github.com/rockymadden/stringmetric | ||
// expected output is 0.6666666666666666 | ||
assertEquals( | ||
0.666666, | ||
instance.similarity("pennsylvania", "pencilvaneya"), | ||
0.000001); | ||
|
||
// test data from wikipedia | ||
// https://en.wikipedia.org/wiki/Gestalt_Pattern_Matching | ||
// expected output is 14/18 = 0.7777777777777778 | ||
assertEquals( | ||
0.777778, | ||
instance.similarity("WIKIMEDIA", "WIKIMANIA"), | ||
0.000001); | ||
|
||
// test data from wikipedia | ||
// https://en.wikipedia.org/wiki/Gestalt_Pattern_Matching | ||
// expected output is 24/40 = 0.65 | ||
assertEquals( | ||
0.6, | ||
instance.similarity("GESTALT PATTERN MATCHING", "GESTALT PRACTICE"), | ||
0.000001); | ||
|
||
NullEmptyTests.testSimilarity(instance); | ||
} | ||
|
||
@Test | ||
public final void testDistance() { | ||
RatcliffObershelp instance = new RatcliffObershelp(); | ||
NullEmptyTests.testDistance(instance); | ||
|
||
// TODO: regular (non-null/empty) distance tests | ||
} | ||
} |