Files
vigilcare-records/vigilcare-records-web/src/composables/useOcrFieldConfidence.ts
T
Trent 9811f2a2ed
CI / backend (push) Successful in 6m16s
CI / frontend (push) Failing after 1m4s
feature: Shared UX Primitives
2026-08-12 03:57:43 +08:00

56 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { type ComputedRef } from 'vue'
import type { OcrConfidenceMap } from '../types'
/** Design-doc §14: 95100% high, 8094% medium, <80% low */
export type OcrConfidenceLevel = 'high' | 'medium' | 'low'
export const OCR_HIGH_THRESHOLD = 0.95
export const OCR_MEDIUM_THRESHOLD = 0.8
export function confidenceToLevel(confidence: number): OcrConfidenceLevel {
if (confidence >= OCR_HIGH_THRESHOLD) return 'high'
if (confidence >= OCR_MEDIUM_THRESHOLD) return 'medium'
return 'low'
}
export function formatOcrBadgeLabel(confidence: number): string {
const pct = Math.round(confidence * 100)
return `OCR ${pct}%`
}
export function useOcrFieldConfidence(
ocrConfidence: ComputedRef<OcrConfidenceMap | null | undefined>,
) {
function getFieldConfidence(fieldPath: string): number | undefined {
if (!ocrConfidence.value) return undefined
return ocrConfidence.value.fieldConfidences[fieldPath]
}
function fieldConfidenceLevel(fieldPath: string): OcrConfidenceLevel | null {
const confidence = getFieldConfidence(fieldPath)
if (confidence === undefined) return null
return confidenceToLevel(confidence)
}
function fieldConfidenceLabel(fieldPath: string): string | null {
const confidence = getFieldConfidence(fieldPath)
if (confidence === undefined) return null
return formatOcrBadgeLabel(confidence)
}
function fieldConfidenceClass(fieldPath: string): string {
const level = fieldConfidenceLevel(fieldPath)
if (!level) return ''
if (level === 'high') return 'ocr-high'
if (level === 'medium') return 'ocr-medium'
return 'ocr-low'
}
return {
getFieldConfidence,
fieldConfidenceLevel,
fieldConfidenceLabel,
fieldConfidenceClass,
}
}