Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/soundexdemo/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186411
  max_deposit = "5000000ugnot"

render_example_test.gno

package soundexdemo

// ExampleRender pins the realm's root Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Soundex
	//
	// Phonetic keys for English names, demoing the [`p/moul/x/daily/soundex`](/p/nym-moultest123/x/daily/soundex/v0) library.
	//
	// | name | key |
	// |---|---|
	// | Robert | `R163` |
	// | Rupert | `R163` |
	// | Rubin | `R150` |
	// | Ashcraft | `A261` |
	// | Ashcroft | `A261` |
	// | Tymczak | `T522` |
	// | Pfister | `P236` |
	// | Honeyman | `H555` |
	//
	// ## Sounds alike
	//
	// - **R163**: Robert, Rupert
	// - **A261**: Ashcraft, Ashcroft
	//
	// > A *blocking* key for finding candidates, never proof that two names match.
}

// ExampleRender_noLetters pins the empty-key path: an input with no ASCII
// letters has no Soundex key at all, and must not be reported as matching.
func ExampleRender_noLetters() {
	print(Render("/12345"))
	// Output:
	// # Soundex
	//
	// Phonetic keys for English names, demoing the [`p/moul/x/daily/soundex`](/p/nym-moultest123/x/daily/soundex/v0) library.
	//
	// ## `12345`
	//
	// _No ASCII letters — no key._
}

soundexdemo.gno

// Package soundexdemo is a small gnoweb demo of the Soundex phonetic algorithm
// provided by the [p/moul/x/daily/soundex](/p/nym-moultest123/x/daily/soundex/v0) library:
// it groups a small name list by phonetic key so the "sounds alike" clusters
// are visible.
//
// It contains no phonetic logic of its own. Stateless, so Render is
// deterministic — names are grouped in first-seen order, never by iterating a
// map.
package soundexdemo

import (
	"strings"

	"gno.land/p/nym-moultest123/x/daily/soundex/v0"
)

// names include several deliberate near-collisions.
var names = []string{
	"Robert", "Rupert", "Rubin",
	"Ashcraft", "Ashcroft",
	"Tymczak", "Pfister", "Honeyman",
}

// Render renders the demo for gnoweb.
//
//	Render("")       / Render("/") -> the name table + clusters
//	Render("/<name>")              -> that name's key
func Render(path string) string {
	var b strings.Builder
	b.WriteString("# Soundex\n\n")
	b.WriteString("Phonetic keys for English names, demoing the ")
	b.WriteString("[`p/moul/x/daily/soundex`](/p/nym-moultest123/x/daily/soundex/v0) library.\n\n")

	if q := parseArg(path); q != "" {
		k := soundex.Encode(q)
		b.WriteString("## `")
		b.WriteString(q)
		b.WriteString("`\n\n")
		if k == "" {
			b.WriteString("_No ASCII letters — no key._\n")
		} else {
			b.WriteString("key: **")
			b.WriteString(k)
			b.WriteString("** (normalized: `")
			b.WriteString(soundex.Normalize(q))
			b.WriteString("`)\n")
		}
		return b.String()
	}

	b.WriteString("| name | key |\n|---|---|\n")
	for _, n := range names {
		b.WriteString("| ")
		b.WriteString(n)
		b.WriteString(" | `")
		b.WriteString(soundex.Encode(n))
		b.WriteString("` |\n")
	}

	// group by key, preserving first-seen order (never iterate a map)
	b.WriteString("\n## Sounds alike\n\n")
	keys := []string{}
	byKey := map[string][]string{}
	for _, n := range names {
		k := soundex.Encode(n)
		if _, seen := byKey[k]; !seen {
			keys = append(keys, k)
		}
		byKey[k] = append(byKey[k], n)
	}
	for _, k := range keys {
		if len(byKey[k]) < 2 {
			continue
		}
		b.WriteString("- **")
		b.WriteString(k)
		b.WriteString("**: ")
		b.WriteString(strings.Join(byKey[k], ", "))
		b.WriteString("\n")
	}

	b.WriteString("\n> A *blocking* key for finding candidates, never proof that two names match.\n")
	return b.String()
}

func parseArg(path string) string {
	s := strings.TrimSpace(path)
	s = strings.TrimPrefix(s, "/")
	if i := strings.IndexByte(s, '/'); i >= 0 {
		s = s[:i]
	}
	return s
}