package glossary import "strings" // Identity identifies an entry within its configured collection. type Identity struct { Collection string ID string } // Term is a recognized spelling and its protected identities, if any. type Term struct { Spelling string Identities []Identity Exempt bool } // Vocabulary derives recognized spellings in selection order. Active spellings // are exempt from protection but still participate in longest matching. func Vocabulary(entries, excluded []Entry) []Term { exclusions := make(map[string]struct{}) var exempt []Term for _, entry := range excluded { for _, spelling := range spellings(entry) { key := strings.ToLower(spelling) if _, exists := exclusions[key]; !exists { exclusions[key] = struct{}{} exempt = append(exempt, Term{Spelling: spelling, Exempt: true}) } } } type contribution struct { spelling string identity Identity } seen := make(map[contribution]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 } member := contribution{spelling: key, identity: id} if _, exists := seen[member]; exists { continue } seen[member] = struct{}{} index, exists := indices[key] if !exists { index = len(terms) indices[key] = index terms = append(terms, Term{Spelling: spelling}) } terms[index].Identities = append(terms[index].Identities, id) } } return append(terms, exempt...) } 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 }