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

    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
    019/**
    020 * Measures the Jaccard distance of two sets of character sequence. Jaccard
    021 * distance is the dissimilarity between two sets. It is the complementary of
    022 * Jaccard similarity.
    023 *
    024 * <p>
    025 * For further explanation about Jaccard Distance, refer
    026 * https://en.wikipedia.org/wiki/Jaccard_index
    027 * </p>
    028 *
    029 * @since 1.0
    030 */
    031public class JaccardDistance implements EditDistance<Double> {
    032
    033    /**
    034     * Calculates Jaccard distance of two set character sequence passed as
    035     * input. Calculates Jaccard similarity and returns the complement of it.
    036     *
    037     * @param left first character sequence
    038     * @param right second character sequence
    039     * @return index
    040     * @throws IllegalArgumentException
    041     *             if either String input {@code null}
    042     */
    043    @Override
    044    public Double apply(final CharSequence left, final CharSequence right) {
    045        if (left == null || right == null) {
    046            throw new IllegalArgumentException("Input cannot be null");
    047        }
    048        return 1.0 - JaccardSimilarity.INSTANCE.apply(left, right).doubleValue();
    049    }
    050}