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 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     * Computes the Jaccard distance of two set character sequence passed as input. Calculates Jaccard similarity and returns the complement of it.
033     *
034     * @param left  first input sequence.
035     * @param right second input sequence.
036     * @return index
037     * @throws IllegalArgumentException if either String input {@code null}.
038     */
039    @Override
040    public Double apply(final CharSequence left, final CharSequence right) {
041        return apply(SimilarityInput.input(left), SimilarityInput.input(right));
042    }
043
044    /**
045     * Computes the Jaccard distance of two set character sequence passed as input. Calculates Jaccard similarity and returns the complement of it.
046     *
047     * @param <E>   The type of similarity score unit.
048     * @param left  first input sequence.
049     * @param right second input sequence.
050     * @return index
051     * @throws IllegalArgumentException if either String input {@code null}.
052     */
053    public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
054        return 1.0 - JaccardSimilarity.INSTANCE.apply(left, right).doubleValue();
055    }
056
057}