    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
    019/**
    020 * Measures the Jaro-Winkler distance of two character sequences.
    021 * It is the complementary of Jaro-Winkler similarity.
    022 *
    023 * @since 1.0
    024 */
    025public class JaroWinklerDistance implements EditDistance<Double> {
    026
    027    /**
    028     * @deprecated Deprecated as of 1.7. This constant will be removed in 2.0.
    029     */
    030    @Deprecated
    031    public static final int INDEX_NOT_FOUND = -1;
    032
    033    /**
    034     * This method returns the Jaro-Winkler string matches, half transpositions, prefix array.
    035     *
    036     * @param first the first string to be matched
    037     * @param second the second string to be matched
    038     * @return mtp array containing: matches, half transpositions, and prefix
    039     * @deprecated Deprecated as of 1.7. This method will be removed in 2.0, and moved to a Jaro Winkler similarity
    040     *             class. TODO see TEXT-104.
    041     */
    042    @Deprecated
    043    protected static int[] matches(final CharSequence first, final CharSequence second) {
    044        return JaroWinklerSimilarity.matches(first, second);
    045    }
    046
    047    /**
    048     * Computes the Jaro Winkler Distance between two character sequences.
    049     *
    050     * <pre>
    051     * distance.apply(null, null)          = IllegalArgumentException
    052     * distance.apply("foo", null)         = IllegalArgumentException
    053     * distance.apply(null, "foo")         = IllegalArgumentException
    054     * distance.apply("", "")              = 0.0
    055     * distance.apply("foo", "foo")        = 0.0
    056     * distance.apply("foo", "foo ")       = 0.06
    057     * distance.apply("foo", "foo  ")      = 0.09
    058     * distance.apply("foo", " foo ")      = 0.13
    059     * distance.apply("foo", "  foo")      = 0.49
    060     * distance.apply("", "a")             = 1.0
    061     * distance.apply("aaapppp", "")       = 1.0
    062     * distance.apply("frog", "fog")       = 0.07
    063     * distance.apply("fly", "ant")        = 1.0
    064     * distance.apply("elephant", "hippo") = 0.56
    065     * distance.apply("hippo", "elephant") = 0.56
    066     * distance.apply("hippo", "zzzzzzzz") = 1.0
    067     * distance.apply("hello", "hallo")    = 0.12
    068     * distance.apply("ABC Corporation", "ABC Corp") = 0.09
    069     * distance.apply("D N H Enterprises Inc", "D &amp; H Enterprises, Inc.") = 0.05
    070     * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.08
    071     * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.12
    072     * </pre>
    073     *
    074     * @param left the first CharSequence, must not be null
    075     * @param right the second CharSequence, must not be null
    076     * @return result distance
    077     * @throws IllegalArgumentException if either CharSequence input is {@code null}
    078     */
    079    @Override
    080    public Double apply(final CharSequence left, final CharSequence right) {
    081
    082        if (left == null || right == null) {
    083            throw new IllegalArgumentException("CharSequences must not be null");
    084        }
    085
    086        return 1 - JaroWinklerSimilarity.INSTANCE.apply(left, right);
    087    }
    088}