levenshtein/README.md
2023-10-09 19:13:11 +02:00

1.1 KiB

Levenshtein

A simple implementation of the Levenshtein distance in Go. It can currently only be used to determine the most likely choice from a hash table of options. In other words, you want a value to a key, but may not know the exact key.

Usage

import (
	"git.0x0001f346.de/andreas/levenshtein"
)

nationalCapitals := map[string]string{
    "Copenhagen": "Denmark",
    "Berlin":     "Germany",
    "Tokyo":      "Japan",
    "Amsterdam":  "Netherlands",
    "Bern":       "Switzerland",
    "Vienna":     "Austria",
    "Prague":     "Czech Republic",
    "Luxembourg": "Luxembourg",
    "Warsaw":     "Poland",
    "Paris":      "France",
    "Rome":       "Italy",
}

// A Ratio of 1.0 means that it is an exact match. The closer the Ratio approaches 1.0, the smaller the difference between the two keys, or the more likely we found a match.
bestFittingChoice := levenshtein.GetBestFittingChoice("Berlin", nationalCapitals) // {Ratio:1.0 Key:"Berlin" Value:"Germany"}
bestFittingChoice = levenshtein.GetBestFittingChoice("Tokio", nationalCapitals)   // {Ratio:0.8 Key:"Tokyo" Value:"Japan"}