pyxDamerauLevenshtein

repository·master·Indexed 42 days ago

https://github.com/lanl/pyxdameraulevenshtein

A high-performance Python implementation of the Damerau-Levenshtein edit distance algorithm written in Cython. Version 1.10.1 provides raw and normalized distance metrics for single sequences or batches of sequences, supporting types such as str, list, tuple, and range.

Tokens
852
Snippets
4
Records
4
Agent score
4%

What's inside pyxDamerauLevenshtein

  1. Compute raw edit distance with damerau_levenshtein_distance

    master

    Use damerau_levenshtein_distance(seq1, seq2, max_distance=None) to compute the minimum number of operations (insertion, deletion, substitution, or transposition) required to transform one sequence into another.

    • Supported types: str, list, tuple, range, and more.
    • Performance optimization: If you provide a max_distance integer, the function will short-circuit and return max_distance + 1 if the true distance exceeds the threshold, avoiding unnecessary computation.
    from pyxdameraulevenshtein import damerau_levenshtein_distance
    
    # Basic usage
    damerau_levenshtein_distance('smtih', 'smith')  # returns 1
    
    # Using a threshold for short-circuiting
    damerau_levenshtein_distance('saturday', 'sunday', max_distance=2)  # returns 3
    
    # Works with non-string sequences
    damerau_levenshtein_distance([1, 2, 3, 4, 5, 6], [7, 8, 9, 7, 10, 11, 4])  # returns 7
  2. Compute normalized edit distance with normalized_damerau_levenshtein_distance

    master

    Use normalized_damerau_levenshtein_distance(seq1, seq2) to compute the ratio of the edit distance to the length of the longer sequence.

    • Scale: 0.0 means sequences are identical; 1.0 means they have nothing in common.
    • Note: This definition is the inverse of difflib.SequenceMatcher.ratio().
    from pyxdameraulevenshtein import normalized_damerau_levenshtein_distance
    
    normalized_damerau_levenshtein_distance('smtih', 'smith')  # returns 0.2
  3. Compute distances against a sequence of sequences

    master

    If you need to compare one sequence against multiple other sequences (e.g., a list of strings), use the _seqs variants of the API. These functions return a collection of distances corresponding to each element in the target sequence.

    • damerau_levenshtein_distance_seqs(seq, seqs, max_distance=None): Returns raw distances.
    • normalized_damerau_levenshtein_distance_seqs(seq, seqs): Returns normalized distance ratios.
    from pyxdameraulevenshtein import damerau_levenshtein_distance_seqs, normalized_damerau_levenshtein_distance_seqs
    
    array = ['test1', 'test12', 'test123']
    
    # Raw distances
    damerau_levenshtein_distance_seqs('test', array)  # returns [1, 2, 3]
    
    # Normalized distances
    normalized_damerau_levenshtein_distance_seqs('test', array)  # returns [0.2, 0.3333333333333333, 0.42857142857142855]