Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7aae02b
Adding Sorensen-Dice similarity algoritham
ameyjadiye Feb 26, 2019
325658b
added documentation and optimised code
ameyjadiye Feb 27, 2019
4191840
replacing bit faster version of empty check
ameyjadiye Mar 1, 2019
9070f05
Improved javadocs and handled some edge cases
ameyjadiye Mar 3, 2019
505066c
TEXT-155: Add a generic IntersectionSimilarity measure
aherbert Mar 7, 2019
8b92150
TEXT-155: Add a generic IntersectionSimilarity measure
aherbert Mar 7, 2019
0ebc77f
Merge branch 'feature-TEXT-155' of
aherbert Mar 8, 2019
0c66921
TEXT-155: IntersectionSimilarity to support duplicates in union.
aherbert Mar 8, 2019
dae816a
TEXT-155: Add word letter pairs test to IntersectionSimilarityTest
aherbert Mar 8, 2019
ae21c63
Text-155: Javadoc fix in IntersectionResult
aherbert Mar 8, 2019
9a7d018
TEXT-155: Renamed to OverlapSimilarity.
aherbert Mar 9, 2019
af9ed62
Merge pull request #1 from apache/master
ameyjadiye Mar 10, 2019
6344be4
Merge pull request #2 from aherbert/feature-TEXT-155
ameyjadiye Mar 10, 2019
7a4aee8
using OverlapSimilarity for scoring Sorensendice similarity
ameyjadiye Mar 10, 2019
211077b
rounded resulring scores for tests
ameyjadiye Mar 10, 2019
29fd2da
fixed spotbug checkstyle errors
ameyjadiye Mar 10, 2019
0d77d4e
Merge pull request #3 from apache/master
ameyjadiye Mar 13, 2019
bcff974
no need
ameyjadiye Mar 18, 2019
2de106e
using new IntersectionSimilarity
ameyjadiye Mar 18, 2019
8f0a97c
corrected javadoc, removed unused code and made instance var private.
ameyjadiye Mar 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text.similarity;

import java.util.HashSet;
import java.util.Set;

/**
*
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated
* @since 1.7
*/
public class SorensenDicesSimilarity implements SimilarityScore<Double> {
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated

/**
* @param left the first CharSequence, must not be null
* @param right the second CharSequence, must not be null
* @return result similarity
* @throws IllegalArgumentException if either CharSequence input is {@code null}
*/

Comment thread
ameyjadiye marked this conversation as resolved.
Outdated
@Override
public Double apply(final CharSequence left, final CharSequence right) {
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated

if (left == null || right == null) {
throw new IllegalArgumentException("CharSequences must not be null");
}

if (left.equals(right)) {
return 1d;
}

if ("".equals(left) || "".equals(right)) {
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated
return 0d;
}

Set<String> nLeft = new HashSet<String>();
Set<String> nRight = new HashSet<String>();

for (int i = 0; i < left.length() - 1; i++) {
char chr = left.charAt(i);
char nextChr = left.charAt(i + 1);
String bi = "" + chr + nextChr;
nLeft.add(bi);
}
for (int j = 0; j < right.length() - 1; j++) {
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated
char chr = right.charAt(j);
char nextChr = right.charAt(j + 1);
String bi = "" + chr + nextChr;
nRight.add(bi);
}

final int total = nLeft.size() + nRight.size();
final Set<String> union = new HashSet<String>(total);
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated
union.addAll(nLeft);
union.addAll(nRight);

final int intersection = total - union.size();
return (2.0d * intersection) / total;
Comment thread
ameyjadiye marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.text.similarity;

import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link SorensenDicesSimilarity}.
*/
public class SorensenDicesSimilarityTest {

private static SorensenDicesSimilarity similarity;

@BeforeAll
public static void setUp() {
similarity = new SorensenDicesSimilarity();
}

@Test
public void testGetJaroWinklerSimilarity_StringString() {
Comment thread
ameyjadiye marked this conversation as resolved.
Outdated

assertEquals(1d, similarity.apply("", ""));
assertEquals(1.0d, similarity.apply("foo", "foo"));
assertEquals(0.8d, similarity.apply("foo", "foo "));
assertEquals(0.4d, similarity.apply("frog", "fog"));
assertEquals(0.0d, similarity.apply("fly", "ant"));
assertEquals(0.0d, similarity.apply("elephant", "hippo"));
assertEquals(0.0d, similarity.apply("hippo", "elephant"));
assertEquals(0.0d, similarity.apply("hippo", "zzzzzzzz"));
assertEquals(0.5d, similarity.apply("hello", "hallo"));
assertEquals(0.7d, similarity.apply("ABC Corporation", "ABC Corp"));
assertEquals(0.7391304347826086d, similarity.apply("D N H Enterprises Inc", "D &amp; H Enterprises, Inc."));
assertEquals(0.8076923076923077d,
similarity.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness"));
assertEquals(0.6956521739130435, similarity.apply("PENNSYLVANIA", "PENNCISYLVNIA"));
assertEquals(0.9230769230769231, similarity.apply("/opt/software1", "/opt/software2"));
assertEquals(0.5d, similarity.apply("aaabcd", "aaacdb"));
assertEquals(0.631578947368421, similarity.apply("John Horn", "John Hopkins"));

}

@Test
public void testGetDicesCoefficientSimilarity_NullNull() {
assertThatIllegalArgumentException().isThrownBy(() -> {
similarity.apply(null, null);
});
}

@Test
public void testGetDicesCoefficientSimilarity_StringNull() {
assertThatIllegalArgumentException().isThrownBy(() -> {
similarity.apply(" ", null);
});
}

@Test
public void testGetDicesCoefficientSimilarity_NullString() {
assertThatIllegalArgumentException().isThrownBy(() -> {
similarity.apply(null, "clear");
});
}
}