WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package metric
|
||||
|
||||
import (
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// MAPMetric calculates Mean Average Precision for retrieval evaluation
|
||||
type MAPMetric struct{}
|
||||
|
||||
// NewMAPMetric creates a new MAPMetric instance
|
||||
func NewMAPMetric() *MAPMetric {
|
||||
return &MAPMetric{}
|
||||
}
|
||||
|
||||
// Compute calculates the Mean Average Precision score
|
||||
func (m *MAPMetric) Compute(metricInput *types.MetricInput) float64 {
|
||||
// Convert ground truth to sets for efficient lookup
|
||||
gts := metricInput.RetrievalGT
|
||||
ids := metricInput.RetrievalIDs
|
||||
|
||||
// Create sets of relevant document IDs for each query
|
||||
gtSets := make([]map[int]struct{}, len(gts))
|
||||
for i, gt := range gts {
|
||||
gtSets[i] = make(map[int]struct{})
|
||||
for _, docID := range gt {
|
||||
gtSets[i][docID] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
var apSum float64 // Sum of average precision for all queries
|
||||
|
||||
// Calculate average precision for each query
|
||||
for _, gtSet := range gtSets {
|
||||
// Mark which predicted documents are relevant
|
||||
predHits := make([]bool, len(ids))
|
||||
for i, predID := range ids {
|
||||
if _, ok := gtSet[predID]; ok {
|
||||
predHits[i] = true
|
||||
} else {
|
||||
predHits[i] = false
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
ap float64 // Average precision for current query
|
||||
hitCount int // Number of relevant documents found
|
||||
)
|
||||
|
||||
// Calculate precision at each rank position
|
||||
for k := 0; k < len(predHits); k++ {
|
||||
if predHits[k] {
|
||||
hitCount++
|
||||
// Precision at k: relevant docs found up to k / k
|
||||
ap += float64(hitCount) / float64(k+1)
|
||||
}
|
||||
}
|
||||
// Normalize by number of relevant documents
|
||||
if hitCount > 0 {
|
||||
ap /= float64(hitCount)
|
||||
}
|
||||
apSum += ap
|
||||
}
|
||||
|
||||
// Handle case with no ground truth
|
||||
if len(gtSets) == 0 {
|
||||
return 0
|
||||
}
|
||||
// Return mean of average precision across all queries
|
||||
return apSum / float64(len(gtSets))
|
||||
}
|
||||
Reference in New Issue
Block a user