    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     * We normalize the jaccardSimilarity for the purpose of computing the distance.
    035     */
    036    private final JaccardSimilarity jaccardSimilarity = new JaccardSimilarity();
    037
    038    /**
    039     * Calculates Jaccard distance of two set character sequence passed as
    040     * input. Calculates Jaccard similarity and returns the complement of it.
    041     *
    042     * @param left first character sequence
    043     * @param right second character sequence
    044     * @return index
    045     * @throws IllegalArgumentException
    046     *             if either String input {@code null}
    047     */
    048    @Override
    049    public Double apply(final CharSequence left, final CharSequence right) {
    050        if (left == null || right == null) {
    051            throw new IllegalArgumentException("Input cannot be null");
    052        }
    053        return 1.0 - jaccardSimilarity.apply(left, right).doubleValue();
    054    }
    055}