| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-07-26 18:48:40 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-07-26 19:07:08 UTC |
| parent | 4eb65ed09a77425400d4f1611fc5c30ca9c043f8 |
| ipa.go | +33 | -26 |
diff --git a/ipa.go b/ipa.go index 5d4ec66..cf79981 100644 --- a/ipa.go +++ b/ipa.go @@ -1,8 +1,7 @@ package main import ( - "bufio" - "embed" + _ "embed" "fmt" "strings" @@ -12,9 +11,26 @@ import ( // We embed the IPA dictionaries for convenience, and parse them internally. // They come from the open-dict-data project, and we use the tab-delimited // text format for convenience. +// +// We embed them as strings (and not as an embed.FS) because then the words +// and pronunciations we extract from them are substrings of the binary's +// read-only data, and the dictionaries below don't need a copy of their own. +var ( + //go:embed ipa/en_US.txt + dict_en_US string + + //go:embed ipa/es_MX.txt + dict_es_MX string +) -//go:embed ipa/*.txt -var ipaFS embed.FS +// The embedded dictionary files, in the order in which they are matched. +var ipaFiles = []struct { + lang string + contents string +}{ + {"en_US", dict_en_US}, + {"es_MX", dict_es_MX}, +} // Map of word -> pronunciation (as IPA symbols). type IPADict map[string]string @@ -56,20 +72,16 @@ var namesIPA = map[string]string{ } func init() { - // We load all the IPA dictionaries from the embedded filesystem. + // We load all the IPA dictionaries from the embedded files. // This is wildly inefficient if we only use it once, but considering // the size of the dictionaries, it's not a problem, and we are optimizing // for repeated use. - des, err := ipaFS.ReadDir("ipa") - if err != nil { - panic(err) - } langs := []language.Tag{} - for i, de := range des { - langS := strings.TrimSuffix(de.Name(), ".txt") - lang := language.MustParse(langS) - dict := IPADict{} - langs = append(langs, lang) + for i, f := range ipaFiles { + // Pre-size the map to the number of lines, to avoid growing it (and + // leaving the discarded intermediate tables behind) as we fill it in. + dict := make(IPADict, strings.Count(f.contents, "\n")+len(namesIPA)) + langs = append(langs, language.MustParse(f.lang)) // The order in which we add it to langs identifies this dictionary. // The matcher will return this index when doing a match. @@ -77,26 +89,21 @@ func init() { // Scan line by line. Format is: // word<TAB>/pronunciation1/, /pronunciation2/, ... - f, err := ipaFS.Open("ipa/" + de.Name()) - if err != nil { - panic(err) - } - scanner := bufio.NewScanner(f) - for scanner.Scan() { - line := scanner.Text() + for line := range strings.Lines(f.contents) { word, prons, ok := strings.Cut(line, "\t") - if !ok || word == "" || prons == "" { + if !ok || word == "" { continue } // We keep the first pronunciation, as for our use case we only - // need one. - slspl := strings.Split(prons, "/") - if len(slspl) < 2 { + // need one. prons is "/pron1/, /pron2/, ...". + _, rest, _ := strings.Cut(prons, "/") + ipa, _, ok := strings.Cut(rest, "/") + if !ok { continue } // TODO: clean up the symbols, remove accents, etc. - dict[word] = strings.TrimSpace(slspl[1]) + dict[word] = strings.TrimSpace(ipa) } // Add the namesIPA to all dictionaries.