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

    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 * The hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different.
    021 *
    022 * <p>
    023 * For further explanation about the Hamming Distance, take a look at its Wikipedia page at https://en.wikipedia.org/wiki/Hamming_distance.
    024 * </p>
    025 *
    026 * @since 1.0
    027 */
    028public class HammingDistance implements EditDistance<Integer> {
    029
    030    /**
    031     * Computes the Hamming Distance between two strings with the same length.
    032     *
    033     * <p>
    034     * The distance starts with zero, and for each occurrence of a different character in either String, it increments the distance by 1, and finally return its
    035     * value.
    036     * </p>
    037     *
    038     * <p>
    039     * Since the Hamming Distance can only be calculated between strings of equal length, input of different lengths will throw IllegalArgumentException
    040     * </p>
    041     *
    042     * <pre>
    043     * distance.apply("", "")               = 0
    044     * distance.apply("pappa", "pappa")     = 0
    045     * distance.apply("1011101", "1011111") = 1
    046     * distance.apply("ATCG", "ACCC")       = 2
    047     * distance.apply("karolin", "kerstin"  = 3
    048     * </pre>
    049     *
    050     * @param left  the first input, must not be null.
    051     * @param right the second input, must not be null.
    052     * @return distance.
    053     * @throws IllegalArgumentException if either input is {@code null} or if they do not have the same length.
    054     */
    055    @Override
    056    public Integer apply(final CharSequence left, final CharSequence right) {
    057        return apply(SimilarityInput.input(left), SimilarityInput.input(right));
    058    }
    059
    060    /**
    061     * Computes the Hamming Distance between two strings with the same length.
    062     *
    063     * <p>
    064     * The distance starts with zero, and for each occurrence of a different character in either String, it increments the distance by 1, and finally return its
    065     * value.
    066     * </p>
    067     * <p>
    068     * Since the Hamming Distance can only be calculated between strings of equal length, input of different lengths will throw IllegalArgumentException
    069     * </p>
    070     *
    071     * <pre>
    072     * distance.apply("", "")               = 0
    073     * distance.apply("pappa", "pappa")     = 0
    074     * distance.apply("1011101", "1011111") = 1
    075     * distance.apply("ATCG", "ACCC")       = 2
    076     * distance.apply("karolin", "kerstin"  = 3
    077     * </pre>
    078     *
    079     * @param <E> The type of similarity score unit.
    080     * @param left  the first input, must not be null.
    081     * @param right the second input, must not be null.
    082     * @return distance.
    083     * @throws IllegalArgumentException if either input is {@code null} or if they do not have the same length.
    084     * @since 1.13.0
    085     */
    086    public <E> Integer apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
    087        if (left == null || right == null) {
    088            throw new IllegalArgumentException("SimilarityInput must not be null");
    089        }
    090        if (left.length() != right.length()) {
    091            throw new IllegalArgumentException("SimilarityInput must have the same length");
    092        }
    093        int distance = 0;
    094        for (int i = 0; i < left.length(); i++) {
    095            if (!left.at(i).equals(right.at(i))) {
    096                distance++;
    097            }
    098        }
    099        return distance;
    100    }
    101
    102}