Playground
gnomod.toml
module = "gno.land/p/nym-moultest123/x/daily/levenshtein/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186405 max_deposit = "5000000ugnot"
levenshtein.gno
// Package levenshtein ports the classic Levenshtein edit-distance algorithm
// (as found in Go libraries like agext/levenshtein) to gno — as a reusable pure
// package.
//
// The core is the textbook dynamic-programming matrix: the minimum number of
// single-character insertions, deletions, or substitutions to turn string a
// into string b. It is fully rune-aware and pure (deterministic), so it runs
// happily on-chain.
//
// A live demo of this package (an interactive distance calculator with the DP
// table) is at [r/moul/x/daily/levenshteindemo](/r/nym-moultest123/x/daily/levenshteindemo/v0).
package levenshtein
// Distance returns the Levenshtein edit distance between a and b.
//
// It counts single-rune insertions, deletions, and substitutions and works on
// runes (not bytes), so multi-byte UTF-8 input is handled correctly. The
// classic two-row DP is used, so memory is O(min(len)) and time is O(len(a)*len(b)).
func Distance(a, b string) int {
ra := []rune(a)
rb := []rune(b)
// Keep the shorter slice as the inner (column) dimension.
if len(ra) < len(rb) {
ra, rb = rb, ra
}
n := len(ra)
m := len(rb)
if m == 0 {
return n
}
// prev[j] = distance between ra[:i] and rb[:j].
prev := make([]int, m+1)
for j := 0; j <= m; j++ {
prev[j] = j
}
curr := make([]int, m+1)
for i := 1; i <= n; i++ {
curr[0] = i
for j := 1; j <= m; j++ {
cost := 1
if ra[i-1] == rb[j-1] {
cost = 0
}
curr[j] = min3(
curr[j-1]+1, // insertion
prev[j]+1, // deletion
prev[j-1]+cost, // substitution / match
)
}
prev, curr = curr, prev
}
return prev[m]
}
// Matrix returns the full (len(a)+1) x (len(b)+1) DP matrix used by Distance.
// matrix[i][j] is the edit distance between the first i runes of a and the
// first j runes of b. The bottom-right cell equals Distance(a, b).
func Matrix(a, b string) [][]int {
ra := []rune(a)
rb := []rune(b)
n := len(ra)
m := len(rb)
d := make([][]int, n+1)
for i := 0; i <= n; i++ {
d[i] = make([]int, m+1)
d[i][0] = i
}
for j := 0; j <= m; j++ {
d[0][j] = j
}
for i := 1; i <= n; i++ {
for j := 1; j <= m; j++ {
cost := 1
if ra[i-1] == rb[j-1] {
cost = 0
}
d[i][j] = min3(d[i][j-1]+1, d[i-1][j]+1, d[i-1][j-1]+cost)
}
}
return d
}
// Similarity returns a 0..100 percentage of how similar a and b are, defined as
// (1 - distance/maxLen) * 100 rounded to the nearest integer. Two empty strings
// are considered 100% similar.
func Similarity(a, b string) int {
la := len([]rune(a))
lb := len([]rune(b))
maxLen := la
if lb > maxLen {
maxLen = lb
}
if maxLen == 0 {
return 100
}
dist := Distance(a, b)
// Rounded percentage of matching characters.
return ((maxLen-dist)*100 + maxLen/2) / maxLen
}
func min3(a, b, c int) int {
m := a
if b < m {
m = b
}
if c < m {
m = c
}
return m
}
levenshtein_test.gno
package levenshtein
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestDistance(t *testing.T) {
cases := []struct {
a, b string
want int
}{
{"", "", 0},
{"abc", "", 3},
{"", "abc", 3},
{"abc", "abc", 0},
{"kitten", "sitting", 3},
{"flaw", "lawn", 2},
{"sunday", "saturday", 3},
{"gno", "gnoland", 4},
{"book", "back", 2},
// Rune-aware: each accented char is one edit, not several bytes.
{"café", "cafe", 1},
{"héllo", "hello", 1},
}
for _, tc := range cases {
got := Distance(tc.a, tc.b)
uassert.Equal(t, tc.want, got)
// Symmetry: distance is the same in both directions.
uassert.Equal(t, tc.want, Distance(tc.b, tc.a))
}
}
func TestSimilarity(t *testing.T) {
// Identical strings.
uassert.Equal(t, 100, Similarity("hello", "hello"))
// Both empty.
uassert.Equal(t, 100, Similarity("", ""))
// Completely different lengths / content.
uassert.Equal(t, 0, Similarity("abc", "xyz"))
// kitten/sitting: max len 7, distance 3 -> round((4/7)*100) = 57.
uassert.Equal(t, 57, Similarity("kitten", "sitting"))
}
func TestMatrix(t *testing.T) {
d := Matrix("kitten", "sitting")
// Dimensions: (6+1) x (7+1).
uassert.Equal(t, 7, len(d))
uassert.Equal(t, 8, len(d[0]))
// First row and column are the identity ramps.
for j := 0; j <= 7; j++ {
uassert.Equal(t, j, d[0][j])
}
for i := 0; i <= 6; i++ {
uassert.Equal(t, i, d[i][0])
}
// Bottom-right cell equals Distance.
uassert.Equal(t, Distance("kitten", "sitting"), d[6][7])
}