    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 *      https://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     * Computes 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 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     * Creates a new instance.
    049     */
    050    public JaroWinklerDistance() {
    051        // empty
    052    }
    053
    054    /**
    055     * Computes the Jaro Winkler Distance between two character sequences.
    056     *
    057     * <pre>
    058     * distance.apply(null, null)          = IllegalArgumentException
    059     * distance.apply("foo", null)         = IllegalArgumentException
    060     * distance.apply(null, "foo")         = IllegalArgumentException
    061     * distance.apply("", "")              = 0.0
    062     * distance.apply("foo", "foo")        = 0.0
    063     * distance.apply("foo", "foo ")       = 0.06
    064     * distance.apply("foo", "foo  ")      = 0.09
    065     * distance.apply("foo", " foo ")      = 0.13
    066     * distance.apply("foo", "  foo")      = 0.49
    067     * distance.apply("", "a")             = 1.0
    068     * distance.apply("aaapppp", "")       = 1.0
    069     * distance.apply("frog", "fog")       = 0.07
    070     * distance.apply("fly", "ant")        = 1.0
    071     * distance.apply("elephant", "hippo") = 0.56
    072     * distance.apply("hippo", "elephant") = 0.56
    073     * distance.apply("hippo", "zzzzzzzz") = 1.0
    074     * distance.apply("hello", "hallo")    = 0.12
    075     * distance.apply("ABC Corporation", "ABC Corp") = 0.09
    076     * distance.apply("D N H Enterprises Inc", "D &amp; H Enterprises, Inc.") = 0.05
    077     * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.08
    078     * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.12
    079     * </pre>
    080     *
    081     * @param left the first input, must not be null.
    082     * @param right the second input, must not be null.
    083     * @return result distance.
    084     * @throws IllegalArgumentException if either CharSequence input is {@code null}.
    085     */
    086    @Override
    087    public Double apply(final CharSequence left, final CharSequence right) {
    088        return apply(SimilarityInput.input(left), SimilarityInput.input(right));
    089    }
    090
    091    /**
    092     * Computes the Jaro Winkler Distance between two character sequences.
    093     *
    094     * <pre>
    095     * distance.apply(null, null)          = IllegalArgumentException
    096     * distance.apply("foo", null)         = IllegalArgumentException
    097     * distance.apply(null, "foo")         = IllegalArgumentException
    098     * distance.apply("", "")              = 0.0
    099     * distance.apply("foo", "foo")        = 0.0
    100     * distance.apply("foo", "foo ")       = 0.06
    101     * distance.apply("foo", "foo  ")      = 0.09
    102     * distance.apply("foo", " foo ")      = 0.13
    103     * distance.apply("foo", "  foo")      = 0.49
    104     * distance.apply("", "a")             = 1.0
    105     * distance.apply("aaapppp", "")       = 1.0
    106     * distance.apply("frog", "fog")       = 0.07
    107     * distance.apply("fly", "ant")        = 1.0
    108     * distance.apply("elephant", "hippo") = 0.56
    109     * distance.apply("hippo", "elephant") = 0.56
    110     * distance.apply("hippo", "zzzzzzzz") = 1.0
    111     * distance.apply("hello", "hallo")    = 0.12
    112     * distance.apply("ABC Corporation", "ABC Corp") = 0.09
    113     * distance.apply("D N H Enterprises Inc", "D &amp; H Enterprises, Inc.") = 0.05
    114     * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.08
    115     * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.12
    116     * </pre>
    117     *
    118     * @param <E> The type of similarity score unit.
    119     * @param left the first input, must not be null.
    120     * @param right the second input, must not be null.
    121     * @return result distance.
    122     * @throws IllegalArgumentException if either CharSequence input is {@code null}.
    123     * @since 1.13.0
    124     */
    125    public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
    126        if (left == null || right == null) {
    127            throw new IllegalArgumentException("CharSequences must not be null");
    128        }
    129        return 1 - JaroWinklerSimilarity.INSTANCE.apply(left, right);
    130    }
    131}