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_r263985647
########## 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 ∩ B|</code>). + * The result can be used to produce various similarity metrics, for example the Jaccard index or + * Sø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ø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 ∩ 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 ∩ B|</code> + */ + public int getIntersection() { + return intersection; + } + /** + * Get the size of the union between set A and B. + * + * @return <code>|A ∪ B|</code> + */ + public long getUnion() { Review comment: From the class description, _Container class to store the intersection results between two sets._, it makes sense to have a `getIntersection()` method. But wouldn't the union belong to something like `UnionResult`? This class appears to have a similar interface to [that class from Simmetrics](https://github.com/Simmetrics/simmetrics/blob/master/simmetrics-core/src/main/java/org/simmetrics/metrics/Math.java) I mentioned some time ago. Perhaps the class and the similarity should be named something else? ---------------------------------------------------------------- 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] With regards, Apache Git Services
