MCPcopy Create free account
hub / github.com/sergi/go-diff / DiffLevenshtein

Method DiffLevenshtein

diffmatchpatch/diff.go:1222–1243  ·  view source on GitHub ↗

DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.

(diffs []Diff)

Source from the content-addressed store, hash-verified

1220
1221// DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.
1222func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
1223 levenshtein := 0
1224 insertions := 0
1225 deletions := 0
1226
1227 for _, aDiff := range diffs {
1228 switch aDiff.Type {
1229 case DiffInsert:
1230 insertions += utf8.RuneCountInString(aDiff.Text)
1231 case DiffDelete:
1232 deletions += utf8.RuneCountInString(aDiff.Text)
1233 case DiffEqual:
1234 // A deletion and an insertion is one substitution.
1235 levenshtein += max(insertions, deletions)
1236 insertions = 0
1237 deletions = 0
1238 }
1239 }
1240
1241 levenshtein += max(insertions, deletions)
1242 return levenshtein
1243}
1244
1245// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
1246// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.

Callers 2

PatchApplyMethod · 0.95
TestDiffLevenshteinFunction · 0.80

Calls 1

maxFunction · 0.85

Tested by 1

TestDiffLevenshteinFunction · 0.64