|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2015 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.NormalizedStringSimilarity; |
| 27 | +import info.debatty.java.stringsimilarity.interfaces.NormalizedStringDistance; |
| 28 | +import java.util.List; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Iterator; |
| 31 | + |
| 32 | +import net.jcip.annotations.Immutable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Ratcliff/Obershelp pattern recognition |
| 36 | + * The Ratcliff/Obershelp algorithm computes the similarity of two strings a |
| 37 | + * the doubled number of matching characters divided by the total number of |
| 38 | + * characters in the two strings. Matching characters are those in the longest |
| 39 | + * common subsequence plus, recursively, matching characters in the unmatched |
| 40 | + * region on either side of the longest common subsequence. |
| 41 | + * The Ratcliff/Obershelp distance is computed as 1 - Ratcliff/Obershelp |
| 42 | + * similarity. |
| 43 | + * |
| 44 | + * @author Ligi https://github.com/dxpux (as a patch for fuzzystring) |
| 45 | + * Ported to java from .net by denmase |
| 46 | + */ |
| 47 | +@Immutable |
| 48 | +public class RatcliffObershelp implements |
| 49 | + NormalizedStringSimilarity, NormalizedStringDistance { |
| 50 | + |
| 51 | + /** |
| 52 | + * Compute the Ratcliff-Obershelp similarity between strings. |
| 53 | + * |
| 54 | + * @param s1 The first string to compare. |
| 55 | + * @param s2 The second string to compare. |
| 56 | + * @return The RatcliffObershelp similarity in the range [0, 1] |
| 57 | + * @throws NullPointerException if s1 or s2 is null. |
| 58 | + */ |
| 59 | + public final double similarity(final String s1, final String s2) { |
| 60 | + if (s1 == null) { |
| 61 | + throw new NullPointerException("s1 must not be null"); |
| 62 | + } |
| 63 | + |
| 64 | + if (s2 == null) { |
| 65 | + throw new NullPointerException("s2 must not be null"); |
| 66 | + } |
| 67 | + |
| 68 | + if (s1.equals(s2)) { |
| 69 | + return 1.0d; |
| 70 | + } |
| 71 | + |
| 72 | + List<String> matches = getMatchList(s1, s2); |
| 73 | + int sumofmatches = 0; |
| 74 | + Iterator it = matches.iterator(); |
| 75 | + |
| 76 | + while (it.hasNext()) { |
| 77 | + String element = it.next().toString(); |
| 78 | + sumofmatches += element.length(); |
| 79 | + } |
| 80 | + |
| 81 | + return 2.0d * sumofmatches / (s1.length() + s2.length()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return 1 - similarity. |
| 86 | + * |
| 87 | + * @param s1 The first string to compare. |
| 88 | + * @param s2 The second string to compare. |
| 89 | + * @return 1 - similarity |
| 90 | + * @throws NullPointerException if s1 or s2 is null. |
| 91 | + */ |
| 92 | + public final double distance(final String s1, final String s2) { |
| 93 | + return 1.0d - similarity(s1, s2); |
| 94 | + } |
| 95 | + |
| 96 | + private static List<String> getMatchList(final String s1, final String s2) { |
| 97 | + List<String> list = new ArrayList<String>(); |
| 98 | + String match = frontMaxMatch(s1, s2); |
| 99 | + |
| 100 | + if (match.length() > 0) { |
| 101 | + String frontsource = s1.substring(0, s1.indexOf(match)); |
| 102 | + String fronttarget = s2.substring(0, s2.indexOf(match)); |
| 103 | + List<String> frontqueue = getMatchList(frontsource, fronttarget); |
| 104 | + |
| 105 | + String endsource = s1.substring(s1.indexOf(match) + match.length()); |
| 106 | + String endtarget = s2.substring(s2.indexOf(match) + match.length()); |
| 107 | + List<String> endqueue = getMatchList(endsource, endtarget); |
| 108 | + |
| 109 | + list.add(match); |
| 110 | + list.addAll(frontqueue); |
| 111 | + list.addAll(endqueue); |
| 112 | + } |
| 113 | + |
| 114 | + return list; |
| 115 | + } |
| 116 | + |
| 117 | + private static String frontMaxMatch(final String s1, final String s2) { |
| 118 | + int longest = 0; |
| 119 | + String longestsubstring = ""; |
| 120 | + |
| 121 | + for (int i = 0; i < s1.length(); ++i) { |
| 122 | + for (int j = i + 1; j <= s1.length(); ++j) { |
| 123 | + String substring = s1.substring(i, j); |
| 124 | + if (s2.contains(substring) && substring.length() > longest) { |
| 125 | + longest = substring.length(); |
| 126 | + longestsubstring = substring; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return longestsubstring; |
| 132 | + } |
| 133 | +} |
0 commit comments