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