Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Damerau distance incorrect for swap + insert? #7

Open
xeoncross opened this issue Sep 28, 2019 · 2 comments
Open

Damerau distance incorrect for swap + insert? #7

xeoncross opened this issue Sep 28, 2019 · 2 comments

Comments

@xeoncross
Copy link

I believe the two strings "CA" and "ABC" technically only require two changes under Damerau-Levenshtein distance: Transposition + Insertion.

CA -> AC -> ABC

This library returns "3" as the number of changes. Is this correct?

@masatana
Copy link
Owner

Thanks for reporting this. It looks like I misimplemented OSA instead of Damerau–Levenshtein distance?

https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance

@xeoncross
Copy link
Author

I'm still not sure this case of swapping is well-covered by the spec. Here is the test I ran:

func TestDL(t *testing.T) {

	var table = []struct {
		A     string
		B     string
		Score int
	}{
		{"", "", 0},
		{" ", " ", 0},
		{"ABC", "ABC", 0},
		{"A", "B", 1},
		{"A", "", 1},
		{"", "B", 1},
		{"c", "cat", 2},
		{"a", "hot", 3},
		{"CA", "ABC", 3}, // or 2? "CA" -> "AC" -> "ABC"
		{"aabc", "abc", 1},
		{"abcc", "abc", 1},
		{"abc", "bac", 1},
		{"abc", "abcc", 1},
		{"abc", "aabc", 1},
		{"abcdef", "poiu", 6},
		{"teh", "the", 1},
		{"tets", "test", 1},
		{"fuor", "four", 1},
		{"kitten", "sitting", 3},
		{"Saturday", "Sunday", 3},
		{"rosettacode", "raisethysword", 8},
	}

	for _, row := range table {
		score := textdistance.DamerauLevenshteinDistance(row.A, row.B)
		if score != row.Score {
			t.Errorf("Textdistance: Expected score=%d, got score=%d (A='%s', B='%s')", row.Score, score, row.A, row.B)
		}
	}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants