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/p/nym-moultest123/x/daily/soundex/v0"
gno = "0.9"

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

soundex.gno

// Package soundex implements the Soundex phonetic algorithm as a pure,
// reusable package: names that sound alike in English encode to the same
// four-character key, so "Robert" and "Rupert" both give R163.
//
// This is the 1918 Russell/Odell algorithm as used by the US census, with the
// rules that are usually got wrong implemented explicitly:
//
//   - the first letter is kept as-is and its digit still suppresses a
//     following consonant of the same code ("Pfister" → P236, not P123);
//   - 'h' and 'w' are TRANSPARENT: consonants either side of them are treated
//     as adjacent ("Ashcraft" → A261, not A226);
//   - vowels are not transparent — they separate, so a repeated code after a
//     vowel is emitted again ("Tymczak" → T522).
//
// Soundex is English-centric and lossy by design: it is a *blocking* key for
// finding candidates, never a proof that two names match.
//
// A live demo of this package is at
// [r/moul/x/daily/soundexdemo](/r/nym-moultest123/x/daily/soundexdemo/v0).
package soundex

import "strings"

// code returns the Soundex digit for a letter, or 0 for vowels and h/w/y.
func code(c byte) byte {
	switch c {
	case 'B', 'F', 'P', 'V':
		return '1'
	case 'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z':
		return '2'
	case 'D', 'T':
		return '3'
	case 'L':
		return '4'
	case 'M', 'N':
		return '5'
	case 'R':
		return '6'
	}
	return 0
}

// upper returns c upper-cased if it is an ASCII letter, else 0.
func upper(c byte) byte {
	if c >= 'a' && c <= 'z' {
		c -= 32
	}
	if c >= 'A' && c <= 'Z' {
		return c
	}
	return 0
}

// Encode returns the four-character Soundex key for s, or "" when s contains
// no ASCII letters. Non-letters are ignored, so "O'Brien" and "OBrien" agree.
func Encode(s string) string {
	// keep letters only
	letters := make([]byte, 0, len(s))
	for i := 0; i < len(s); i++ {
		if c := upper(s[i]); c != 0 {
			letters = append(letters, c)
		}
	}
	if len(letters) == 0 {
		return ""
	}

	out := []byte{letters[0]}
	prev := code(letters[0]) // the first letter's own code still suppresses

	for i := 1; i < len(letters) && len(out) < 4; i++ {
		c := letters[i]
		d := code(c)
		switch {
		case d == 0:
			// 'h' and 'w' are transparent: leave prev alone so the consonants
			// either side are treated as adjacent. Vowels (and 'y') separate,
			// so they reset prev and allow the same code to repeat.
			if c != 'H' && c != 'W' {
				prev = 0
			}
		case d != prev:
			out = append(out, d)
			prev = d
		default:
			// same code as the previous consonant, and not separated by a
			// vowel: collapsed.
		}
	}
	for len(out) < 4 {
		out = append(out, '0')
	}
	return string(out)
}

// Match reports whether two strings share a Soundex key. Empty keys never
// match, so two inputs with no letters are not "the same name".
func Match(a, b string) bool {
	ka, kb := Encode(a), Encode(b)
	return ka != "" && ka == kb
}

// EncodeAll returns the keys for each input, in order.
func EncodeAll(names []string) []string {
	out := make([]string, 0, len(names))
	for _, n := range names {
		out = append(out, Encode(n))
	}
	return out
}

// Normalize upper-cases and strips non-letters — the input Encode actually
// sees. Exposed so callers can show their work.
func Normalize(s string) string {
	var b strings.Builder
	for i := 0; i < len(s); i++ {
		if c := upper(s[i]); c != 0 {
			b.WriteByte(c)
		}
	}
	return b.String()
}

soundex_test.gno

package soundex

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// The canonical examples, including the three that catch a naive
// implementation: Pfister, Tymczak and Ashcraft.
func TestEncodeCanonical(t *testing.T) {
	cases := []struct{ in, want string }{
		{"Robert", "R163"},
		{"Rupert", "R163"},
		{"Rubin", "R150"},
		{"Ashcraft", "A261"}, // 'h' is transparent: s and c collapse
		{"Ashcroft", "A261"},
		{"Tymczak", "T522"}, // vowel separates, so 2 repeats
		{"Pfister", "P236"}, // first letter's code suppresses the f
		{"Honeyman", "H555"},
		{"Jackson", "J250"},
		{"Washington", "W252"},
		{"Lee", "L000"},
		{"Gutierrez", "G362"},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, Encode(c.in))
	}
}

// The first letter keeps its own code for suppression purposes — the rule most
// often missed. Without it "Pfister" would come out P123.
func TestFirstLetterSuppresses(t *testing.T) {
	uassert.Equal(t, "P236", Encode("Pfister"))
	uassert.Equal(t, "B000", Encode("Bb"))
}

// 'h'/'w' are transparent; vowels are not.
func TestTransparentHW(t *testing.T) {
	uassert.Equal(t, Encode("Ashcraft"), Encode("Ascraft"))
	// a vowel between the same two codes does NOT collapse them
	uassert.Equal(t, "T522", Encode("Tymczak"))
}

func TestCaseAndPunctuationIgnored(t *testing.T) {
	uassert.Equal(t, Encode("OBrien"), Encode("O'Brien"))
	uassert.Equal(t, Encode("robert"), Encode("ROBERT"))
	uassert.Equal(t, "R163", Encode("  Robert!  "))
}

func TestNoLetters(t *testing.T) {
	uassert.Equal(t, "", Encode(""))
	uassert.Equal(t, "", Encode("12345"))
	uassert.Equal(t, "", Encode("!!!"))
}

// Two inputs with no letters are not "the same name".
func TestMatch(t *testing.T) {
	uassert.True(t, Match("Robert", "Rupert"))
	uassert.False(t, Match("Robert", "Rubin"))
	uassert.False(t, Match("", ""))
	uassert.False(t, Match("123", "456"))
}

// Always exactly four characters, padded with zeros.
func TestAlwaysFourChars(t *testing.T) {
	for _, s := range []string{"A", "Lee", "Robert", "Washington", "Supercalifragilistic"} {
		uassert.Equal(t, 4, len(Encode(s)))
	}
}

func TestEncodeAllAndNormalize(t *testing.T) {
	got := EncodeAll([]string{"Robert", "Lee"})
	uassert.Equal(t, 2, len(got))
	uassert.Equal(t, "R163", got[0])
	uassert.Equal(t, "L000", got[1])
	uassert.Equal(t, "OBRIEN", Normalize("O'Brien"))
}