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

Method DiffLevenshtein

diffmatchpatch/diff.go:1205–1226  ·  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

1203
1204// DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.
1205func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
1206 levenshtein := 0
1207 insertions := 0
1208 deletions := 0
1209
1210 for _, aDiff := range diffs {
1211 switch aDiff.Type {
1212 case DiffInsert:
1213 insertions += utf8.RuneCountInString(aDiff.Text)
1214 case DiffDelete:
1215 deletions += utf8.RuneCountInString(aDiff.Text)
1216 case DiffEqual:
1217 // A deletion and an insertion is one substitution.
1218 levenshtein += max(insertions, deletions)
1219 insertions = 0
1220 deletions = 0
1221 }
1222 }
1223
1224 levenshtein += max(insertions, deletions)
1225 return levenshtein
1226}
1227
1228// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
1229// 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