Skip to content

Latest commit

 

History

History
48 lines (33 loc) · 1.4 KB

File metadata and controls

48 lines (33 loc) · 1.4 KB

Levenshtein Distance in Golang

Godoc Build Status

Calculate levenshtein distance in Golang.

Install

By go tool: go get github.com/agflow/levenshtein

Usage

This uses default calculator which has cost of 1 for additions, deletions and substitutions.

import github.com/agflow/levenshtein

levenshtein.Dist("aaa", "ab") // 2

You can specify different weights to increment/deletion and substitutions.

levenshtein.New(1, 1).Dist("aaa", "ab") // 2
levenshtein.New(1, 2).Dist("aaa", "ab") // 3
levenshtein.New(1, 3).Dist("aaa", "ab") // 3
levenshtein.New(1, 4).Dist("aaa", "ab") // 3
levenshtein.New(2, 2).Dist("aaa", "ab") // 4
levenshtein.New(3, 2).Dist("aaa", "ab") // 5

If you don't care difference more than some predefined value and strings are encoded in ascii, and also you want to compare same string to multiple different strings then there is one more performant interface you can utilize:

d := levenshtein.FromBytes([]byte("Mustafa"), 2)
d.Dist([]byte("Kemal")) // 2
d.Dist([]byte("Mustfa")) // 1

d = levenshtein.FromBytes("Mustafa", 6)
d.Dist([]byte("Kemal")) // 6
d.Dist([]byte("Mustfa")) // 1

LICENSE

MIT © AgFlow