    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 *      https://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 distance is the dissimilarity between two sets. It is the complementary of Jaccard
    021 * similarity.
    022 *
    023 * <p>
    024 * For further explanation about Jaccard Distance, refer https://en.wikipedia.org/wiki/Jaccard_index
    025 * </p>
    026 *
    027 * @since 1.0
    028 */
    029public class JaccardDistance implements EditDistance<Double> {
    030
    031    /**
    032     * Creates a new instance.
    033     */
    034    public JaccardDistance() {
    035        // empty
    036    }
    037
    038    /**
    039     * Computes the Jaccard distance of two set character sequence passed as input. Calculates Jaccard similarity and returns the complement of it.
    040     *
    041     * @param left  first input sequence.
    042     * @param right second input sequence.
    043     * @return The Jaccard distance.
    044     * @throws IllegalArgumentException if either String input {@code null}.
    045     */
    046    @Override
    047    public Double apply(final CharSequence left, final CharSequence right) {
    048        return apply(SimilarityInput.input(left), SimilarityInput.input(right));
    049    }
    050
    051    /**
    052     * Computes the Jaccard distance of two set character sequence passed as input. Calculates Jaccard similarity and returns the complement of it.
    053     *
    054     * @param <E>   The type of similarity score unit.
    055     * @param left  first input sequence.
    056     * @param right second input sequence.
    057     * @return index.
    058     * @throws IllegalArgumentException if either String input {@code null}.
    059     */
    060    public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
    061        return 1.0 - JaccardSimilarity.INSTANCE.apply(left, right).doubleValue();
    062    }
    063
    064}