levenshtein computes the edit distance between two strings.
(a, b string)
| 303 | |
| 304 | // levenshtein computes the edit distance between two strings. |
| 305 | func levenshtein(a, b string) int { |
| 306 | la, lb := len(a), len(b) |
| 307 | if la == 0 { |
| 308 | return lb |
| 309 | } |
| 310 | if lb == 0 { |
| 311 | return la |
| 312 | } |
| 313 | |
| 314 | prev := make([]int, lb+1) |
| 315 | for j := range prev { |
| 316 | prev[j] = j |
| 317 | } |
| 318 | |
| 319 | for i := 1; i <= la; i++ { |
| 320 | curr := make([]int, lb+1) |
| 321 | curr[0] = i |
| 322 | for j := 1; j <= lb; j++ { |
| 323 | cost := 1 |
| 324 | if a[i-1] == b[j-1] { |
| 325 | cost = 0 |
| 326 | } |
| 327 | curr[j] = min(curr[j-1]+1, min(prev[j]+1, prev[j-1]+cost)) |
| 328 | } |
| 329 | prev = curr |
| 330 | } |
| 331 | return prev[lb] |
| 332 | } |
| 333 | |
| 334 | // promptSelection displays fuzzy matches and asks the user to pick one. |
| 335 | func promptSelection(query string, matches []fuzzyMatch) (*model.Task, error) { |
no outgoing calls