Source code
Description: source: package: org.apache.commons.text.similarity, class: JaccardSimilarity

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.text.similarity;
    018
    019import java.util.HashSet;
    020import java.util.Set;
    021
    022/**
    023 * Measures the Jaccard similarity (aka Jaccard index) of two sets of character sequence. Jaccard similarity is the size of the intersection divided by the size
    024 * of the union of the two sets.
    025 *
    026 * <p>
    027 * For further explanation about Jaccard Similarity, refer https://en.wikipedia.org/wiki/Jaccard_index
    028 * </p>
    029 *
    030 * @since 1.0
    031 */
    032public class JaccardSimilarity implements SimilarityScore<Double> {
    033
    034    /**
    035     * Singleton instance.
    036     */
    037    static final JaccardSimilarity INSTANCE = new JaccardSimilarity();
    038
    039    /**
    040     * Computes the Jaccard Similarity of two set character sequence passed as input.
    041     *
    042     * @param left  first input sequence.
    043     * @param right second input sequence.
    044     * @return index.
    045     * @throws IllegalArgumentException if either String input {@code null}.
    046     */
    047    @Override
    048    public Double apply(final CharSequence left, final CharSequence right) {
    049        return apply(SimilarityInput.input(left), SimilarityInput.input(right));
    050    }
    051
    052    /**
    053     * Computes the Jaccard Similarity of two character sequences passed as input. Does the calculation by identifying the union (characters in at least one of
    054     * the two sets) of the two sets and intersection (characters which are present in set one which are present in set two)
    055     *
    056     * @param <E>   The type of similarity score unit.
    057     * @param left  first input sequence.
    058     * @param right second input sequence.
    059     * @return index.
    060     * @since 1.13.0
    061     */
    062    public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
    063        if (left == null || right == null) {
    064            throw new IllegalArgumentException("Input cannot be null");
    065        }
    066        final int leftLength = left.length();
    067        final int rightLength = right.length();
    068        if (leftLength == 0 && rightLength == 0) {
    069            return 1d;
    070        }
    071        if (leftLength == 0 || rightLength == 0) {
    072            return 0d;
    073        }
    074        final Set<E> leftSet = new HashSet<>();
    075        for (int i = 0; i < leftLength; i++) {
    076            leftSet.add(left.at(i));
    077        }
    078        final Set<E> rightSet = new HashSet<>();
    079        for (int i = 0; i < rightLength; i++) {
    080            rightSet.add(right.at(i));
    081        }
    082        final Set<E> unionSet = new HashSet<>(leftSet);
    083        unionSet.addAll(rightSet);
    084        final int intersectionSize = leftSet.size() + rightSet.size() - unionSet.size();
    085        return 1.0d * intersectionSize / unionSet.size();
    086    }
    087}