ajmalsiddiqui / levenshtein-diff

A fast, generic implementation of Levenshtein's Algorithm for any kind of sequence, along with the ability to generate edits and regenerate the target sequence by applying edits to the source sequence.
MIT License
8 stars 3 forks source link

Removing or swapping first item results in an invalid distance matrix. #2

Closed Themayu closed 2 years ago

Themayu commented 2 years ago

Calculating the distance between two collections, where one collection is an exact copy of the other except for the presence of the first item, appears to result in an invalid distance matrix:

fn main() {
    let collection_1: Vec<String> = vec!["Hello".into(), "World".into()];
    let collection_2: Vec<String> = vec!["World".into()];

    let (distance, matrix) = levenshtein_diff::distance(&collection_1, &collection_2);
    let _edits = levenshtein_diff::generate_edits(&collection_1, &collection_2, &matrix);

    println!("levenshtein distance of {distance}");
}

Error log:

   Compiling test-crate v0.1.0 (C:\Users\<private>\test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target\debug\test-crate.exe`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidDistanceMatrixError', src\main.rs:6:90
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\test-crate.exe` (exit code: 101)

This also appears to occur when the first and second items are swapped:

fn main() {
    let collection_1: Vec<String> = vec!["Hello".into(), "World".into()];
    let collection_2: Vec<String> = vec!["World".into(), "Hello".into()];

    let (distance, matrix) = levenshtein_diff::distance(&collection_1, &collection_2);
    let _edits = levenshtein_diff::generate_edits(&collection_1, &collection_2, &matrix); // thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidDistanceMatrixError', src\main.rs:6:90

    println!("levenshtein distance of {distance}");
}
ajmalsiddiqui commented 2 years ago

Hi! Thanks for the bug report! :grinning:

I just pushed a commit with the fix. The commit message and the diff should be enough to explain what caused the bug and why the fix works, in case you're interested.

I've also published version 0.2.3 of levenshtein-diff to crates.io, and this contains the fix for this bug, so please update to that.

I'll be closing this issue, but if it persists even with 0.2.3, feel free to reopen it and I'll be happy to investigate further! :)