my-homelab-configs/cmd/nextpath/internal/career/catalog.go

106 lines
3.4 KiB
Go

// Package career maps an interest result to occupations worth exploring. It
// does not contain labor-market demand or employability calculations.
package career
import (
_ "embed"
"encoding/json"
"fmt"
"sort"
"my-homelab-configs/cmd/nextpath/internal/assessment"
)
//go:embed occupations.json
var defaultCatalogJSON []byte
type Occupation struct {
ONETSOC string `json:"onet_soc"`
Title string `json:"title"`
InterestOrder []assessment.Dimension `json:"interest_order"`
Summary string `json:"summary"`
TryFirst string `json:"try_first"`
SourceURL string `json:"source_url"`
}
type Catalog struct {
Version string `json:"version"`
Occupations []Occupation `json:"occupations"`
}
type Match struct {
Occupation Occupation `json:"occupation"`
FitScore int `json:"fit_score"`
Reason string `json:"reason"`
Notice string `json:"notice"`
}
func DefaultCatalog() (Catalog, error) {
var catalog Catalog
if err := json.Unmarshal(defaultCatalogJSON, &catalog); err != nil {
return Catalog{}, fmt.Errorf("decode embedded occupation catalog: %w", err)
}
if catalog.Version == "" || len(catalog.Occupations) == 0 {
return Catalog{}, fmt.Errorf("embedded occupation catalog is empty")
}
return catalog, nil
}
// Rank returns deterministic personal-fit matches. FitScore is only an
// interest-alignment score and must never be displayed as employability.
func Rank(result assessment.Result, catalog Catalog, limit int) []Match {
if limit <= 0 {
return nil
}
percent := make(map[assessment.Dimension]int, len(result.Scores))
for _, score := range result.Scores {
percent[score.Dimension] = score.Percent
}
weights := []int{50, 30, 20}
matches := make([]Match, 0, len(catalog.Occupations))
for _, occupation := range catalog.Occupations {
weighted := 0
weightTotal := 0
for index, dimension := range occupation.InterestOrder {
if index >= len(weights) {
break
}
weighted += percent[dimension] * weights[index]
weightTotal += weights[index]
}
fit := 0
if weightTotal > 0 {
fit = weighted / weightTotal
}
matches = append(matches, Match{
Occupation: occupation,
FitScore: fit,
Reason: reason(occupation.InterestOrder, percent),
Notice: "Personal-interest alignment only; Arizona demand, wages, education access, and job evidence are not yet included.",
})
}
sort.SliceStable(matches, func(i, j int) bool {
if matches[i].FitScore == matches[j].FitScore {
return matches[i].Occupation.Title < matches[j].Occupation.Title
}
return matches[i].FitScore > matches[j].FitScore
})
if limit < len(matches) {
matches = matches[:limit]
}
return matches
}
func reason(order []assessment.Dimension, percent map[assessment.Dimension]int) string {
if len(order) == 0 {
return "The catalog does not yet contain an interest profile for this occupation."
}
labels := map[assessment.Dimension]string{
assessment.Realistic: "hands-on", assessment.Investigative: "analytical",
assessment.Artistic: "creative", assessment.Social: "helping and teaching",
assessment.Enterprising: "leading and persuading", assessment.Conventional: "organizing and verifying",
}
primary := order[0]
return fmt.Sprintf("This work emphasizes %s activities; your current %s interest score is %d%%.", labels[primary], labels[primary], percent[primary])
}