DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.
(diffs []Diff)
| 1220 | |
| 1221 | // DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters. |
| 1222 | func (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. |