feat(search): add exact company aliases
Embed a versioned local alias overlay and rank exact aliases before legal-name matches and word prefixes. Normalize Unicode consistently, deduplicate by company identity before limiting, and update bilingual search guidance and tests. Refs #24 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@ -28,6 +28,7 @@ type CompanyCatalog interface {
|
||||
var ErrUnknownCompany = errors.New("unknown company")
|
||||
|
||||
type Catalog struct {
|
||||
aliases map[string]map[string]bool
|
||||
companies []Company
|
||||
byID map[string]Company
|
||||
}
|
||||
@ -40,6 +41,9 @@ var embeddedManifest []byte
|
||||
|
||||
//go:embed data/FINANCEDATABASE_LICENSE
|
||||
var embeddedLicense []byte
|
||||
|
||||
//go:embed data/aliases.v1.json
|
||||
var embeddedAliases []byte
|
||||
var defaultOnce sync.Once
|
||||
var defaultCatalog *Catalog
|
||||
var defaultError error
|
||||
@ -58,6 +62,12 @@ func Default() (*Catalog, error) {
|
||||
return
|
||||
}
|
||||
defaultCatalog, defaultError = readCatalog(bytes.NewReader(embeddedSnapshot))
|
||||
if defaultError == nil {
|
||||
defaultError = defaultCatalog.loadAliases(embeddedAliases)
|
||||
}
|
||||
if defaultError != nil {
|
||||
defaultCatalog = nil
|
||||
}
|
||||
})
|
||||
return defaultCatalog, defaultError
|
||||
}
|
||||
@ -140,26 +150,6 @@ func readCatalog(input io.Reader) (*Catalog, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Name search folds case/spacing only: punctuation from a derivative ticker
|
||||
// (for example ES=F) must not become unrelated fragments of company names.
|
||||
func normalizeName(s string) string { return strings.Join(strings.Fields(strings.ToLower(s)), " ") }
|
||||
|
||||
func (c *Catalog) SearchCompanies(query string, limit int) ([]Company, error) {
|
||||
query = normalizeName(query)
|
||||
if query == "" || limit < 1 || limit > 20 {
|
||||
return nil, errors.New("invalid company search")
|
||||
}
|
||||
out := make([]Company, 0, limit)
|
||||
for _, company := range c.companies {
|
||||
if strings.Contains(normalizeName(company.Name), query) {
|
||||
out = append(out, company)
|
||||
if len(out) == limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (c *Catalog) ResolveCompany(id string) (Company, error) {
|
||||
if company, ok := c.byID[id]; ok {
|
||||
return company, nil
|
||||
|
||||
Reference in New Issue
Block a user