commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [commons-text] kinow commented on a change in pull request #109: TEXT-155: Add a generic IntersectionSimilarity measure
Date Sat, 09 Mar 2019 04:09:50 GMT
kinow commented on a change in pull request #109: TEXT-155: Add a generic IntersectionSimilarity
measure
URL: https://github.com/apache/commons-text/pull/109#discussion_r263985279
 
 

 ##########
 File path: src/main/java/org/apache/commons/text/similarity/IntersectionResult.java
 ##########
 @@ -0,0 +1,166 @@
+/*
+ * 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.Objects;
+
+/**
+ * Container class to store the intersection results between two sets.
+ *
+ * <p>Stores the size of set A, set B and the intersection of A and B (<code>|A
&#8745; B|</code>).
+ * The result can be used to produce various similarity metrics, for example the Jaccard
index or
+ * S&#248;rensen-Dice coefficient (F1 score).</p>
+ *
+ * <p>This class is immutable.</p>
+ *
+ * @since 1.7
+ * @see <a href="https://en.wikipedia.org/wiki/Jaccard_index">Jaccard index</a>
+ * @see <a href="http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient">S&#248;rensen
Dice coefficient</a>
+ * @see <a href="https://en.wikipedia.org/wiki/F1_score">F1 score</a>
+ */
+public class IntersectionResult {
+    /**
+     * The size of set A.
+     */
+    private final int sizeA;
+    /**
+     * The size of set B.
+     */
+    private final int sizeB;
+    /**
+     * The size of the intersection between set A and B.
+     */
+    private final int intersection;
+
+    /**
+     * Create the results for an intersection between two sets.
+     *
+     * @param sizeA the size of set A ({@code |A|})
+     * @param sizeB the size of set B ({@code |B|})
+     * @param intersection the size of the intersection of A and B (<code>|A &#8745;
B|</code>)
+     * @throws IllegalArgumentException if the sizes are negative or the intersection is
greater
+     * than the minimum of the two set sizes
+     */
+    public IntersectionResult(final int sizeA, final int sizeB, final int intersection) {
+        if (sizeA < 0) {
+            throw new IllegalArgumentException("Set size |A| is not positive: " + sizeA);
+        }
+        if (sizeB < 0) {
+            throw new IllegalArgumentException("Set size |B| is not positive: " + sizeB);
+        }
+        if (intersection < 0 || intersection > Math.min(sizeA, sizeB)) {
+            throw new IllegalArgumentException("Invalid intersection of |A| and |B|: " +
intersection);
+        }
+        this.sizeA = sizeA;
+        this.sizeB = sizeB;
+        this.intersection = intersection;
+    }
+
+    /**
+     * Get the size of set A.
+     *
+     * @return |A|
+     */
+    public int getSizeA() {
+        return sizeA;
+    }
+
+    /**
+     * Get the size of set B.
+     *
+     * @return |B|
+     */
+    public int getSizeB() {
+        return sizeB;
+    }
+
+    /**
+     * Get the size of the intersection between set A and B.
+     *
+     * @return <code>|A &#8745; B|</code>
+     */
+    public int getIntersection() {
+        return intersection;
+    }
+    /**
+     * Get the size of the union between set A and B.
+     *
+     * @return <code>|A &#8746; B|</code>
+     */
+    public long getUnion() {
+        return (long) sizeA + sizeB - intersection;
+    }
+
+    /**
+     * Gets the Jaccard index. The Jaccard is the intersection divided by the union.
+     *
+     * <pre><code>|A &#8745; B| / |A &#8746; B| </code></pre>
+     *
+     * <p>This implementation defines the result as zero if there is no intersection,
+     * even when the union is zero to avoid a {@link Double#NaN} result.</p>
+     *
+     * @return the Jaccard index
+     * @see <a href="https://en.wikipedia.org/wiki/Jaccard_index">Jaccard index</a>
+     */
+    public double getJaccardIndex() {
+        return intersection == 0 ? 0.0 : (double) intersection / getUnion();
+    }
 
 Review comment:
   This is weird. The `IntersectionResult` is also able to calculate the jaccard index. In
my opinion, this is responsibility of somebody else, and the intersection result responsibility
should be only store the intersection result.
   
   Here's what I mean in a diff.
   
   ```diff
   diff --git a/src/main/java/org/apache/commons/text/similarity/IntersectionResult.java b/src/main/java/org/apache/commons/text/similarity/IntersectionResult.java
   index 823d5a9..df24017 100644
   --- a/src/main/java/org/apache/commons/text/similarity/IntersectionResult.java
   +++ b/src/main/java/org/apache/commons/text/similarity/IntersectionResult.java
   @@ -105,21 +105,6 @@ public class IntersectionResult {
            return (long) sizeA + sizeB - intersection;
        }
    
   -    /**
   -     * Gets the Jaccard index. The Jaccard is the intersection divided by the union.
   -     *
   -     * <pre><code>|A &#8745; B| / |A &#8746; B| </code></pre>
   -     *
   -     * <p>This implementation defines the result as zero if there is no intersection,
   -     * even when the union is zero to avoid a {@link Double#NaN} result.</p>
   -     *
   -     * @return the Jaccard index
   -     * @see <a href="https://en.wikipedia.org/wiki/Jaccard_index">Jaccard index</a>
   -     */
   -    public double getJaccardIndex() {
   -        return intersection == 0 ? 0.0 : (double) intersection / getUnion();
   -    }
   -
        /**
         * Gets the S&#248;rensen-Dice coefficient. The coefficient is twice the size
of the intersection
         * divided by the size of both sets.
   diff --git a/src/main/java/org/apache/commons/text/similarity/JaccardSimilarity.java b/src/main/java/org/apache/commons/text/similarity/JaccardSimilarity.java
   index 2e88dd2..20084b7 100644
   --- a/src/main/java/org/apache/commons/text/similarity/JaccardSimilarity.java
   +++ b/src/main/java/org/apache/commons/text/similarity/JaccardSimilarity.java
   @@ -16,8 +16,10 @@
     */
    package org.apache.commons.text.similarity;
    
   +import java.util.Collection;
    import java.util.HashSet;
    import java.util.Set;
   +import java.util.function.Function;
    
    /**
     * Measures the Jaccard similarity (aka Jaccard index) of two sets of character
   @@ -62,22 +64,18 @@ public class JaccardSimilarity implements SimilarityScore<Double>
{
         * @return index
         */
        private Double calculateJaccardSimilarity(final CharSequence left, final CharSequence
right) {
   -        final int leftLength = left.length();
   -        final int rightLength = right.length();
   -        if (leftLength == 0 || rightLength == 0) {
   -            return 0d;
   -        }
   -        final Set<Character> leftSet = new HashSet<>();
   -        for (int i = 0; i < leftLength; i++) {
   -            leftSet.add(left.charAt(i));
   -        }
   -        final Set<Character> rightSet = new HashSet<>();
   -        for (int i = 0; i < rightLength; i++) {
   -            rightSet.add(right.charAt(i));
   -        }
   -        final Set<Character> unionSet = new HashSet<>(leftSet);
   -        unionSet.addAll(rightSet);
   -        final int intersectionSize = leftSet.size() + rightSet.size() - unionSet.size();
   -        return 1.0d * intersectionSize / unionSet.size();
   +        final Function<CharSequence, Collection<Character>> converter = cs
-> {
   +            final int length = cs.length();
   +            final Set<Character> set = new HashSet<>(length);
   +            for (int i = 0; i < length; i++) {
   +                set.add(cs.charAt(i));
   +            }
   +            return set;
   +        };
   +        IntersectionSimilarity<Character> intersectionSimilarity = new IntersectionSimilarity<>(converter);
   +        IntersectionResult intersectionResult = intersectionSimilarity.apply(left,  right);
   +        int intersection = intersectionResult.getIntersection();
   +        long union = intersectionResult.getUnion();
   +        return intersection == 0 ? 0.0 : (double) intersection / union;
        }
    }
   ```
   
   The change above is replacing our existing logic to calculate the jaccard index in `JacardSimilarity`
class. Now it is calling the `IntersectionSimilarity` class to get an `IntersectResult`, and
then calculating the jaccard index. All unit tests still pass this way.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message