View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.text.similarity;
18  
19  import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.junit.jupiter.api.BeforeAll;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests {@link JaroWinklerDistance}.
27   */
28  public class JaroWinklerDistanceTest {
29  
30      private static JaroWinklerDistance distance;
31  
32      @BeforeAll
33      public static void setUp() {
34          distance = new JaroWinklerDistance();
35      }
36  
37      @Test
38      public void testGetJaroWinklerDistance_NullNull() {
39          assertThatIllegalArgumentException().isThrownBy(() -> distance.apply(null, null));
40      }
41  
42      @Test
43      public void testGetJaroWinklerDistance_NullString() {
44          assertThatIllegalArgumentException().isThrownBy(() -> distance.apply(null, "clear"));
45      }
46  
47      @Test
48      public void testGetJaroWinklerDistance_StringNull() {
49          assertThatIllegalArgumentException().isThrownBy(() -> distance.apply(" ", null));
50      }
51  
52      @Test
53      public void testGetJaroWinklerDistance_StringString() {
54          assertEquals(0.07501d, distance.apply("frog", "fog"), 0.00001d);
55          assertEquals(1.0d, distance.apply("fly", "ant"), 0.00000000000000000001d);
56          assertEquals(0.55834d, distance.apply("elephant", "hippo"), 0.00001d);
57          assertEquals(0.09334d, distance.apply("ABC Corporation", "ABC Corp"), 0.00001d);
58          assertEquals(0.04749d, distance.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."), 0.00001d);
59          assertEquals(0.058d, distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"), 0.00001d);
60          assertEquals(0.101982d, distance.apply("PENNSYLVANIA", "PENNCISYLVNIA"), 0.00001d);
61          assertEquals(0.028572d, distance.apply("/opt/software1", "/opt/software2"), 0.00001d);
62          assertEquals(0.058334d, distance.apply("aaabcd", "aaacdb"), 0.00001d);
63          assertEquals(0.088889d, distance.apply("John Horn", "John Hopkins"), 0.00001d);
64          assertEquals(0d, distance.apply("", ""), 0.00001d);
65          assertEquals(0d, distance.apply("foo", "foo"), 0.00001d);
66          assertEquals(1 - 0.94166d, distance.apply("foo", "foo "), 0.00001d);
67          assertEquals(1 - 0.90666d, distance.apply("foo", "foo  "), 0.00001d);
68          assertEquals(1 - 0.86666d, distance.apply("foo", " foo "), 0.00001d);
69          assertEquals(1 - 0.51111d, distance.apply("foo", "  foo"), 0.00001d);
70      }
71  
72  }