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
    024 * sequence. Jaccard similarity is the size of the intersection divided by the
    025 * size of the union of the two sets.
    026 *
    027 * <p>
    028 * For further explanation about Jaccard Similarity, refer
    029 * https://en.wikipedia.org/wiki/Jaccard_index
    030 * </p>
    031 *
    032 * @since 1.0
    033 */
    034public class JaccardSimilarity implements SimilarityScore<Double> {
    035
    036    /**
    037     * Singleton instance.
    038     */
    039    static final JaccardSimilarity INSTANCE = new JaccardSimilarity();
    040
    041    /**
    042     * Calculates Jaccard Similarity of two set character sequence passed as
    043     * input.
    044     *
    045     * @param left first character sequence
    046     * @param right second character sequence
    047     * @return index
    048     * @throws IllegalArgumentException
    049     *             if either String input {@code null}
    050     */
    051    @Override
    052    public Double apply(final CharSequence left, final CharSequence right) {
    053        if (left == null || right == null) {
    054            throw new IllegalArgumentException("Input cannot be null");
    055        }
    056        return calculateJaccardSimilarity(left, right);
    057    }
    058
    059    /**
    060     * Calculates Jaccard Similarity of two character sequences passed as
    061     * input. Does the calculation by identifying the union (characters in at
    062     * least one of the two sets) of the two sets and intersection (characters
    063     * which are present in set one which are present in set two)
    064     *
    065     * @param left first character sequence
    066     * @param right second character sequence
    067     * @return index
    068     */
    069    private Double calculateJaccardSimilarity(final CharSequence left, final CharSequence right) {
    070        final int leftLength = left.length();
    071        final int rightLength = right.length();
    072        if (leftLength == 0 && rightLength == 0) {
    073            return 1d;
    074        }
    075        if (leftLength == 0 || rightLength == 0) {
    076            return 0d;
    077        }
    078        final Set<Character> leftSet = new HashSet<>();
    079        for (int i = 0; i < leftLength; i++) {
    080            leftSet.add(left.charAt(i));
    081        }
    082        final Set<Character> rightSet = new HashSet<>();
    083        for (int i = 0; i < rightLength; i++) {
    084            rightSet.add(right.charAt(i));
    085        }
    086        final Set<Character> unionSet = new HashSet<>(leftSet);
    087        unionSet.addAll(rightSet);
    088        final int intersectionSize = leftSet.size() + rightSet.size() - unionSet.size();
    089        return 1.0d * intersectionSize / unionSet.size();
    090    }
    091}