    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 org.apache.commons.lang3.StringUtils;
    020
    021import java.util.Arrays;
    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="http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance">
    035 * http://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     * Computes the Jaro Winkler Similarity between two character sequences.
    048     *
    049     * <pre>
    050     * sim.apply(null, null)          = IllegalArgumentException
    051     * sim.apply("foo", null)         = IllegalArgumentException
    052     * sim.apply(null, "foo")         = IllegalArgumentException
    053     * sim.apply("", "")              = 1.0
    054     * sim.apply("foo", "foo")        = 1.0
    055     * sim.apply("foo", "foo ")       = 0.94
    056     * sim.apply("foo", "foo  ")      = 0.91
    057     * sim.apply("foo", " foo ")      = 0.87
    058     * sim.apply("foo", "  foo")      = 0.51
    059     * sim.apply("", "a")             = 0.0
    060     * sim.apply("aaapppp", "")       = 0.0
    061     * sim.apply("frog", "fog")       = 0.93
    062     * sim.apply("fly", "ant")        = 0.0
    063     * sim.apply("elephant", "hippo") = 0.44
    064     * sim.apply("hippo", "elephant") = 0.44
    065     * sim.apply("hippo", "zzzzzzzz") = 0.0
    066     * sim.apply("hello", "hallo")    = 0.88
    067     * sim.apply("ABC Corporation", "ABC Corp") = 0.91
    068     * sim.apply("D N H Enterprises Inc", "D &amp; H Enterprises, Inc.") = 0.95
    069     * sim.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.92
    070     * sim.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.88
    071     * </pre>
    072     *
    073     * @param left the first CharSequence, must not be null
    074     * @param right the second CharSequence, must not be null
    075     * @return result similarity
    076     * @throws IllegalArgumentException if either CharSequence input is {@code null}
    077     */
    078    @Override
    079    public Double apply(final CharSequence left, final CharSequence right) {
    080        final double defaultScalingFactor = 0.1;
    081
    082        if (left == null || right == null) {
    083            throw new IllegalArgumentException("CharSequences must not be null");
    084        }
    085
    086        if (StringUtils.equals(left, right)) {
    087            return 1d;
    088        }
    089
    090        final int[] mtp = matches(left, right);
    091        final double m = mtp[0];
    092        if (m == 0) {
    093            return 0d;
    094        }
    095        final double j = ((m / left.length() + m / right.length() + (m - (double) mtp[1] / 2) / m)) / 3;
    096        final double jw = j < 0.7d ? j : j + defaultScalingFactor * mtp[2] * (1d - j);
    097        return jw;
    098    }
    099
    100    /**
    101     * This method returns the Jaro-Winkler string matches, half transpositions, prefix array.
    102     *
    103     * @param first the first string to be matched
    104     * @param second the second string to be matched
    105     * @return mtp array containing: matches, half transpositions, and prefix
    106     */
    107    protected static int[] matches(final CharSequence first, final CharSequence second) {
    108        CharSequence max, min;
    109        if (first.length() > second.length()) {
    110            max = first;
    111            min = second;
    112        } else {
    113            max = second;
    114            min = first;
    115        }
    116        final int range = Math.max(max.length() / 2 - 1, 0);
    117        final int[] matchIndexes = new int[min.length()];
    118        Arrays.fill(matchIndexes, -1);
    119        final boolean[] matchFlags = new boolean[max.length()];
    120        int matches = 0;
    121        for (int mi = 0; mi < min.length(); mi++) {
    122            final char c1 = min.charAt(mi);
    123            for (int xi = Math.max(mi - range, 0), xn = Math.min(mi + range + 1, max.length()); xi < xn; xi++) {
    124                if (!matchFlags[xi] && c1 == max.charAt(xi)) {
    125                    matchIndexes[mi] = xi;
    126                    matchFlags[xi] = true;
    127                    matches++;
    128                    break;
    129                }
    130            }
    131        }
    132        final char[] ms1 = new char[matches];
    133        final char[] ms2 = new char[matches];
    134        for (int i = 0, si = 0; i < min.length(); i++) {
    135            if (matchIndexes[i] != -1) {
    136                ms1[si] = min.charAt(i);
    137                si++;
    138            }
    139        }
    140        for (int i = 0, si = 0; i < max.length(); i++) {
    141            if (matchFlags[i]) {
    142                ms2[si] = max.charAt(i);
    143                si++;
    144            }
    145        }
    146        int halfTranspositions = 0;
    147        for (int mi = 0; mi < ms1.length; mi++) {
    148            if (ms1[mi] != ms2[mi]) {
    149                halfTranspositions++;
    150            }
    151        }
    152        int prefix = 0;
    153        for (int mi = 0; mi < Math.min(4, min.length()); mi++) {
    154            if (first.charAt(mi) == second.charAt(mi)) {
    155                prefix++;
    156            } else {
    157                break;
    158            }
    159        }
    160        return new int[] {matches, halfTranspositions, prefix};
    161    }
    162
    163}