001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.text.similarity; 018 019import java.util.Arrays; 020 021/** 022 * Measures the Jaro-Winkler distance of two character sequences. 023 * It is the complementary of Jaro-Winkler similarity. 024 * 025 * @since 1.0 026 */ 027public class JaroWinklerDistance implements EditDistance<Double> { 028 029 /** 030 * @deprecated Deprecated as of 1.7. This constant will be removed in 2.0. 031 */ 032 @Deprecated 033 public static final int INDEX_NOT_FOUND = -1; 034 035 /** 036 * Computes the Jaro Winkler Distance between two character sequences. 037 * 038 * <pre> 039 * distance.apply(null, null) = IllegalArgumentException 040 * distance.apply("foo", null) = IllegalArgumentException 041 * distance.apply(null, "foo") = IllegalArgumentException 042 * distance.apply("", "") = 0.0 043 * distance.apply("foo", "foo") = 0.0 044 * distance.apply("foo", "foo ") = 0.06 045 * distance.apply("foo", "foo ") = 0.09 046 * distance.apply("foo", " foo ") = 0.13 047 * distance.apply("foo", " foo") = 0.49 048 * distance.apply("", "a") = 1.0 049 * distance.apply("aaapppp", "") = 1.0 050 * distance.apply("frog", "fog") = 0.07 051 * distance.apply("fly", "ant") = 1.0 052 * distance.apply("elephant", "hippo") = 0.56 053 * distance.apply("hippo", "elephant") = 0.56 054 * distance.apply("hippo", "zzzzzzzz") = 1.0 055 * distance.apply("hello", "hallo") = 0.12 056 * distance.apply("ABC Corporation", "ABC Corp") = 0.09 057 * distance.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.05 058 * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.08 059 * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.12 060 * </pre> 061 * 062 * @param left the first CharSequence, must not be null 063 * @param right the second CharSequence, must not be null 064 * @return result distance 065 * @throws IllegalArgumentException if either CharSequence input is {@code null} 066 */ 067 @Override 068 public Double apply(final CharSequence left, final CharSequence right) { 069 070 if (left == null || right == null) { 071 throw new IllegalArgumentException("CharSequences must not be null"); 072 } 073 074 // TODO: replace the rest of the code by this in 2.0, see TEXT-104 075 // 076 // JaroWinklerSimilarity similarity = new JaroWinklerSimilarity(); 077 // return 1 - similarity.apply(left, right); 078 079 final double defaultScalingFactor = 0.1; 080 final int[] mtp = matches(left, right); 081 final double m = mtp[0]; 082 if (m == 0) { 083 return 0D; 084 } 085 final double j = ((m / left.length() + m / right.length() + (m - (double) mtp[1] / 2) / m)) / 3; 086 final double jw = j < 0.7D ? j : j + defaultScalingFactor * mtp[2] * (1D - j); 087 return jw; 088 } 089 090 // TODO: remove this method in 2.0, see TEXT-104 091 /** 092 * This method returns the Jaro-Winkler string matches, half transpositions, prefix array. 093 * 094 * @param first the first string to be matched 095 * @param second the second string to be matched 096 * @return mtp array containing: matches, half transpositions, and prefix 097 * @deprecated Deprecated as of 1.7. This method will be removed in 2.0, and moved to a Jaro Winkler similarity 098 * class. 099 */ 100 @Deprecated 101 protected static int[] matches(final CharSequence first, final CharSequence second) { 102 CharSequence max, min; 103 if (first.length() > second.length()) { 104 max = first; 105 min = second; 106 } else { 107 max = second; 108 min = first; 109 } 110 final int range = Math.max(max.length() / 2 - 1, 0); 111 final int[] matchIndexes = new int[min.length()]; 112 Arrays.fill(matchIndexes, -1); 113 final boolean[] matchFlags = new boolean[max.length()]; 114 int matches = 0; 115 for (int mi = 0; mi < min.length(); mi++) { 116 final char c1 = min.charAt(mi); 117 for (int xi = Math.max(mi - range, 0), xn = Math.min(mi + range + 1, max.length()); xi < xn; xi++) { 118 if (!matchFlags[xi] && c1 == max.charAt(xi)) { 119 matchIndexes[mi] = xi; 120 matchFlags[xi] = true; 121 matches++; 122 break; 123 } 124 } 125 } 126 final char[] ms1 = new char[matches]; 127 final char[] ms2 = new char[matches]; 128 for (int i = 0, si = 0; i < min.length(); i++) { 129 if (matchIndexes[i] != -1) { 130 ms1[si] = min.charAt(i); 131 si++; 132 } 133 } 134 for (int i = 0, si = 0; i < max.length(); i++) { 135 if (matchFlags[i]) { 136 ms2[si] = max.charAt(i); 137 si++; 138 } 139 } 140 int halfTranspositions = 0; 141 for (int mi = 0; mi < ms1.length; mi++) { 142 if (ms1[mi] != ms2[mi]) { 143 halfTranspositions++; 144 } 145 } 146 int prefix = 0; 147 for (int mi = 0; mi < Math.min(4, min.length()); mi++) { 148 if (first.charAt(mi) == second.charAt(mi)) { 149 prefix++; 150 } else { 151 break; 152 } 153 } 154 return new int[] {matches, halfTranspositions, prefix}; 155 } 156}