spb/datacenterindex
Public
HTML 53.9%
TypeScript 44.5%
JavaScript 0.6%
SQL 0.5%
1import type { ConfidenceLevel, SourceKind } from "./types.js";23export const CONFIDENCE_RANK: Record<ConfidenceLevel, number> = { verified: 5, high: 4, moderate: 3, estimated: 2, unverified: 1 };45/** Base confidence by source category (before adjustments). */6export const SOURCE_KIND_BASE: Record<SourceKind, ConfidenceLevel> = {7 operator: "high",8 government: "high",9 filing: "high",10 utility: "high",11 cloud_provider: "high",12 registry: "high",13 dataset: "moderate",14 community: "moderate",15 secondary: "moderate",16 news: "moderate",17};1819export interface ConfidenceInput {20 sourceKind: SourceKind;21 sourcePriority?: number; // 1 (best) … 522 extractionCertainty?: number; // 0..1 (structured field = 1, regex on prose = 0.6, llm = 0.4)23 ageDays?: number;24 corroborations?: number; // number of independent sources agreeing25 conflicts?: number; // independent sources disagreeing26 isEstimate?: boolean;27}2829export function computeConfidence(i: ConfidenceInput): ConfidenceLevel {30 if (i.isEstimate) return "estimated";31 let score = CONFIDENCE_RANK[SOURCE_KIND_BASE[i.sourceKind]];32 const cert = i.extractionCertainty ?? 1;33 if (cert < 0.5) score -= 2;34 else if (cert < 0.8) score -= 1;35 if ((i.corroborations ?? 0) >= 2) score += 1;36 if ((i.conflicts ?? 0) >= 1) score -= 1;37 if ((i.ageDays ?? 0) > 730) score -= 1;38 if ((i.sourcePriority ?? 3) >= 5) score -= 1;39 score = Math.max(1, Math.min(5, score));40 // "verified" requires corroboration by ≥2 independent primary-grade sources41 if (score === 5 && !((i.corroborations ?? 0) >= 2 && cert >= 0.8)) score = 4;42 return (Object.keys(CONFIDENCE_RANK) as ConfidenceLevel[]).find((k) => CONFIDENCE_RANK[k] === score) ?? "unverified";43}4445export function bestConfidence(levels: ConfidenceLevel[]): ConfidenceLevel {46 return levels.reduce<ConfidenceLevel>((best, l) => (CONFIDENCE_RANK[l] > CONFIDENCE_RANK[best] ? l : best), "unverified");47}4849export const CONFIDENCE_LABEL: Record<ConfidenceLevel, string> = { verified: "Verified", high: "High confidence", moderate: "Moderate confidence", estimated: "Estimated", unverified: "Unverified" };50