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 JaccardDistance}.
27   */
28  public class JaccardDistanceTest {
29  
30      private static JaccardDistance classBeingTested;
31  
32      @BeforeAll
33      public static void setUp() {
34          classBeingTested = new JaccardDistance();
35      }
36  
37      @Test
38      public void testGettingJaccardDistance() {
39          // Expected Jaccard distance = 1.0 - (intersect / union)
40          assertEquals(0.0, classBeingTested.apply("", ""));
41          assertEquals(1.0, classBeingTested.apply("left", ""));
42          assertEquals(1.0, classBeingTested.apply("", "right"));
43          assertEquals(1.0 - 3.0 / 4, classBeingTested.apply("frog", "fog"));
44          assertEquals(1.0, classBeingTested.apply("fly", "ant"));
45          assertEquals(1.0 - 2.0 / 9, classBeingTested.apply("elephant", "hippo"));
46          assertEquals(1.0 - 7.0 / 11, classBeingTested.apply("ABC Corporation", "ABC Corp"));
47          assertEquals(1.0 - 13.0 / 17,
48                  classBeingTested.apply("D N H Enterprises Inc", "D & H Enterprises, Inc."));
49          assertEquals(1.0 - 16.0 / 18,
50                  classBeingTested.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
51          assertEquals(1.0 - 9.0 / 10, classBeingTested.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
52          assertEquals(1.0 - 1.0 / 8, classBeingTested.apply("left", "right"));
53          assertEquals(1.0 - 1.0 / 8, classBeingTested.apply("leettteft", "ritttght"));
54          assertEquals(0.0, classBeingTested.apply("the same string", "the same string"));
55      }
56  
57      @Test
58      public void testGettingJaccardDistanceNullNull() {
59          assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(null, null));
60      }
61  
62      @Test
63      public void testGettingJaccardDistanceNullString() {
64          assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(null, "right"));
65      }
66  
67      @Test
68      public void testGettingJaccardDistanceStringNull() {
69          assertThatIllegalArgumentException().isThrownBy(() -> classBeingTested.apply(" ", null));
70      }
71  }