-
Notifications
You must be signed in to change notification settings - Fork 281
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 1 commit
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
74 changes: 74 additions & 0 deletions
74
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,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; | ||
|
|
||
| /** | ||
| * | ||
| * @since 1.7 | ||
| */ | ||
| public class SorensenDicesSimilarity implements SimilarityScore<Double> { | ||
|
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} | ||
| */ | ||
|
|
||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
| @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 ("".equals(left) || "".equals(right)) { | ||
|
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++) { | ||
|
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); | ||
|
ameyjadiye marked this conversation as resolved.
Outdated
|
||
| union.addAll(nLeft); | ||
| union.addAll(nRight); | ||
|
|
||
| final int intersection = total - union.size(); | ||
| return (2.0d * intersection) / total; | ||
|
ameyjadiye marked this conversation as resolved.
|
||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
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,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() { | ||
|
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 & 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"); | ||
| }); | ||
| } | ||
| } | ||
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.