| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-07-26 18:22:05 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-07-26 19:01:29 UTC |
| parent | e564b02a635a30824ab2a0db1ab338e372fd7c67 |
| ipa.go | +2 | -2 |
| ipa_test.go | +98 | -0 |
| svg.go | +1 | -1 |
diff --git a/ipa.go b/ipa.go index c4770f0..5d4ec66 100644 --- a/ipa.go +++ b/ipa.go @@ -343,14 +343,14 @@ func mapSyllables(glyphs []Glyph, slashes []int, wlen int) Word { return word } -// smartWordToGlyphs converts a string word to a glyph Word. +// SmartWordToGlyphs converts a string word to a glyph Word. // If the word has a dictionary prefix (e.g. "en:shadow"), we use that. // Otherwise, we search through some known IPA dictionaries to try to find it. // And if that fails, we assume the word is a sequence of phonemes // (e.g. "SH-fEEt-R-All"). // Syllables are separated by "/", and when doing IPA conversion we do a // best-effort heuristic mapping. -func smartWordToGlyphs(word string) (Word, error) { +func SmartWordToGlyphs(word string) (Word, error) { if lang, w, ok := strings.Cut(word, ":"); ok { if lang == "" || lang == "firstones" { return phonemesToGlyphs(w) diff --git a/ipa_test.go b/ipa_test.go index 3fc73d7..7282402 100644 --- a/ipa_test.go +++ b/ipa_test.go @@ -1,6 +1,7 @@ package main import ( + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -114,3 +115,100 @@ func TestMapSyllables(t *testing.T) { } } } + +// TestLookups checks that words are resolved consistently, regardless of +// which source they come from (namesIPA or one of the dictionaries), how they +// are spelled, and how the language is selected. +func TestLookups(t *testing.T) { + cases := []struct { + word string + // Language prefixes that must all resolve to `want`. The empty string + // means no prefix, so the language is detected automatically. + langs []string + want string + // Language prefixes that must not resolve at all. + fails []string + }{ + // namesIPA entries are hand-curated, and reachable from every + // supported language. + { + word: "catra", + langs: []string{"", "en", "en-US", "en-GB", "es", "es-MX"}, + want: "K-sAd-T-R-sAd", + }, { + word: "she-ra", + langs: []string{"", "en", "es"}, + want: "SH-fEEt-R-All", + }, { + // Also in en_US, as /ˈbaʊ/ (B-hOUse); the name must win. + word: "bow", + langs: []string{"", "en", "es"}, + want: "B-gO", + }, { + // Same, but shadowing an es_MX entry (/sea/, S-sAy-sAd). + word: "sea", + langs: []string{"", "en", "es"}, + want: "S-fEEt", + }, + + // Dictionary entries: only available in their own language, and + // picked up by automatic detection. + { + word: "hello", + langs: []string{"", "en", "en-US", "en-GB"}, + want: "H-fUn-L-gO", + fails: []string{"es", "es-MX", "fr"}, + }, { + word: "gato", + langs: []string{"", "es", "es-MX", "es-ES"}, + want: "G-sAd-T-All", + fails: []string{"en", "en-US", "fr"}, + }, + + // Words in both dictionaries keep their per-language pronunciation. + // Automatic detection prefers es, as it is tried first. + { + word: "casa", + langs: []string{"", "es", "es-MX"}, + want: "K-sAd-S-sAd", + }, { + word: "casa", + langs: []string{"en", "en-US"}, + want: "K-All-S-fUn", + }, + } + + withLang := func(lang, word string) string { + if lang == "" { + return word + } + return lang + ":" + word + } + + for _, c := range cases { + // Lookups are case-insensitive, so every spelling must behave the + // same way. + for _, w := range []string{ + c.word, + strings.ToUpper(c.word[:1]) + c.word[1:], + strings.ToUpper(c.word), + } { + for _, lang := range c.langs { + in := withLang(lang, w) + got, err := SmartWordToGlyphs(in) + switch { + case err != nil: + t.Errorf("%q: unexpected error: %v", in, err) + case got.String() != c.want: + t.Errorf("%q: got %q, want %q", in, got, c.want) + } + } + for _, lang := range c.fails { + in := withLang(lang, w) + if got, err := SmartWordToGlyphs(in); err == nil { + t.Errorf("%q: expected an error, got %q", in, got) + } + } + } + } +} diff --git a/svg.go b/svg.go index 6830ff2..4be1d85 100644 --- a/svg.go +++ b/svg.go @@ -287,7 +287,7 @@ func wordsToSVG(words []string) (SVG, int, int, error) { // wordsG contains the words, as syllables of Glyphs. wordsG := []Word{} for _, word := range words { - wordG, err := smartWordToGlyphs(word) + wordG, err := SmartWordToGlyphs(word) if err != nil { return svg, 0, 0, fmt.Errorf( "error converting %q to glyphs: %v", word, err)