github.com/blend/go-sdk@v1.20220411.3/diff/levenshtein.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package diff 9 10 import "unicode/utf8" 11 12 // Levenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters. 13 func Levenshtein(diffs []Diff) int { 14 levenshtein := 0 15 insertions := 0 16 deletions := 0 17 18 for _, aDiff := range diffs { 19 switch aDiff.Type { 20 case DiffInsert: 21 insertions += utf8.RuneCountInString(aDiff.Text) 22 case DiffDelete: 23 deletions += utf8.RuneCountInString(aDiff.Text) 24 case DiffEqual: 25 // A deletion and an insertion is one substitution. 26 levenshtein += max(insertions, deletions) 27 insertions = 0 28 deletions = 0 29 } 30 } 31 32 levenshtein += max(insertions, deletions) 33 return levenshtein 34 }