Files
ai_academy/internal/application/service/metric/recall.go
T
runny 7ec21f35b0 chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
2026-11-16 18:12:23 +08:00

42 lines
911 B
Go

package metric
import (
"academy/internal/types"
)
// RecallMetric calculates recall for retrieval evaluation
type RecallMetric struct{}
// NewRecallMetric creates a new RecallMetric instance
func NewRecallMetric() *RecallMetric {
return &RecallMetric{}
}
// Compute calculates the recall score
func (r *RecallMetric) Compute(metricInput *types.MetricInput) float64 {
// Get ground truth and predicted IDs
gts := metricInput.RetrievalGT
ids := metricInput.RetrievalIDs
// Convert ground truth to sets for efficient lookup
gtSets := SliceMap(gts, ToSet)
if len(gtSets) == 0 {
return 0.0
}
if len(ids) == 0 {
return 0.0
}
var totalRecall float64
for _, gtSet := range gtSets {
hits := Hit(ids, gtSet)
if len(gtSet) > 0 {
totalRecall += float64(hits) / float64(len(gtSet))
}
}
// Recall = average recall across all ground truth sets
return totalRecall / float64(len(gtSets))
}