[ 
https://issues.apache.org/jira/browse/TEXT-155?focusedWorklogId=210527&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-210527
 ]

ASF GitHub Bot logged work on TEXT-155:
---------------------------------------

                Author: ASF GitHub Bot
            Created on: 09/Mar/19 12:21
            Start Date: 09/Mar/19 12:21
    Worklog Time Spent: 10m 
      Work Description: aherbert commented on pull request #109: TEXT-155: Add 
a generic IntersectionSimilarity measure
URL: https://github.com/apache/commons-text/pull/109#discussion_r263997429
 
 

 ##########
 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();
+    }
+
+    /**
+     * Gets the S&#248;rensen-Dice coefficient. The coefficient is twice the 
size of the intersection
+     * divided by the size of both sets.
+     *
+     * <pre>
+     * <code>2|A &#8745; B| / (|A| + |B|) </code>
+     * </pre>
+     *
+     * <p>This is also known as the F1 score.
 
 Review comment:
   Thanks. I also noticed that but had stopped firing up insignificant changes 
to GitHub. It is not detected by checkstyle and I ran `mvn javadoc` OK. It 
would have to be a JDK < 1.8 to break building the javadoc IIUC.
 
----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 210527)
    Time Spent: 4h  (was: 3h 50m)

> Add a generic SetSimilarity measure
> -----------------------------------
>
>                 Key: TEXT-155
>                 URL: https://issues.apache.org/jira/browse/TEXT-155
>             Project: Commons Text
>          Issue Type: New Feature
>    Affects Versions: 1.6
>            Reporter: Alex D Herbert
>            Priority: Minor
>          Time Spent: 4h
>  Remaining Estimate: 0h
>
> The {{SimilarityScore<T>}} interface can be used to compute a generic result. 
> I propose to add a class that can compute the intersection between two sets 
> formed from the characters. The sets must be formed from the {{CharSequence}} 
> input to the {{apply}} method using a {{Function<CharSequence, Set<T>>}} to 
> convert the {{CharSequence}}. This function can be passed to the 
> {{SimilarityScore<T>}} during construction.
> The result can then be computed to have the size of each set and the 
> intersection.
> I have created an implementation that can compute the equivalent of the 
> {{JaccardSimilary}} class by creating {{Set<Character>}} and also the 
> F1-score using bigrams (pairs of characters) by creating {{Set<String>}}. 
> This relates to 
> [Text-126|https://issues.apache.org/jira/projects/TEXT/issues/TEXT-126] which 
> suggested an algorithm for the Sorensen-Dice similarity, also known as the 
> F1-score.
> Here is an example:
> {code:java}
> // Match the functionality of the JaccardSimilarity class
> Function<CharSequence, Set<Character>> converter = (cs) -> {
>     final Set<Character> set = new HashSet<>();
>     for (int i = 0; i < cs.length(); i++) {
>         set.add(cs.charAt(i));
>     }
>     return set;
> };
> IntersectionSimilarity<Character> similarity = new 
> IntersectionSimilarity<>(converter);
> IntersectionResult result = similarity.apply("something", "something else");
> {code}
> The result has the size of set A, set B and the intersection between them.
> This class was inspired by my look through the various similarity 
> implementations. All of them except the {{CosineSimilarity}} perform single 
> character matching between the input {{CharSequence}}s. The 
> {{CosineSimilarity}} tokenises using whitespace to create words.
> This more generic type of implementation will allow a user to determine how 
> to divide the {{CharSequence}} but to create the sets that are compared, e.g. 
> single characters, words, bigrams, etc.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to