-
Notifications
You must be signed in to change notification settings - Fork 282
TEXT-126: Adding Sorensen-Dice similarity algoritham #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7aae02b
Adding Sorensen-Dice similarity algoritham
ameyjadiye 325658b
added documentation and optimised code
ameyjadiye 4191840
replacing bit faster version of empty check
ameyjadiye 9070f05
Improved javadocs and handled some edge cases
ameyjadiye 505066c
TEXT-155: Add a generic IntersectionSimilarity measure
aherbert 8b92150
TEXT-155: Add a generic IntersectionSimilarity measure
aherbert 0ebc77f
Merge branch 'feature-TEXT-155' of
aherbert 0c66921
TEXT-155: IntersectionSimilarity to support duplicates in union.
aherbert dae816a
TEXT-155: Add word letter pairs test to IntersectionSimilarityTest
aherbert ae21c63
Text-155: Javadoc fix in IntersectionResult
aherbert 9a7d018
TEXT-155: Renamed to OverlapSimilarity.
aherbert af9ed62
Merge pull request #1 from apache/master
ameyjadiye 6344be4
Merge pull request #2 from aherbert/feature-TEXT-155
ameyjadiye 7a4aee8
using OverlapSimilarity for scoring Sorensendice similarity
ameyjadiye 211077b
rounded resulring scores for tests
ameyjadiye 29fd2da
fixed spotbug checkstyle errors
ameyjadiye 0d77d4e
Merge pull request #3 from apache/master
ameyjadiye bcff974
no need
ameyjadiye 2de106e
using new IntersectionSimilarity
ameyjadiye 8f0a97c
corrected javadoc, removed unused code and made instance var private.
ameyjadiye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/main/java/org/apache/commons/text/similarity/SorensenDicesSimilarity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * A similarity algorithm indicating the percentage of matched characters | ||
| * between two character sequences. | ||
| * | ||
| * <p> | ||
| * The Sørensen–Dice coefficient is a statistic used for comparing the | ||
| * similarity of two samples. It was independently developed by the botanists | ||
| * Thorvald Sørensen and Lee Raymond Dice, who published in 1948 and 1945 | ||
| * respectively. The index is known by several other names, especially | ||
| * Sørensen–Dice index, Sørensen index and Dice's coefficient. Other variations | ||
| * include the "similarity coefficient" or "index", such as Dice similarity | ||
| * coefficient (DSC). | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * This implementation is based on the Sørensen–Dice similarity algorithm from | ||
| * <a href= | ||
| * "http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient"> | ||
| * http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient</a>. | ||
| * | ||
| * | ||
| * </p> | ||
| * | ||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
| * @since 1.7 | ||
| */ | ||
| public class SorensenDicesSimilarity implements SimilarityScore<Double> { | ||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * | ||
|
ameyjadiye marked this conversation as resolved.
|
||
| * <pre> | ||
| * similarity.apply("", "") = 1.0 | ||
| * similarity.apply("foo", "foo") = 1.0 | ||
| * similarity.apply("foo", "foo ") = 0.8 | ||
| * similarity.apply("foo", "foo ") = 0.66 | ||
| * similarity.apply("foo", " foo ") = 0.66 | ||
| * similarity.apply("foo", " foo") = 0.66 | ||
| * similarity.apply("", "a") = 0.0 | ||
| * similarity.apply("aaapppp", "") = 0.0 | ||
| * similarity.apply("frog", "fog") = 0.4 | ||
| * similarity.apply("fly", "ant") = 0.0 | ||
| * similarity.apply("elephant", "hippo") = 0.0 | ||
| * similarity.apply("hippo", "elephant") = 0.0 | ||
| * similarity.apply("hippo", "zzzzzzzz") = 0.0 | ||
| * similarity.apply("hello", "hallo") = 0.5 | ||
|
ameyjadiye marked this conversation as resolved.
|
||
| * similarity.apply("ABC Corporation", "ABC Corp") = 0.7 | ||
| * similarity.apply("D N H Enterprises Inc", "D & H Enterprises, Inc.") = 0.74 | ||
| * similarity.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.81 | ||
| * similarity.apply("PENNSYLVANIA", "PENNCISYLVNIA") = 0.69 | ||
| * </pre> | ||
| * | ||
| * @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} | ||
| */ | ||
| @Override | ||
| public Double apply(final CharSequence left, final CharSequence right) { | ||
|
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 (left.length() == 0 || right.length() == 0) { | ||
| return 0d; | ||
| } | ||
|
|
||
| Set<String> nLeft = createBigrams(left); | ||
| Set<String> nRight = createBigrams(right); | ||
|
|
||
| final int total = nLeft.size() + nRight.size(); | ||
| nLeft.retainAll(nRight); | ||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
| final int intersection = nLeft.size(); | ||
|
|
||
| return (2.0d * intersection) / total; | ||
|
ameyjadiye marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Method for creating Bigrams, bigrams are nothing but set of two consecutive | ||
| * characters. | ||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
| * @param charSequence The char sequence for which we need set of bigrams. | ||
| * @return set of bigrams. | ||
| */ | ||
| protected Set<String> createBigrams(CharSequence charSequence) { | ||
| Set<String> set = new HashSet<String>(); | ||
| for (int i = 0; i < charSequence.length() - 1; i++) { | ||
| char chr = charSequence.charAt(i); | ||
| char nextChr = charSequence.charAt(i + 1); | ||
| String bi = "" + chr + nextChr; | ||
| set.add(bi); | ||
| } | ||
| return set; | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
src/test/java/org/apache/commons/text/similarity/SorensenDicesSimilarityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * 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 test() { | ||
| assertEquals(0.25d, similarity.apply("night", "nacht")); | ||
|
ameyjadiye marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| public void testGetSorensenDicesSimilarity_StringString() { | ||
|
|
||
| assertEquals(1d, similarity.apply("", "")); | ||
| assertEquals(0d, similarity.apply("", "a")); | ||
| assertEquals(0d, similarity.apply("a", "")); | ||
| 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 & 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 testGetSorensenDicesSimilarity_NullNull() { | ||
| assertThatIllegalArgumentException().isThrownBy(() -> { | ||
| similarity.apply(null, null); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSorensenDicesSimilarity_StringNull() { | ||
| assertThatIllegalArgumentException().isThrownBy(() -> { | ||
| similarity.apply(" ", null); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSorensenDicesSimilarity_NullString() { | ||
| assertThatIllegalArgumentException().isThrownBy(() -> { | ||
| similarity.apply(null, "clear"); | ||
| }); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.