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 * Jaro Winkler similarity object used to calculate the distance (1 - similarity). 049 */ 050 private final JaroWinklerSimilarity similarity = new JaroWinklerSimilarity(); 051 052 /** 053 * Computes the Jaro Winkler Distance between two character sequences. 054 * 055 * <pre> 056 * distance.apply(null, null) = IllegalArgumentException 057 * distance.apply("foo", null) = IllegalArgumentException 058 * distance.apply(null, "foo") = IllegalArgumentException 059 * distance.apply("", "") = 0.0 060 * distance.apply("foo", "foo") = 0.0 061 * distance.apply("foo", "foo ") = 0.06 062 * distance.apply("foo", "foo ") = 0.09 063 * distance.apply("foo", " foo ") = 0.13 064 * distance.apply("foo", " foo") = 0.49 065 * distance.apply("", "a") = 1.0 066 * distance.apply("aaapppp", "") = 1.0 067 * distance.apply("frog", "fog") = 0.07 068 * distance.apply("fly", "ant") = 1.0 069 * distance.apply("elephant", "hippo") = 0.56 070 * distance.apply("hippo", "elephant") = 0.56 071 * distance.apply("hippo", "zzzzzzzz") = 1.0 072 * distance.apply("hello", "hallo") = 0.12 073 * distance.apply("ABC Corporation", "ABC Corp") = 0.09 074 * distance.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.05 075 * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.08 076 * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.12 077 * </pre> 078 * 079 * @param left the first CharSequence, must not be null 080 * @param right the second CharSequence, must not be null 081 * @return result distance 082 * @throws IllegalArgumentException if either CharSequence input is {@code null} 083 */ 084 @Override 085 public Double apply(final CharSequence left, final CharSequence right) { 086 087 if (left == null || right == null) { 088 throw new IllegalArgumentException("CharSequences must not be null"); 089 } 090 091 return 1 - similarity.apply(left, right); 092 } 093}