~linuxgoose/linguistics-robin

ref: f61602823a31a9fe5eeddb01debbcb4188d35056 linguistics-robin/linguistics_robin/distance_metrics/hamming.py -rw-r--r-- 524 bytes
f6160282 — Jordan Robinson Merge pull request #25 from linuxgoose/23-caverphone-2-__vowels-undefined-error 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from ..exceptions import WrongLengthException


def hamming_distance(word1, word2):
    """
    Computes the Hamming distance.

    [Reference]: https://en.wikipedia.org/wiki/Hamming_distance
    [Article]: Hamming, Richard W. (1950), "Error detecting and error correcting codes",
        Bell System Technical Journal 29 (2): 147–160
    """
    from operator import ne
    if len(word1) != len(word2):
        raise WrongLengthException('The words need to be of the same length!')

    return sum(map(ne, word1, word2))