/Levenshtein

This is a Go package that provides a function to calculate the Levenshtein distance between two strings. The Levenshtein distance is a measure of the difference between two strings, defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other.

Primary LanguageGoApache License 2.0Apache-2.0

Levenshtein Distance Calculation

This is a Go package that provides a function to calculate the Levenshtein distance between two strings,the distance is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character

This implementation is optimized to use O(min(m,n)) space.It is based on the optimized C version found here

Install

To install this package, use go get:

go get github.com/Darklabel91/Levenshtein

Usage

To use this package in your Go program, import it and call the Distance function with two strings:

package main

import (
	"fmt"
	"github.com/Darklabel91/Levenshtein"
)

func main() {
    str1 := "kitten"
    str2 := "sitting"
    dist := levenshtein.Distance(str1, str2)
    fmt.Printf("The Levenshtein distance between %q and %q is %d\n", str1, str2, dist)
}

This will be the output:

The Levenshtein distance between "kitten" and "sitting" is 3

Testing

This package includes a test file levenshtein_test.go that tests the Distance function with various inputs. To run the tests, use go test:

go test

Contributing

Contributions are welcome! If you find a bug or would like to add a feature, please open an issue or pull request.