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/allinone/textlab/v0"
gno = "0.9"
private = true

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186413
  max_deposit = "7000000ugnot"

textlab.gno

// Package textlab runs every string algorithm in p/moul over one input at
// once, so they can be compared rather than read about one at a time.
//
// Each of these has its own demo realm showing it alone. What none of those can
// show is how the answers differ for the SAME word, which is the only question
// a reader actually has: soundex and levenshtein both claim to tell you whether
// two words are alike, and they are measuring different things.
//
// soundex keys on the FIRST LETTER and then on consonant classes, so it is
// blind to how the rest is spelled. levenshtein counts edits and is blind to
// how it sounds. "knight" and "night" are the clean case: one edit apart (83%
// similar) and yet soundex calls them unrelated, K523 against N230, because
// the silent K changes the first character of the code. "robert" and "rupert"
// go the other way, identical codes at two edits apart. Measured 2026-09-23
// against the packages in this tree, not quoted from a textbook.
//
//	/r/nym-moultest123/x/allinone/textlab/v0:knight
//	/r/nym-moultest123/x/allinone/textlab/v0:knight/night   compares two
//
// Eight packages, no state, nothing to redeploy carefully: the input comes out
// of the render path and nothing is stored.
package textlab

import (
	"strconv"
	"strings"

	"gno.land/p/nym-moultest123/kit/ui/v0"
	"gno.land/p/nym-moultest123/md/v0"
	"gno.land/p/nym-moultest123/realmpath/v0"
	"gno.land/p/nym-moultest123/x/daily/levenshtein/v0"
	"gno.land/p/nym-moultest123/x/daily/piglatin/v0"
	"gno.land/p/nym-moultest123/x/daily/romannum/v0"
	"gno.land/p/nym-moultest123/x/daily/rot13/v0"
	"gno.land/p/nym-moultest123/x/daily/soundex/v0"
	"gno.land/r/nym-moultest123/config/v1"
)

const realmPath = "gno.land/r/nym-moultest123/x/allinone/textlab/v0"

// maxInput bounds what a render path can carry into the algorithms below.
// levenshtein.Matrix is O(len(a) * len(b)), so an unbounded pair of inputs is
// a way to make a query expensive for whoever serves it.
const maxInput = 64

// defaultWord is what the root view demonstrates, chosen because it is the
// textbook case where the two similarity measures disagree: "knight" and
// "night" sound identical to soundex and are one edit apart.
const defaultWord = "knight"

// Render takes its input from the path: one word, or two separated by "/".
func Render(path string) string {
	req := realmpath.Parse(path)

	a := strings.TrimSpace(req.PathPart(0))
	b := strings.TrimSpace(req.PathPart(1))
	if a == "" {
		a = defaultWord
	}

	var out strings.Builder
	out.WriteString(config.TopBlockFor(realmPath))
	out.WriteString(md.H1("allinone: textlab"))
	out.WriteString("\nEight packages over one input. ")
	out.WriteString(md.Link("source", config.MygnoscanFor(realmPath)))
	out.WriteString("\n\n")

	if tooLong(a) || tooLong(b) {
		out.WriteString(ui.Empty("input too long: max " + strconv.Itoa(maxInput) + " bytes"))
		out.WriteString("\n")
		out.WriteString(config.BottomBlockFor(realmPath))
		return out.String()
	}

	out.WriteString(renderOne(a))
	if b != "" {
		out.WriteString("\n")
		out.WriteString(renderPair(a, b))
	} else {
		out.WriteString("\n")
		out.WriteString(renderTry())
	}

	out.WriteString(config.BottomBlockFor(realmPath))
	return out.String()
}

func tooLong(s string) bool { return len(s) > maxInput }

// renderOne is every single-input algorithm, one row each.
//
// ui.Cell, not ui.Inline: these land in table cells, where an unescaped pipe
// would silently eat a column. The input is whatever someone put in a URL.
func renderOne(word string) string {
	t := ui.NewTable("package", "answer")
	t.Row("`x/daily/soundex`", ui.Cell(soundex.Encode(word)))
	t.Row("`x/daily/rot13`", ui.Cell(rot13.Rot13(word)))
	t.Row("`x/daily/piglatin`", ui.Cell(piglatin.Translate(word)))
	t.Row("`x/daily/romannum`", ui.Cell(romanOf(word)))

	return md.H2("Input: "+ui.Inline(word)) + "\n" + t.String()
}

// romanOf shows the round trip both ways, because romannum is the one package
// here whose input is a number rather than a word.
//
// FromRoman PANICS on anything that is not a roman numeral rather than
// returning zero, and the input here comes out of a URL, so it is screened
// first. Without that screen the DEFAULT view aborts: "knight" is not a roman
// numeral, and neither is most of what anyone would type.
func romanOf(word string) string {
	if n, err := strconv.Atoi(word); err == nil {
		if n <= 0 || n > 3999 {
			return "out of range (1..3999)"
		}
		return romannum.ToRoman(n)
	}
	upper := strings.ToUpper(word)
	if !isRomanNumeral(upper) {
		return "not a number"
	}
	if n := romannum.FromRoman(upper); n > 0 {
		return strconv.Itoa(n) + " (read as roman)"
	}
	return "not a number"
}

// isRomanNumeral reports whether every byte is a roman digit. It is a screen
// for romanOf, not a validity check: "IIII" passes here and FromRoman decides.
func isRomanNumeral(s string) bool {
	if s == "" {
		return false
	}
	for i := 0; i < len(s); i++ {
		switch s[i] {
		case 'I', 'V', 'X', 'L', 'C', 'D', 'M':
		default:
			return false
		}
	}
	return true
}

// renderPair is the comparison, and the reason this realm exists: the two
// similarity measures answer different questions, and a reader should watch
// them disagree rather than be told they might.
func renderPair(a, b string) string {
	sa, sb := soundex.Encode(a), soundex.Encode(b)

	t := ui.NewTable("measure", "answer")
	t.Row("`soundex` of "+ui.Cell(a), ui.Cell(sa))
	t.Row("`soundex` of "+ui.Cell(b), ui.Cell(sb))
	t.Row("sound alike", yesNo(soundex.Match(a, b)))
	t.Row("`levenshtein` distance", strconv.Itoa(levenshtein.Distance(a, b)))
	t.Row("`levenshtein` similarity", strconv.Itoa(levenshtein.Similarity(a, b))+"%")

	return md.H2("Compared with "+ui.Inline(b)) + "\n" + t.String()
}

func yesNo(b bool) string {
	if b {
		return "yes"
	}
	return "no"
}

func renderTry() string {
	return md.H2("Try two") + "\n" +
		md.BulletList([]string{
			md.Link("knight vs night", "/r/nym-moultest123/x/allinone/textlab/v0:knight/night") +
				" (one edit apart, yet soundex says unrelated)",
			md.Link("robert vs rupert", "/r/nym-moultest123/x/allinone/textlab/v0:robert/rupert") +
				" (two edits apart, yet soundex says identical)",
			md.Link("1987 as a roman numeral", "/r/nym-moultest123/x/allinone/textlab/v0:1987"),
		})
}

textlab_test.gno

package textlab

import (
	"testing"

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

// TestRomanOf covers the one package here whose input is a number rather than
// a word, in both directions plus the two ways it can be neither.
func TestRomanOf(t *testing.T) {
	tests := []struct{ in, want string }{
		{"1987", "MCMLXXXVII"},
		{"1", "I"},
		{"3999", "MMMCMXCIX"},
		{"0", "out of range (1..3999)"},
		{"-5", "out of range (1..3999)"},
		{"4000", "out of range (1..3999)"},
		{"MCMLXXXVII", "1987 (read as roman)"},
		{"mcmlxxxvii", "1987 (read as roman)"},
		{"knight", "not a number"},
	}
	for _, tt := range tests {
		uassert.Equal(t, tt.want, romanOf(tt.in), "romanOf("+tt.in+")")
	}
}

// TestTheDisagreement is the claim the realm is built to show, pinned in both
// directions. Measured 2026-09-23; the numbers in the package doc and on the
// page come from here, so if either package changes its mind this fails rather
// than the page quietly stating something false.
func TestTheDisagreement(t *testing.T) {
	// One edit apart, and yet soundex says unrelated: it keys on the first
	// letter, and the silent K changes it.
	uassert.Equal(t, "K523", soundex.Encode("knight"))
	uassert.Equal(t, "N230", soundex.Encode("night"))
	uassert.False(t, soundex.Match("knight", "night"), "different soundex codes")
	uassert.Equal(t, 1, levenshtein.Distance("knight", "night"))
	uassert.Equal(t, 83, levenshtein.Similarity("knight", "night"))

	// And the other way: two edits apart, identical codes.
	uassert.Equal(t, "R163", soundex.Encode("robert"))
	uassert.Equal(t, "R163", soundex.Encode("rupert"))
	uassert.True(t, soundex.Match("robert", "rupert"), "same soundex code")
	uassert.Equal(t, 2, levenshtein.Distance("robert", "rupert"))
}

// TestRomanOfDoesNotAbortOnOrdinaryWords is the defect this realm shipped with
// until a test found it: romannum.FromRoman PANICS on a non-numeral instead of
// returning zero, so the DEFAULT view aborted. "knight" is not a roman numeral,
// and neither is most of what anyone would put in the URL.
func TestRomanOfDoesNotAbortOnOrdinaryWords(t *testing.T) {
	for _, w := range []string{"knight", "night", "a|b", "", "hello world", "MMM!"} {
		uassert.Equal(t, "not a number", romanOf(w), "romanOf("+w+")")
	}
	uassert.True(t, isRomanNumeral("MCMLXXXVII"))
	uassert.False(t, isRomanNumeral("KNIGHT"))
	uassert.False(t, isRomanNumeral(""))
}

// TestInputIsBounded pins the ceiling. levenshtein.Matrix is O(len(a)*len(b)),
// so an unbounded pair out of a URL is a way to make a query expensive for
// whoever serves it.
func TestInputIsBounded(t *testing.T) {
	long := ""
	for i := 0; i < maxInput+1; i++ {
		long += "x"
	}
	uassert.True(t, tooLong(long))
	uassert.False(t, tooLong("knight"))

	out := Render(long)
	uassert.True(t, len(out) < 600, "an over-long input renders a refusal, not a matrix")
}

// TestTableCellsAreEscaped: the input comes out of a URL and lands in a table
// cell, where an unescaped pipe eats a column.
func TestTableCellsAreEscaped(t *testing.T) {
	out := Render("a|b")
	uassert.False(t, contains(out, "| a|b |"), "a raw pipe reached a table cell")
}

func contains(h, n string) bool {
	for i := 0; i+len(n) <= len(h); i++ {
		if h[i:i+len(n)] == n {
			return true
		}
	}
	return false
}

// ExampleRender pins the root view, which is the default word and the "try
// two" list. Nothing here moves with the chain.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # allinone: textlab
	//
	// Eight packages over one input. [source](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/x/allinone/textlab/v0)
	//
	// ## Input: knight
	//
	// | package | answer |
	// | --- | --- |
	// | `x/daily/soundex` | K523 |
	// | `x/daily/rot13` | xavtug |
	// | `x/daily/piglatin` | ightknay |
	// | `x/daily/romannum` | not a number |
	//
	// ## Try two
	//
	// - [knight vs night](/r/nym-moultest123/x/allinone/textlab/v0:knight/night) (one edit apart, yet soundex says unrelated)
	// - [robert vs rupert](/r/nym-moultest123/x/allinone/textlab/v0:robert/rupert) (two edits apart, yet soundex says identical)
	// - [1987 as a roman numeral](/r/nym-moultest123/x/allinone/textlab/v0:1987)
}

// ExampleRender_pair pins the comparison view, which is the realm's whole
// argument: the same pair, judged opposite ways by the two measures.
func ExampleRender_pair() {
	print(Render("knight/night"))
	// Output:
	// # allinone: textlab
	//
	// Eight packages over one input. [source](https://mygnoscan.moul.p2p.team/realm/r/nym-moultest123/x/allinone/textlab/v0)
	//
	// ## Input: knight
	//
	// | package | answer |
	// | --- | --- |
	// | `x/daily/soundex` | K523 |
	// | `x/daily/rot13` | xavtug |
	// | `x/daily/piglatin` | ightknay |
	// | `x/daily/romannum` | not a number |
	//
	// ## Compared with night
	//
	// | measure | answer |
	// | --- | --- |
	// | `soundex` of knight | K523 |
	// | `soundex` of night | N230 |
	// | sound alike | no |
	// | `levenshtein` distance | 1 |
	// | `levenshtein` similarity | 83% |
}