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 * A similarity algorithm indicating the percentage of matched characters between two character sequences. 023 * 024 * <p> 025 * The Jaro measure is the weighted sum of percentage of matched characters 026 * from each file and transposed characters. Winkler increased this measure 027 * for matching initial characters. 028 * </p> 029 * 030 * <p> 031 * This implementation is based on the Jaro Winkler similarity algorithm 032 * from <a href="http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance"> 033 * http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance</a>. 034 * </p> 035 * 036 * <p> 037 * This code has been adapted from Apache Commons Lang 3.3. 038 * </p> 039 * 040 * @since 1.0 041 */ 042public class JaroWinklerDistance implements SimilarityScore<Double> { 043 044 /** 045 * The default prefix length limit set to four. 046 */ 047 private static final int PREFIX_LENGTH_LIMIT = 4; 048 /** 049 * Represents a failed index search. 050 */ 051 public static final int INDEX_NOT_FOUND = -1; 052 053 /** 054 * Find the Jaro Winkler Distance which indicates the similarity score 055 * between two CharSequences. 056 * 057 * <pre> 058 * distance.apply(null, null) = IllegalArgumentException 059 * distance.apply("","") = 0.0 060 * distance.apply("","a") = 0.0 061 * distance.apply("aaapppp", "") = 0.0 062 * distance.apply("frog", "fog") = 0.93 063 * distance.apply("fly", "ant") = 0.0 064 * distance.apply("elephant", "hippo") = 0.44 065 * distance.apply("hippo", "elephant") = 0.44 066 * distance.apply("hippo", "zzzzzzzz") = 0.0 067 * distance.apply("hello", "hallo") = 0.88 068 * distance.apply("ABC Corporation", "ABC Corp") = 0.93 069 * distance.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.95 070 * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.92 071 * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.88 072 * </pre> 073 * 074 * @param left the first String, must not be null 075 * @param right the second String, must not be null 076 * @return result distance 077 * @throws IllegalArgumentException if either String input {@code null} 078 */ 079 @Override 080 public Double apply(final CharSequence left, final CharSequence right) { 081 final double defaultScalingFactor = 0.1; 082 final double percentageRoundValue = 100.0; 083 084 if (left == null || right == null) { 085 throw new IllegalArgumentException("Strings must not be null"); 086 } 087 088 int[] mtp = matches(left, right); 089 double m = mtp[0]; 090 if (m == 0) { 091 return 0D; 092 } 093 double j = ((m / left.length() + m / right.length() + (m - mtp[1]) / m)) / 3; 094 double jw = j < 0.7D ? j : j + Math.min(defaultScalingFactor, 1D / mtp[3]) * mtp[2] * (1D - j); 095 return Math.round(jw * percentageRoundValue) / percentageRoundValue; 096 } 097 098 /** 099 * This method returns the Jaro-Winkler string matches, transpositions, prefix, max array. 100 * 101 * @param first the first string to be matched 102 * @param second the second string to be machted 103 * @return mtp array containing: matches, transpositions, prefix, and max length 104 */ 105 protected static int[] matches(final CharSequence first, final CharSequence second) { 106 CharSequence max, min; 107 if (first.length() > second.length()) { 108 max = first; 109 min = second; 110 } else { 111 max = second; 112 min = first; 113 } 114 int range = Math.max(max.length() / 2 - 1, 0); 115 int[] matchIndexes = new int[min.length()]; 116 Arrays.fill(matchIndexes, -1); 117 boolean[] matchFlags = new boolean[max.length()]; 118 int matches = 0; 119 for (int mi = 0; mi < min.length(); mi++) { 120 char c1 = min.charAt(mi); 121 for (int xi = Math.max(mi - range, 0), xn = Math.min(mi + range + 1, max.length()); xi < xn; xi++) { 122 if (!matchFlags[xi] && c1 == max.charAt(xi)) { 123 matchIndexes[mi] = xi; 124 matchFlags[xi] = true; 125 matches++; 126 break; 127 } 128 } 129 } 130 char[] ms1 = new char[matches]; 131 char[] ms2 = new char[matches]; 132 for (int i = 0, si = 0; i < min.length(); i++) { 133 if (matchIndexes[i] != -1) { 134 ms1[si] = min.charAt(i); 135 si++; 136 } 137 } 138 for (int i = 0, si = 0; i < max.length(); i++) { 139 if (matchFlags[i]) { 140 ms2[si] = max.charAt(i); 141 si++; 142 } 143 } 144 int transpositions = 0; 145 for (int mi = 0; mi < ms1.length; mi++) { 146 if (ms1[mi] != ms2[mi]) { 147 transpositions++; 148 } 149 } 150 int prefix = 0; 151 for (int mi = 0; mi < min.length(); mi++) { 152 if (first.charAt(mi) == second.charAt(mi)) { 153 prefix++; 154 } else { 155 break; 156 } 157 } 158 return new int[] { matches, transpositions / 2, prefix, max.length() }; 159 } 160 161}