levenshtein/README.md

31 lines
1.1 KiB
Markdown
Raw Permalink Normal View History

2023-10-09 19:13:11 +02:00
# Levenshtein
A simple implementation of the [Levenshtein distance](https://en.wikipedia.org/wiki/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
```go
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"}
```