package glossary import "strings" // Identity identifies an entry within its configured collection. type Identity struct { Collection string ID string } // Term retains every entry that contributes a recognized spelling. type Term struct { Spelling string Identities []Identity } // Vocabulary derives recognized spellings in selection order. Excluded entries // remove matching spellings regardless of which protected entries supplied them. func Vocabulary(entries, excluded []Entry) []Term { exclusions := make(map[string]struct{}) for _, entry := range excluded { for _, spelling := range spellings(entry) { exclusions[strings.ToLower(spelling)] = struct{}{} } } indices := make(map[string]int) var terms []Term for _, entry := range entries { id := Identity{Collection: entry.Collection, ID: entry.ID} for _, spelling := range spellings(entry) { key := strings.ToLower(spelling) if _, excluded := exclusions[key]; excluded { continue } index, exists := indices[key] if !exists { index = len(terms) indices[key] = index terms = append(terms, Term{Spelling: spelling}) } identities := terms[index].Identities found := false for _, existing := range identities { if existing == id { found = true break } } if !found { terms[index].Identities = append(identities, id) } } } return terms } func spellings(entry Entry) []string { var result []string for _, value := range append([]string{entry.Name}, entry.Aliases...) { value = strings.TrimSpace(value) if value != "" { result = append(result, value, value+"s") } } if entry.Plural != nil { if plural := strings.TrimSpace(*entry.Plural); plural != "" { result = append(result, plural) } } return result }