Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

levenshtein source realm

Package levenshtein ports the classic Levenshtein edit-distance algorithm (as found in Go libraries like agext/levens...

Overview

Package levenshtein ports the classic Levenshtein edit-distance algorithm (as found in Go libraries like agext/levenshtein) to a gno.land realm.

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.

Functions 4

func Distance

Action
1func Distance(a, b string) int
source

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 Matrix

Action
1func Matrix(a, b string) [][]int
source

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 Render

1func Render(path string) string
source

Render implements the gnoweb view.

  • "" or "/" : explanation + examples.
  • "/<a>/<b>" : distance between a and b, with the DP table.

func Similarity

Action
1func Similarity(a, b string) int
source

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.

Imports 2

  • strconv stdlib
  • strings stdlib

Source Files 2