All files / scripts statistical-claims-detector.ts

82.25% Statements 51/62
72.88% Branches 43/59
100% Functions 7/7
90.74% Lines 49/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507                                                                                                                                                                                                3x                                                                                                                                                                                                                   3x                                                                                                                                                                                                                       16x 1x     15x 15x   15x   210x     210x 17x   17x 17x 17x       17x 17x 17x 17x       17x   17x                           15x                           8x 2x           6x   6x 3x   3x 1x   2x 1x   1x                                       8x   8x 2x         6x 1x         5x           8x   8x                 8x   8x 1x     4x                           9x 9x                     18x 18x                                 122x                     17x 17x              
/**
 * @module Intelligence/StatisticalClaimsDetector
 * @description Detects and fact-checks statistical claims made by politicians
 * in parliamentary speeches and debates. Cross-references claimed figures
 * against official data from World Bank and SCB (Statistics Sweden) MCP servers.
 *
 * This enables breaking-news or commentary articles when politicians use
 * statistics to validate policy claims — surfacing whether the data supports
 * or contradicts their assertions.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type { Language } from './types/language.js';
 
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
 
/** A statistical claim extracted from a speech or document */
export interface StatisticalClaim {
  /** The original text excerpt containing the claim */
  readonly sourceText: string;
  /** Detected statistical topic (e.g., 'unemployment', 'gdp', 'migration') */
  readonly topic: string;
  /** Claimed numeric value (if extractable) */
  readonly claimedValue?: number;
  /** Unit of the claimed value (e.g., 'percent', 'billions SEK') */
  readonly claimedUnit?: string;
  /** Person making the claim */
  readonly speaker?: string;
  /** Party affiliation */
  readonly party?: string;
  /** Data source for verification ('world-bank' | 'scb' | 'both') */
  readonly verificationSource: 'world-bank' | 'scb' | 'both';
  /** World Bank indicator ID for cross-reference (if applicable) */
  readonly worldBankIndicator?: string;
  /** SCB table ID for cross-reference (if applicable) */
  readonly scbTableId?: string;
}
 
/** Result of fact-checking a statistical claim */
export interface FactCheckResult {
  /** The original claim */
  readonly claim: StatisticalClaim;
  /** Official value from data source */
  readonly officialValue?: number;
  /** Unit of the official value */
  readonly officialUnit?: string;
  /** Period of the official data */
  readonly officialPeriod?: string;
  /** Verdict on the claim */
  readonly verdict: ClaimVerdict;
  /** Percentage deviation from official data (if both values available) */
  readonly deviationPercent?: number;
  /** Human-readable explanation */
  readonly explanation: string;
  /** Data source used for verification */
  readonly dataSource: string;
}
 
/** Possible verdicts for a statistical claim */
export type ClaimVerdict =
  | 'accurate'          // Claim matches official data (within 5%)
  | 'mostly-accurate'   // Claim is close but not exact (5-15% deviation)
  | 'misleading'        // Claim uses real data but in a misleading way (15-30%)
  | 'inaccurate'        // Claim significantly deviates from official data (>30%)
  | 'unverifiable'      // Cannot be verified against available data
  | 'outdated';         // Claim uses old data when newer is available
 
/** Localized headings for fact-check sections in articles */
export interface FactCheckHeadings {
  readonly factCheck: string;
  readonly claimVsReality: string;
  readonly verdict: string;
  readonly dataSource: string;
}
 
// ---------------------------------------------------------------------------
// Claim detection patterns
// ---------------------------------------------------------------------------
 
/** Pattern definitions for detecting statistical claims in text */
interface ClaimPattern {
  readonly pattern: RegExp;
  readonly topic: string;
  readonly verificationSource: 'world-bank' | 'scb' | 'both';
  readonly worldBankIndicator?: string;
  readonly scbTableId?: string;
}
 
/**
 * Patterns for detecting statistical claims in Swedish and English political text.
 * Each pattern maps to a specific topic and data source for verification.
 */
const CLAIM_PATTERNS: readonly ClaimPattern[] = [
  // Unemployment claims
  {
    pattern: /arbetslöshet(?:en)?\s+(?:ligger\s+på|är|uppgår\s+till|(?:har\s+)?(?:sjunkit|ökat|minskat)\s+till)\s+(\d+[,.]?\d*)\s*(?:procent|%)/gi,
    topic: 'unemployment',
    verificationSource: 'both',
    worldBankIndicator: 'SL.UEM.TOTL.ZS',
    scbTableId: 'TAB5765',
  },
  {
    pattern: /unemployment\s+(?:rate\s+)?(?:is|stands?\s+at|(?:has\s+)?(?:fallen|risen|dropped)\s+to)\s+(\d+[,.]?\d*)\s*(?:percent|%)/gi,
    topic: 'unemployment',
    verificationSource: 'both',
    worldBankIndicator: 'SL.UEM.TOTL.ZS',
    scbTableId: 'TAB5765',
  },
  // GDP claims
  {
    pattern: /(?:BNP|bruttonationalprodukt)(?:en)?\s+(?:växer|ökar|krymper|sjunker|har\s+(?:vuxit|ökat|krympt))\s+(?:med\s+)?(\d+[,.]?\d*)\s*(?:procent|%)/gi,
    topic: 'gdp',
    verificationSource: 'both',
    worldBankIndicator: 'NY.GDP.MKTP.KD.ZG',
    scbTableId: 'TAB5802',
  },
  {
    pattern: /GDP\s+(?:growth\s+)?(?:grew|expanded|contracted|shrank)\s+(?:by\s+)?(\d+[,.]?\d*)\s*(?:percent|%)/gi,
    topic: 'gdp',
    verificationSource: 'both',
    worldBankIndicator: 'NY.GDP.MKTP.KD.ZG',
    scbTableId: 'TAB5802',
  },
  // Inflation claims
  {
    pattern: /inflation(?:en)?\s+(?:ligger\s+på|är|uppgår\s+till|(?:har\s+)?(?:sjunkit|ökat)\s+till)\s+(\d+[,.]?\d*)\s*(?:procent|%)/gi,
    topic: 'inflation',
    verificationSource: 'both',
    worldBankIndicator: 'FP.CPI.TOTL.ZG',
  },
  {
    pattern: /inflation\s+(?:rate\s+)?(?:is|stands?\s+at|(?:has\s+)?(?:fallen|risen)\s+to)\s+(\d+[,.]?\d*)\s*(?:percent|%)/gi,
    topic: 'inflation',
    verificationSource: 'both',
    worldBankIndicator: 'FP.CPI.TOTL.ZG',
  },
  // Migration claims
  {
    pattern: /(?:invandring|immigration)(?:en)?\s+(?:ligger\s+på|är|uppgår\s+till|(?:har\s+)?(?:ökat|minskat)\s+till)\s+(\d+[\s,.]?\d*)\s*(?:personer|person|människor)?/gi,
    topic: 'migration',
    verificationSource: 'scb',
    scbTableId: 'TAB637',
  },
  {
    pattern: /immigration\s+(?:stands?\s+at|(?:has\s+)?(?:risen|fallen)\s+to)\s+(\d+[\s,.]?\d*)\s*(?:people|persons?)?/gi,
    topic: 'migration',
    verificationSource: 'scb',
    scbTableId: 'TAB637',
  },
  // Crime statistics claims
  {
    pattern: /(?:brott|brottslighet)(?:en)?\s+(?:har\s+)?(?:ökat|minskat)\s+(?:med\s+)?(\d+[,.]?\d*)\s*(?:procent|%)/gi,
    topic: 'crime',
    verificationSource: 'scb',
    scbTableId: 'TAB1172',
  },
  // Defence spending claims
  {
    pattern: /försvarsutgift(?:er|erna)?\s+(?:ligger\s+på|uppgår\s+till|är)\s+(\d+[,.]?\d*)\s*(?:procent|%)\s*(?:av\s+BNP)?/gi,
    topic: 'defence',
    verificationSource: 'both',
    worldBankIndicator: 'MS.MIL.XPND.GD.ZS',
  },
  {
    pattern: /(?:military|defence|defense)\s+spending\s+(?:is|stands?\s+at)\s+(\d+[,.]?\d*)\s*(?:percent|%)\s*(?:of\s+GDP)?/gi,
    topic: 'defence',
    verificationSource: 'both',
    worldBankIndicator: 'MS.MIL.XPND.GD.ZS',
  },
  // Education spending claims
  {
    pattern: /utbildning(?:sutgifter|sbudget)?\s+(?:uppgår\s+till|är|ligger\s+på)\s+(\d+[,.]?\d*)\s*(?:miljarder|procent|%)/gi,
    topic: 'education',
    verificationSource: 'scb',
    scbTableId: 'TAB4787',
  },
  // Housing claims
  {
    pattern: /bostadsbyggande(?:t)?\s+(?:har\s+)?(?:ökat|minskat)\s+(?:med\s+)?(\d+[,.]?\d*)\s*(?:procent|%|bostäder)/gi,
    topic: 'housing',
    verificationSource: 'scb',
    scbTableId: 'TAB2052',
  },
  // Generic numeric claims with percent
  {
    pattern: /(\d+[,.]?\d*)\s*(?:procent|%)\s+(?:av\s+)?(?:BNP|bruttonationalprodukt)/gi,
    topic: 'gdp-share',
    verificationSource: 'scb',
  },
];
 
// ---------------------------------------------------------------------------
// Localized headings
// ---------------------------------------------------------------------------
 
/**
 * Localized section headings for fact-check sections in articles.
 */
export const FACT_CHECK_HEADINGS: Readonly<Record<Language, FactCheckHeadings>> = {
  en: {
    factCheck: 'Fact Check',
    claimVsReality: 'Claim vs. Reality',
    verdict: 'Verdict',
    dataSource: 'Data Source',
  },
  sv: {
    factCheck: 'Faktakoll',
    claimVsReality: 'Påstående kontra verklighet',
    verdict: 'Bedömning',
    dataSource: 'Datakälla',
  },
  da: {
    factCheck: 'Faktatjek',
    claimVsReality: 'Påstand kontra virkelighed',
    verdict: 'Vurdering',
    dataSource: 'Datakilde',
  },
  no: {
    factCheck: 'Faktasjekk',
    claimVsReality: 'Påstand kontra virkelighet',
    verdict: 'Vurdering',
    dataSource: 'Datakilde',
  },
  fi: {
    factCheck: 'Faktatarkistus',
    claimVsReality: 'Väite vastaan todellisuus',
    verdict: 'Arvio',
    dataSource: 'Tietolähde',
  },
  de: {
    factCheck: 'Faktencheck',
    claimVsReality: 'Behauptung vs. Realität',
    verdict: 'Bewertung',
    dataSource: 'Datenquelle',
  },
  fr: {
    factCheck: 'Vérification des faits',
    claimVsReality: 'Affirmation vs. Réalité',
    verdict: 'Verdict',
    dataSource: 'Source des données',
  },
  es: {
    factCheck: 'Verificación de datos',
    claimVsReality: 'Afirmación vs. Realidad',
    verdict: 'Veredicto',
    dataSource: 'Fuente de datos',
  },
  nl: {
    factCheck: 'Feitencontrole',
    claimVsReality: 'Bewering vs. Werkelijkheid',
    verdict: 'Oordeel',
    dataSource: 'Databron',
  },
  ar: {
    factCheck: 'تدقيق الحقائق',
    claimVsReality: 'الادعاء مقابل الواقع',
    verdict: 'الحكم',
    dataSource: 'مصدر البيانات',
  },
  he: {
    factCheck: 'בדיקת עובדות',
    claimVsReality: 'טענה מול מציאות',
    verdict: 'הכרעה',
    dataSource: 'מקור נתונים',
  },
  ja: {
    factCheck: 'ファクトチェック',
    claimVsReality: '主張 vs. 現実',
    verdict: '判定',
    dataSource: 'データソース',
  },
  ko: {
    factCheck: '팩트체크',
    claimVsReality: '주장 대 현실',
    verdict: '판정',
    dataSource: '데이터 출처',
  },
  zh: {
    factCheck: '事实核查',
    claimVsReality: '声称与现实',
    verdict: '判定',
    dataSource: '数据来源',
  },
} as const;
 
// ---------------------------------------------------------------------------
// Core detection functions
// ---------------------------------------------------------------------------
 
/**
 * Extract statistical claims from text content (speech, debate transcript, document).
 *
 * Scans for patterns where politicians cite specific numbers related to
 * economic indicators, social statistics, or policy metrics that can be
 * verified against World Bank or SCB data.
 *
 * @param text - Text content to analyze (speech transcript, article, etc.)
 * @param speaker - Optional speaker name
 * @param party - Optional party affiliation
 * @returns Array of detected statistical claims
 */
export function detectStatisticalClaims(
  text: string,
  speaker?: string,
  party?: string,
): StatisticalClaim[] {
  if (!text || text.trim().length === 0) {
    return [];
  }
 
  const claims: StatisticalClaim[] = [];
  const seen = new Set<string>();
 
  for (const claimPattern of CLAIM_PATTERNS) {
    // Reset regex state for each iteration
    const pattern = new RegExp(claimPattern.pattern.source, claimPattern.pattern.flags);
    let match: RegExpExecArray | null;
 
    while ((match = pattern.exec(text)) !== null) {
      const sourceText = match[0];
      // Deduplicate by source text
      const key = `${claimPattern.topic}:${sourceText}`;
      Iif (seen.has(key)) continue;
      seen.add(key);
 
      // Parse the claimed numeric value
      let claimedValue: number | undefined;
      Eif (match[1]) {
        const normalized = match[1].replace(/\s/g, '').replace(',', '.');
        claimedValue = parseFloat(normalized);
        Iif (isNaN(claimedValue)) claimedValue = undefined;
      }
 
      // Determine unit from context
      const claimedUnit = detectUnit(sourceText);
 
      claims.push({
        sourceText,
        topic: claimPattern.topic,
        claimedValue,
        claimedUnit,
        speaker,
        party,
        verificationSource: claimPattern.verificationSource,
        worldBankIndicator: claimPattern.worldBankIndicator,
        scbTableId: claimPattern.scbTableId,
      });
    }
  }
 
  return claims;
}
 
/**
 * Determine the verdict for a claim based on deviation from official data.
 *
 * @param claimedValue - The value claimed by the politician
 * @param officialValue - The official value from data source
 * @returns Verdict and deviation percentage
 */
export function assessClaim(
  claimedValue: number,
  officialValue: number,
): { verdict: ClaimVerdict; deviationPercent: number } {
  if (officialValue === 0) {
    return {
      verdict: claimedValue === 0 ? 'accurate' : 'unverifiable',
      deviationPercent: claimedValue === 0 ? 0 : 100,
    };
  }
 
  const deviation = Math.abs(claimedValue - officialValue) / Math.abs(officialValue) * 100;
 
  if (deviation <= 5) {
    return { verdict: 'accurate', deviationPercent: deviation };
  }
  if (deviation <= 15) {
    return { verdict: 'mostly-accurate', deviationPercent: deviation };
  }
  if (deviation <= 30) {
    return { verdict: 'misleading', deviationPercent: deviation };
  }
  return { verdict: 'inaccurate', deviationPercent: deviation };
}
 
/**
 * Generate a human-readable explanation for a fact-check result.
 *
 * @param claim - The statistical claim
 * @param officialValue - Official value (if available)
 * @param officialPeriod - Period of official data
 * @param verdict - The assessed verdict
 * @param lang - Language code for localization
 * @returns Explanation string
 */
export function generateExplanation(
  claim: StatisticalClaim,
  officialValue: number | undefined,
  officialPeriod: string | undefined,
  verdict: ClaimVerdict,
  lang: Language | string = 'en',
): string {
  const isSwedish = lang === 'sv';
 
  if (verdict === 'unverifiable') {
    return isSwedish
      ? `Påståendet om ${claim.topic} kunde inte verifieras mot tillgängliga datakällor.`
      : `The claim about ${claim.topic} could not be verified against available data sources.`;
  }
 
  if (claim.claimedValue === undefined || officialValue === undefined) {
    return isSwedish
      ? `Kvantitativt påstående om ${claim.topic} detekterat men saknar verifierbart värde.`
      : `Quantitative claim about ${claim.topic} detected but lacks a verifiable value.`;
  }
 
  const sourceName = claim.verificationSource === 'scb'
    ? 'SCB (Statistics Sweden)'
    : claim.verificationSource === 'world-bank'
      ? 'World Bank'
      : 'SCB / World Bank';
 
  const period = officialPeriod ? ` (${officialPeriod})` : '';
 
  const verdictLabels: Record<ClaimVerdict, { en: string; sv: string }> = {
    accurate: { en: 'Accurate', sv: 'Korrekt' },
    'mostly-accurate': { en: 'Mostly accurate', sv: 'I huvudsak korrekt' },
    misleading: { en: 'Misleading', sv: 'Missvisande' },
    inaccurate: { en: 'Inaccurate', sv: 'Felaktigt' },
    unverifiable: { en: 'Unverifiable', sv: 'Ej verifierbart' },
    outdated: { en: 'Outdated data', sv: 'Föråldrade data' },
  };
 
  const verdictLabel = isSwedish ? verdictLabels[verdict].sv : verdictLabels[verdict].en;
 
  if (isSwedish) {
    return `${verdictLabel}: Påstått värde ${claim.claimedValue}${claim.claimedUnit ? ' ' + claim.claimedUnit : ''} jämfört med officiellt värde ${officialValue}${period} enligt ${sourceName}.`;
  }
 
  return `${verdictLabel}: Claimed value ${claim.claimedValue}${claim.claimedUnit ? ' ' + claim.claimedUnit : ''} compared to official value ${officialValue}${period} from ${sourceName}.`;
}
 
/**
 * Get localized fact-check section heading.
 *
 * @param lang - Language code
 * @param section - Section key
 * @returns Localized heading string
 */
export function getFactCheckHeading(
  lang: Language | string,
  section: keyof FactCheckHeadings,
): string {
  const headings = FACT_CHECK_HEADINGS[lang as Language] ?? FACT_CHECK_HEADINGS.en;
  return headings[section];
}
 
/**
 * Check if content contains statistical claims worth fact-checking.
 * Quick pre-filter before running full detection.
 *
 * @param content - Text content to check
 * @returns True if content likely contains statistical claims
 */
export function hasStatisticalClaims(content: string): boolean {
  const text = content.toLowerCase();
  const patterns: readonly RegExp[] = [
    /\d+[,.]?\d*\s*(?:procent|percent|%)/i,
    /(?:ökade?|minskade?|sjönk|steg)\s+(?:med|till)\s+\d/i,
    /\b(?:increased?|decreased?|fell|rose|dropped)\s+(?:by|to)\s+\d/i,
    /\barbetslöshet/i,
    /\bunemployment/i,
    /\bBNP\b/i,
    /\bGDP\b/i,
    /\binflation/i,
    /\binvandring/i,
    /\bimmigration\b/i,
    /\bbrott/i,
    /\bcrime\b/i,
    /\bförsvarsutgift/i,
    /\bmilitary\s+spending\b/i,
  ];
 
  return patterns.some((p) => p.test(text));
}
 
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
 
/**
 * Detect the unit of measurement from claim text.
 */
function detectUnit(text: string): string | undefined {
  const lower = text.toLowerCase();
  Eif (/procent|percent|%/.test(lower)) return 'percent';
  if (/miljarder/.test(lower)) return 'billions SEK';
  if (/miljoner/.test(lower)) return 'millions SEK';
  if (/personer|person|människor|people|persons?/.test(lower)) return 'persons';
  if (/bostäder/.test(lower)) return 'housing units';
  return undefined;
}