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