Keep match boundaries for exempt glossary names
This commit is contained in:
@@ -8,19 +8,25 @@ type Identity struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
// Term retains every entry that contributes a recognized spelling.
|
||||
// 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. Excluded entries
|
||||
// remove matching spellings regardless of which protected entries supplied them.
|
||||
// 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) {
|
||||
exclusions[strings.ToLower(spelling)] = struct{}{}
|
||||
key := strings.ToLower(spelling)
|
||||
if _, exists := exclusions[key]; !exists {
|
||||
exclusions[key] = struct{}{}
|
||||
exempt = append(exempt, Term{Spelling: spelling, Exempt: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
indices := make(map[string]int)
|
||||
@@ -51,7 +57,7 @@ func Vocabulary(entries, excluded []Entry) []Term {
|
||||
}
|
||||
}
|
||||
}
|
||||
return terms
|
||||
return append(terms, exempt...)
|
||||
}
|
||||
|
||||
func spellings(entry Entry) []string {
|
||||
|
||||
@@ -17,12 +17,17 @@ func TestVocabularyKeepsContributorsAndExemptsOnlyActiveSpellings(t *testing.T)
|
||||
t.Fatalf("shared spelling contributors: %+v", full[0])
|
||||
}
|
||||
reduced := Vocabulary(protected, active)
|
||||
var got []string
|
||||
var remaining, exempt []string
|
||||
for _, term := range reduced {
|
||||
got = append(got, term.Spelling)
|
||||
if term.Exempt {
|
||||
exempt = append(exempt, term.Spelling)
|
||||
} else {
|
||||
remaining = append(remaining, term.Spelling)
|
||||
}
|
||||
}
|
||||
if !reflect.DeepEqual(got, []string{"Aegis", "Aegiss", "Aegides"}) {
|
||||
t.Fatalf("remaining spellings: %v", got)
|
||||
if !reflect.DeepEqual(remaining, []string{"Aegis", "Aegiss", "Aegides"}) ||
|
||||
!reflect.DeepEqual(exempt, []string{"Shield", "Shields", "Ward", "Wards"}) {
|
||||
t.Fatalf("protected: %v; exempt: %v", remaining, exempt)
|
||||
}
|
||||
if !reflect.DeepEqual(full, Vocabulary(protected, nil)) || protected[0].Aliases[0] != "Ward" {
|
||||
t.Fatal("deriving an exemption changed another view or its inputs")
|
||||
|
||||
Reference in New Issue
Block a user