159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package companycatalog
|
||
|
||
import (
|
||
"encoding/json"
|
||
"errors"
|
||
"sort"
|
||
"strings"
|
||
"unicode"
|
||
|
||
"golang.org/x/text/unicode/norm"
|
||
)
|
||
|
||
// Normalization is shared by aliases, legal names and queries. Periods in
|
||
// initials and apostrophes are ignored; other punctuation separates words.
|
||
// Diacritics fold away; original display names and opaque IDs never change.
|
||
func normalizeName(value string) string {
|
||
var out strings.Builder
|
||
for _, r := range norm.NFKD.String(strings.ToLower(value)) {
|
||
switch {
|
||
case unicode.Is(unicode.Mn, r):
|
||
continue
|
||
case unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsSymbol(r):
|
||
out.WriteRune(r)
|
||
case r == '.' || r == '\'' || r == '’':
|
||
continue
|
||
default:
|
||
out.WriteByte(' ')
|
||
}
|
||
}
|
||
return strings.Join(strings.Fields(out.String()), " ")
|
||
}
|
||
|
||
// loadAliases validates the whole overlay before publishing it. Alias targets
|
||
// must resolve in this snapshot; stale IDs never cause partial alias updates.
|
||
func (c *Catalog) loadAliases(data []byte) error {
|
||
var file struct {
|
||
Version int `json:"version"`
|
||
Aliases []struct {
|
||
Alias string `json:"alias"`
|
||
CompanyIDs []string `json:"company_ids"`
|
||
} `json:"aliases"`
|
||
}
|
||
if json.Unmarshal(data, &file) != nil || file.Version != 1 {
|
||
return errors.New("invalid company alias file")
|
||
}
|
||
aliases := map[string]map[string]bool{}
|
||
for _, entry := range file.Aliases {
|
||
key := normalizeName(entry.Alias)
|
||
if key == "" || len(entry.CompanyIDs) == 0 {
|
||
return errors.New("invalid company alias entry")
|
||
}
|
||
if aliases[key] == nil {
|
||
aliases[key] = map[string]bool{}
|
||
}
|
||
for _, id := range entry.CompanyIDs {
|
||
if _, ok := c.byID[id]; !ok {
|
||
return errors.New("unresolved company alias target")
|
||
}
|
||
aliases[key][id] = true
|
||
}
|
||
}
|
||
c.aliases = aliases
|
||
return nil
|
||
}
|
||
|
||
// Symbol lookup has its own exact normalization: punctuation, accents,
|
||
// internal whitespace and exchange suffixes are never folded or inferred.
|
||
func normalizeExactSymbol(value string) string { return strings.ToUpper(strings.TrimSpace(value)) }
|
||
|
||
func legalWordPrefix(name, query string) bool {
|
||
return strings.HasPrefix(name, query) || strings.Contains(name, " "+query)
|
||
}
|
||
|
||
func (c *Catalog) SearchCompanies(query string, limit int) ([]Company, error) {
|
||
if strings.TrimSpace(query) == "" || limit < 1 || limit > 20 {
|
||
return nil, errors.New("invalid company search")
|
||
}
|
||
symbol := normalizeExactSymbol(query)
|
||
query = normalizeName(query)
|
||
type match struct {
|
||
company Company
|
||
tier int
|
||
name string
|
||
}
|
||
matches := map[string]match{}
|
||
add := func(company Company, tier int) {
|
||
normalized := normalizeName(company.Name)
|
||
old, exists := matches[company.CompanyID]
|
||
if !exists || tier < old.tier || (tier == old.tier && normalized < old.name) {
|
||
matches[company.CompanyID] = match{company, tier, normalized}
|
||
}
|
||
}
|
||
for _, id := range c.symbolIDs[symbol] {
|
||
add(c.byID[id], 0)
|
||
}
|
||
for _, company := range c.companies {
|
||
if normalizeExactSymbol(company.YahooSymbol) == symbol {
|
||
add(company, 0)
|
||
}
|
||
}
|
||
if query != "" {
|
||
for id := range c.aliases[query] {
|
||
add(c.byID[id], 1)
|
||
}
|
||
for _, company := range c.companies {
|
||
name := normalizeName(company.Name)
|
||
if name == query {
|
||
add(company, 2)
|
||
} else if legalWordPrefix(name, query) {
|
||
add(company, 3)
|
||
}
|
||
}
|
||
}
|
||
|
||
ranked := make([]match, 0, len(matches))
|
||
for _, candidate := range matches {
|
||
ranked = append(ranked, candidate)
|
||
}
|
||
sort.Slice(ranked, func(i, j int) bool {
|
||
a, b := ranked[i], ranked[j]
|
||
if a.tier != b.tier {
|
||
return a.tier < b.tier
|
||
}
|
||
if a.name != b.name {
|
||
return a.name < b.name
|
||
}
|
||
return a.company.CompanyID < b.company.CompanyID
|
||
})
|
||
if len(ranked) > limit {
|
||
ranked = ranked[:limit]
|
||
}
|
||
out := make([]Company, 0, len(ranked))
|
||
for _, candidate := range ranked {
|
||
out = append(out, candidate.company)
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// MentionNames is an inverse lookup by resolved identity, never a fuzzy search.
|
||
// Ambiguous alias targets are excluded from deterministic article acceptance.
|
||
func (c *Catalog) MentionNames(id string) ([]string, error) {
|
||
if c == nil {
|
||
return nil, ErrUnknownCompany
|
||
}
|
||
company, ok := c.byID[id]
|
||
if !ok {
|
||
return nil, ErrUnknownCompany
|
||
}
|
||
names := []string{company.Name}
|
||
aliases := []string{}
|
||
for alias, ids := range c.aliases {
|
||
if len(ids) == 1 && ids[id] {
|
||
aliases = append(aliases, alias)
|
||
}
|
||
}
|
||
sort.Strings(aliases)
|
||
return append(names, aliases...), nil
|
||
}
|