All files / scripts/ai-analysis/swot index.ts

90.6% Statements 164/181
79.24% Branches 84/106
73.77% Functions 45/61
89.04% Lines 130/146

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                                                                      19x                 19x                 19x                             16x         16x         609x 205x 25x         595x 595x 595x 595x 16x 16x       579x                           604x 604x 604x 604x       604x 9x   595x 595x   604x                               849x 849x 849x                                                       340x 340x 340x 340x 104x 340x 340x 340x 340x 104x 104x 104x 102x 102x       104x 80x 31x 24x 24x   104x 63x 14x   104x 31x     104x 41x 10x     104x 104x 104x 104x     104x 64x 41x 14x   104x 104x 9x   104x 70x     104x 104x 104x 104x     104x 31x 24x   104x 104x 25x   104x   104x 104x 104x 104x   104x                                                                               2x   2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 6x 2x 2x         2x       2x               2x     2x       2x                   4x 2x         2x 2x                 2x 2x       2x 2x                                                                 112x 104x 104x 104x   104x 104x 104x 104x 104x 104x   104x 112x 100x 100x 100x 300x 300x 1429x   100x         104x    
/**
 * @module ai-analysis/swot
 * @description SWOT bounded context – stakeholder SWOT matrix construction,
 *              enrichment and confidence scoring.
 * @license Apache-2.0
 * @author Hack23 AB
 */
 
import type { Language } from '../../types/language.js';
import type { RawDocument } from '../../data-transformers/types.js';
import { detectPolicyDomains, getDomainSpecificAnalysis } from '../../data-transformers/policy-analysis.js';
import { localizeDocType } from '../../data-transformers/content-generators/index.js';
import {
  type LangRecord,
  docType,
  docTitle,
  docId,
  isSfsDoc,
  normalizedDocType,
  isMetadataEnriched,
  hasFullTextContent,
  extractPassage,
} from '../helpers.js';
import type {
  AnalysisSwotEntry,
  AnalysisStakeholderSwot,
  KnownStakeholderRole,
  SwotQuadrant,
} from '../types.js';
import { buildPlaceholderText } from './placeholders.js';
 
/* ------------------------------------------------------------------ */
/*  Localised stakeholder name records                                 */
/* ------------------------------------------------------------------ */
 
export const GOV_NAMES: LangRecord = {
  en: 'Government / Policy Administration', sv: 'Regering / Policyförvaltning',
  da: 'Regering / Politisk forvaltning', no: 'Regjering / Politisk forvaltning',
  fi: 'Hallitus / Poliittinen hallinto', de: 'Regierung / Politikverwaltung',
  fr: 'Gouvernement / Administration', es: 'Gobierno / Administración pública',
  nl: 'Regering / Beleidsadministratie', ar: 'الحكومة / الإدارة السياسية',
  he: 'ממשלה / מינהל מדיניות', ja: '政府 / 政策行政', ko: '정부 / 정책 행정', zh: '政府 / 政策管理',
};
 
export const OPP_NAMES: LangRecord = {
  en: 'Parliament / Opposition', sv: 'Riksdag / Opposition',
  da: 'Folketing / Opposition', no: 'Storting / Opposisjon',
  fi: 'Eduskunta / Oppositio', de: 'Parlament / Opposition',
  fr: 'Parlement / Opposition', es: 'Parlamento / Oposición',
  nl: 'Parlement / Oppositie', ar: 'البرلمان / المعارضة',
  he: 'פרלמנט / אופוזיציה', ja: '議会 / 野党', ko: '의회 / 야당', zh: '议会 / 反对派',
};
 
export const PRIVATE_NAMES: LangRecord = {
  en: 'Private Sector / Civil Society', sv: 'Privat sektor / Civilsamhälle',
  da: 'Privat sektor / Civilsamfund', no: 'Privat sektor / Sivilsamfunn',
  fi: 'Yksityissektori / Kansalaisyhteiskunta', de: 'Privatsektor / Zivilgesellschaft',
  fr: 'Secteur privé / Société civile', es: 'Sector privado / Sociedad civil',
  nl: 'Privésector / Maatschappelijk middenveld', ar: 'القطاع الخاص / المجتمع المدني',
  he: 'המגזר הפרטי / החברה האזרחית', ja: '民間セクター / 市民社会', ko: '민간 부문 / 시민 사회', zh: '私营部门 / 民间社会',
};
 
/* ------------------------------------------------------------------ */
/*  Helper functions                                                   */
/* ------------------------------------------------------------------ */
 
/** Small localised phrase: "relevant to" / "relevant för" … */
function relevantLabel(lang: Language): string {
  const map: LangRecord = {
    en: 'relevant to', sv: 'relevant för', da: 'relevant for', no: 'relevant for',
    fi: 'liittyy', de: 'relevant für', fr: 'pertinent pour', es: 'relevante para',
    nl: 'relevant voor', ar: 'ذو صلة بـ', he: 'רלוונטי ל', ja: 'に関連:', ko: '관련:', zh: '相关:',
  };
  return map[lang] ?? 'relevant to';
}
 
/** Derive impact from document type. */
export function impactFromDocType(dt: string): 'high' | 'medium' | 'low' {
  if (['prop', 'sfs', 'bet', 'fpm'].includes(dt)) return 'high';
  if (['mot', 'skr', 'pressm', 'ip'].includes(dt)) return 'medium';
  return 'low';
}
 
/** Return a SwotEntry text built from a document title. */
function entryFromDoc(doc: RawDocument, topic: string | null, lang: Language): string {
  const title = docTitle(doc);
  const type = normalizedDocType(doc);
  const typeLabel = type && type !== 'other' ? localizeDocType(type, lang, 1) : '';
  if (topic) {
    const relevance = relevantLabel(lang);
    return typeLabel
      ? `${typeLabel}: ${title} — ${relevance} ${topic}`
      : `${title} — ${relevance} ${topic}`;
  }
  return typeLabel ? `${typeLabel}: ${title}` : title;
}
 
/* ------------------------------------------------------------------ */
/*  Entry builder functions                                            */
/* ------------------------------------------------------------------ */
 
/** Build a content-derived SWOT entry from full-text passage when available. */
export function buildEnrichedEntry(
  doc: RawDocument,
  topic: string | null,
  lang: Language,
  passageMaxChars: number,
): AnalysisSwotEntry {
  const passage = extractPassage(doc, passageMaxChars);
  const type = normalizedDocType(doc);
  const domainAnalysis = detectPolicyDomains(doc, lang);
  const domainText = domainAnalysis.length > 0
    ? getDomainSpecificAnalysis(domainAnalysis[0]!, type, lang)
    : '';
  let text: string;
  if (passage) {
    text = domainText ? `${passage} — ${domainText}` : passage;
  } else {
    text = entryFromDoc(doc, topic, lang);
    if (domainText) text = `${text}. ${domainText}`;
  }
  return {
    text,
    impact: impactFromDocType(type),
    sourceDocIds: [docId(doc)].filter(Boolean),
    confidence: passage ? 'HIGH' : isMetadataEnriched(doc) ? 'MEDIUM' : 'LOW',
  };
}
 
/** Build a structural placeholder entry when no documents exist for a quadrant. */
export function placeholderEntry(
  role: KnownStakeholderRole,
  quadrant: SwotQuadrant,
  topic: string | null,
  lang: Language,
  domains: string[],
): AnalysisSwotEntry {
  const primaryDomain = domains[0] ?? null;
  const text = buildPlaceholderText(role, quadrant, topic, primaryDomain, lang);
  return {
    text,
    impact: quadrant === 'strengths' || quadrant === 'opportunities' ? 'medium' : 'low',
    sourceDocIds: [],
    confidence: 'LOW',
  };
}
 
/* ------------------------------------------------------------------ */
/*  Main stakeholder SWOT builder                                      */
/* ------------------------------------------------------------------ */
 
/**
 * Build the three-stakeholder SWOT matrix from classified documents.
 *
 * @param docs - Raw documents to classify
 * @param topic - Optional focus topic
 * @param lang - Target language
 * @param domains - Detected policy domains
 * @returns Array of three stakeholder SWOT analyses
 */
export function buildStakeholderSwot(
  docs: RawDocument[],
  topic: string | null,
  lang: Language,
  domains: string[],
): AnalysisStakeholderSwot[] {
  // Classify documents by type
  const propDocs    = docs.filter(d => docType(d) === 'prop');
  const betDocs     = docs.filter(d => docType(d) === 'bet');
  const motDocs     = docs.filter(d => docType(d) === 'mot');
  const skrDocs     = docs.filter(d => docType(d) === 'skr');
  const sfsDocs     = docs.filter(isSfsDoc);
  const euDocs      = docs.filter(d => docType(d) === 'fpm' || docType(d) === 'eu');
  const pressmDocs  = docs.filter(d => docType(d) === 'pressm');
  const extDocs     = docs.filter(d => docType(d) === 'ext');
  const ipDocs      = docs.filter(d => docType(d) === 'ip');
  let govThreatIpDocs = ipDocs.slice(2, 4);
  let oppOpportunityIpDocs = ipDocs.slice(2, 3);
  if (ipDocs.length < 3) {
    govThreatIpDocs = ipDocs.length === 2 ? ipDocs.slice(1, 2) : ipDocs.slice(0, 1);
    oppOpportunityIpDocs = ipDocs.slice(0, 1);
  }
 
  // ── Government stakeholder SWOT
  const govStrengths: AnalysisSwotEntry[] = [
    ...propDocs.slice(0, 3).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...sfsDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...skrDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...pressmDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const govWeaknesses: AnalysisSwotEntry[] = [
    ...betDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...ipDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const govOpportunities: AnalysisSwotEntry[] = [
    ...euDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...skrDocs.slice(1, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const govThreats: AnalysisSwotEntry[] = [
    ...motDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...govThreatIpDocs.map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
 
  if (govStrengths.length === 0)    govStrengths.push(placeholderEntry('government', 'strengths', topic, lang, domains));
  if (govWeaknesses.length === 0)   govWeaknesses.push(placeholderEntry('government', 'weaknesses', topic, lang, domains));
  if (govOpportunities.length === 0) govOpportunities.push(placeholderEntry('government', 'opportunities', topic, lang, domains));
  if (govThreats.length === 0)      govThreats.push(placeholderEntry('government', 'threats', topic, lang, domains));
 
  // ── Parliament / Opposition SWOT
  const oppStrengths: AnalysisSwotEntry[] = [
    ...betDocs.slice(0, 3).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...motDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...ipDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const oppWeaknesses: AnalysisSwotEntry[] = [];
  const oppOpportunities: AnalysisSwotEntry[] = [
    ...oppOpportunityIpDocs.map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const oppThreats: AnalysisSwotEntry[] = [
    ...propDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
 
  if (oppStrengths.length === 0)    oppStrengths.push(placeholderEntry('parliament', 'strengths', topic, lang, domains));
  Eif (oppWeaknesses.length === 0)   oppWeaknesses.push(placeholderEntry('parliament', 'weaknesses', topic, lang, domains));
  if (oppOpportunities.length === 0) oppOpportunities.push(placeholderEntry('parliament', 'opportunities', topic, lang, domains));
  if (oppThreats.length === 0)      oppThreats.push(placeholderEntry('parliament', 'threats', topic, lang, domains));
 
  // ── Private Sector / Civil Society SWOT
  const privateStrengths: AnalysisSwotEntry[] = [
    ...sfsDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, 200)),
    ...extDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const privateWeaknesses: AnalysisSwotEntry[] = [];
  const privateOpportunities: AnalysisSwotEntry[] = [
    ...euDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, 200)),
  ];
  const privateThreats: AnalysisSwotEntry[] = [];
 
  if (privateStrengths.length === 0)    privateStrengths.push(placeholderEntry('private-sector', 'strengths', topic, lang, domains));
  Eif (privateWeaknesses.length === 0)   privateWeaknesses.push(placeholderEntry('private-sector', 'weaknesses', topic, lang, domains));
  if (privateOpportunities.length === 0) privateOpportunities.push(placeholderEntry('private-sector', 'opportunities', topic, lang, domains));
  Eif (privateThreats.length === 0)      privateThreats.push(placeholderEntry('private-sector', 'threats', topic, lang, domains));
 
  return [
    {
      name: GOV_NAMES[lang] ?? GOV_NAMES.en!,
      role: 'government',
      swot: { strengths: govStrengths, weaknesses: govWeaknesses, opportunities: govOpportunities, threats: govThreats },
    },
    {
      name: OPP_NAMES[lang] ?? OPP_NAMES.en!,
      role: 'parliament',
      swot: { strengths: oppStrengths, weaknesses: oppWeaknesses, opportunities: oppOpportunities, threats: oppThreats },
    },
    {
      name: PRIVATE_NAMES[lang] ?? PRIVATE_NAMES.en!,
      role: 'private-sector',
      swot: { strengths: privateStrengths, weaknesses: privateWeaknesses, opportunities: privateOpportunities, threats: privateThreats },
    },
  ];
}
 
/* ------------------------------------------------------------------ */
/*  Refinement                                                         */
/* ------------------------------------------------------------------ */
 
/**
 * Refine stakeholder SWOT entries using full-text document content.
 * Replaces metadata-derived entries with richer passage-based entries
 * when full text is available.
 *
 * @param initial - Current stakeholder SWOT array
 * @param fullTextDocs - Documents with full-text content
 * @param topic - Optional focus topic
 * @param lang - Target language
 * @returns Refined stakeholder SWOT array
 */
export function refineStakeholderSwot(
  initial: AnalysisStakeholderSwot[],
  fullTextDocs: RawDocument[],
  topic: string | null,
  lang: Language,
): AnalysisStakeholderSwot[] {
  const passageMax = 400;
 
  const propDocs   = fullTextDocs.filter(d => docType(d) === 'prop');
  const betDocs    = fullTextDocs.filter(d => docType(d) === 'bet');
  const motDocs    = fullTextDocs.filter(d => docType(d) === 'mot');
  const sfsDocs    = fullTextDocs.filter(isSfsDoc);
  const euDocs     = fullTextDocs.filter(d => docType(d) === 'fpm' || docType(d) === 'eu');
  const pressmDocs = fullTextDocs.filter(d => docType(d) === 'pressm');
  const extDocs    = fullTextDocs.filter(d => docType(d) === 'ext');
  const skrDocs    = fullTextDocs.filter(d => docType(d) === 'skr');
  const ipDocs     = fullTextDocs.filter(d => docType(d) === 'ip');
 
  return initial.map(sh => {
    if (sh.role === 'government') {
      const enrichedStrengths: AnalysisSwotEntry[] = [
        ...propDocs.slice(0, 3).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...sfsDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...skrDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...pressmDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      const enrichedWeaknesses: AnalysisSwotEntry[] = [
        ...betDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...ipDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      const enrichedOpportunities: AnalysisSwotEntry[] = [
        ...euDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...skrDocs.slice(1, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      // IPs 0–1 go to weaknesses above; IPs 2–3 go to threats.
      // When fewer than 3 IPs exist, reuse the last available IP for threats
      // so threat enrichment still fires for small IP counts.
      const ipDocsForThreats =
        ipDocs.length >= 3
          ? ipDocs.slice(2, 4)
          : ipDocs.slice(Math.max(ipDocs.length - 1, 0));
      const enrichedThreats: AnalysisSwotEntry[] = [
        ...motDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...ipDocsForThreats.map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      return {
        ...sh,
        swot: {
          strengths:     enrichedStrengths.length > 0 ? enrichedStrengths : sh.swot.strengths,
          weaknesses:    enrichedWeaknesses.length > 0 ? enrichedWeaknesses : sh.swot.weaknesses,
          opportunities: enrichedOpportunities.length > 0 ? enrichedOpportunities : sh.swot.opportunities,
          threats:       enrichedThreats.length > 0 ? enrichedThreats : sh.swot.threats,
        },
      };
    }
    if (sh.role === 'parliament') {
      const enrichedStrengths: AnalysisSwotEntry[] = [
        ...betDocs.slice(0, 3).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...motDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...ipDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      const enrichedThreats: AnalysisSwotEntry[] = propDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, passageMax));
      return {
        ...sh,
        swot: {
          ...sh.swot,
          strengths: enrichedStrengths.length > 0 ? enrichedStrengths : sh.swot.strengths,
          threats:   enrichedThreats.length > 0 ? enrichedThreats : sh.swot.threats,
        },
      };
    }
    Eif (sh.role === 'private-sector') {
      const enrichedStrengths: AnalysisSwotEntry[] = [
        ...sfsDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
        ...extDocs.slice(0, 2).map(d => buildEnrichedEntry(d, topic, lang, passageMax)),
      ];
      const enrichedOpportunities: AnalysisSwotEntry[] = euDocs.slice(0, 1).map(d => buildEnrichedEntry(d, topic, lang, passageMax));
      return {
        ...sh,
        swot: {
          ...sh.swot,
          strengths:     enrichedStrengths.length > 0 ? enrichedStrengths : sh.swot.strengths,
          opportunities: enrichedOpportunities.length > 0 ? enrichedOpportunities : sh.swot.opportunities,
        },
      };
    }
    return sh;
  });
}
 
/* ------------------------------------------------------------------ */
/*  Confidence scoring                                                 */
/* ------------------------------------------------------------------ */
 
/**
 * Calculate confidence score (0-100) calibrated on both document evidence
 * quality **and** SWOT entry evidence backing.
 *
 * Score breakdown (total 100):
 * - Evidence depth (70%): metadata enrichment, full-text, doc count, type variety
 * - SWOT quality (30%): proportion of non-placeholder SWOT entries
 *
 * @param docs - Documents analysed
 * @param stakeholderSwot - Optional SWOT entries (undefined → 15-point midpoint default)
 * @returns Confidence score 0-100
 */
export function calculateConfidenceScore(
  docs: RawDocument[],
  stakeholderSwot?: AnalysisStakeholderSwot[],
): number {
  if (docs.length === 0) return 0;
  const metadataEnriched = docs.filter(isMetadataEnriched).length;
  const fullText = docs.filter(hasFullTextContent).length;
  const typeVariety = new Set(docs.map(normalizedDocType)).size;
 
  const metadataRatio = metadataEnriched / docs.length;
  const fullTextRatio = fullText / docs.length;
  const enrichmentScore = metadataRatio * 20 + fullTextRatio * 20;
  const countScore = Math.min(docs.length / 10, 1) * 15;
  const varietyScore = Math.min(typeVariety / 5, 1) * 15;
  const evidenceDepth = enrichmentScore + countScore + varietyScore;
 
  let swotQuality = stakeholderSwot === undefined ? 15 : 0;
  if (stakeholderSwot && stakeholderSwot.length > 0) {
    let totalEntries = 0;
    let nonPlaceholderEntries = 0;
    for (const sh of stakeholderSwot) {
      const all = [...sh.swot.strengths, ...sh.swot.weaknesses, ...sh.swot.opportunities, ...sh.swot.threats];
      totalEntries += all.length;
      nonPlaceholderEntries += all.filter(e => e.sourceDocIds.length > 0).length;
    }
    swotQuality = totalEntries > 0
      ? (nonPlaceholderEntries / totalEntries) * 30
      : 0;
  }
 
  return Math.round(evidenceDepth + swotQuality);
}