levenshtein

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2019 License: MIT Imports: 2 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dist

func Dist(s1, s2 string) int

Dist is a convenience function for a levenshtein distance calculator with equal costs.

Types

type BytesFrom

type BytesFrom interface {
	Dist(to []byte, maxCost int) int
}

BytesFrom computes distance from one byte array to other byte arrays. Instances should not be used concurrently.

func FromBytes

func FromBytes(from []byte) BytesFrom

FromBytes return BytesFrom for a given bytes array

type D

type D interface {
	// Dist calculates levenshtein distance between two utf-8 encoded strings
	Dist(string, string) int
}

D is the levenshtein distance calculator interface

func New

func New(indel, sub int) D

New creates a new levenshtein distance calculator where indel is increment/deletion cost and sub is the substitution cost.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL