All files / scripts/analysis-framework/lenses opposition.ts

94.66% Statements 71/75
76.19% Branches 64/84
100% Functions 12/12
98.38% Lines 61/62

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                                                1x                         1x                                   71x       191x 5348x               71x   71x   8x   7x 1x       71x     71x     65x     65x   64x               71x 71x 71x   71x       71x 6x     65x 65x   71x               71x 71x 71x 71x   71x                 71x 6x               71x 38x               71x 39x               71x               71x   71x 39x         39x             71x               71x 71x   71x           71x 121x             71x               71x 71x 71x 71x 71x 71x                                                 71x 71x   77x 39x   71x   71x 71x   71x                          
/**
 * @module analysis-framework/lenses/opposition
 * @description Opposition perspective lens for parliamentary document analysis.
 *
 * Evaluates every document from the opposition's vantage point:
 * - Scrutiny opportunities
 * - Alternative policy proposals
 * - Electoral positioning impact
 * - Coalition-building potential
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type { RawDocument, CIAContext } from '../../data-transformers/types.js';
import type { Language } from '../../types/language.js';
import type { PerspectiveAnalysis, ImpactLevel, Sentiment, SwotContribution, DashboardMetric, MindmapNode } from '../types.js';
import { detectPolicyDomains } from '../../data-transformers/policy-analysis.js';
 
// ---------------------------------------------------------------------------
// Scrutiny trigger keywords
// ---------------------------------------------------------------------------
 
/** Topics that typically attract intense opposition scrutiny */
const SCRUTINY_KEYWORDS: readonly string[] = [
  'privatisering', 'privatization', 'avreglering', 'deregulation',
  'skattesänkning', 'tax cut', 'neddragning', 'cut', 'nedskärning',
  'socialbidrag', 'welfare', 'sjukvård', 'healthcare', 'skola', 'school',
  'pension', 'arbetsmarknad', 'labour market', 'integration', 'migration',
  'bostadsbrist', 'housing shortage', 'korruption', 'corruption',
  'granskning', 'scrutiny', 'utredning', 'inquiry',
];
 
// ---------------------------------------------------------------------------
// Localised labels
// ---------------------------------------------------------------------------
 
const LENS_LABELS: Readonly<Record<string, { lensName: string; stakeholder: string }>> = {
  en: { lensName: 'Opposition Perspective', stakeholder: 'Opposition' },
  sv: { lensName: 'Oppositionsperspektiv', stakeholder: 'Oppositionen' },
  da: { lensName: 'Oppositionsperspektiv', stakeholder: 'Oppositionen' },
  no: { lensName: 'Opposisjonsperspektiv', stakeholder: 'Opposisjonen' },
  fi: { lensName: 'Opposition näkökulma', stakeholder: 'Oppositio' },
  de: { lensName: 'Oppositionsperspektive', stakeholder: 'Opposition' },
  fr: { lensName: "Perspective de l'opposition", stakeholder: "Opposition" },
  es: { lensName: 'Perspectiva de la oposición', stakeholder: 'Oposición' },
  nl: { lensName: 'Oppositieperspectief', stakeholder: 'Oppositie' },
  ar: { lensName: 'منظور المعارضة', stakeholder: 'المعارضة' },
  he: { lensName: 'פרספקטיבת האופוזיציה', stakeholder: 'האופוזיציה' },
  ja: { lensName: '野党の視点', stakeholder: '野党' },
  ko: { lensName: '야당 관점', stakeholder: '야당' },
  zh: { lensName: '反对党视角', stakeholder: '反对党' },
};
 
function label(lang: Language | string): { lensName: string; stakeholder: string } {
  return LENS_LABELS[lang] ?? LENS_LABELS['en'];
}
 
function containsAny(text: string, keywords: readonly string[]): boolean {
  const lower = text.toLowerCase();
  return keywords.some(kw => lower.includes(kw.toLowerCase()));
}
 
// ---------------------------------------------------------------------------
// Scoring helpers
// ---------------------------------------------------------------------------
 
function computeImpact(doc: RawDocument): ImpactLevel {
  const docType = doc.doktyp || doc.documentType || '';
  // Government propositions are prime opposition targets
  if (docType === 'prop') return 'high';
  // Committee reports signal a policy decision about to be enacted
  if (docType === 'bet') return 'high';
  // Opposition motions: medium self-originated impact
  if (docType === 'mot') return 'medium';
  return 'low';
}
 
function computeSentiment(doc: RawDocument): Sentiment {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  // Opposition motions are opportunities for the opposition → positive
  if (doc.doktyp === 'mot') return 'positive';
 
  // Scrutiny-triggering content from the government is an opportunity
  Iif (doc.doktyp === 'prop' && containsAny(allText, SCRUTINY_KEYWORDS)) return 'positive';
 
  // Committee reports passing government proposals: negative for opposition
  if (doc.doktyp === 'bet') return 'negative';
 
  return 'neutral';
}
 
// ---------------------------------------------------------------------------
// Summary generation
// ---------------------------------------------------------------------------
 
function buildSummary(doc: RawDocument, cia: CIAContext | undefined, _lang: Language | string, domains: string[]): string {
  const title = doc.titel || doc.title || 'Untitled';
  const docType = doc.doktyp || doc.documentType || 'document';
  const domainPhrase = domains.length > 0 ? ` in ${domains.slice(0, 2).join(' and ')}` : '';
 
  const denialNote = cia
    ? ` Note: approximately ${cia.overallMotionDenialRate ?? 99}% of opposition motions are denied.`
    : '';
 
  if (docType === 'mot') {
    return `From the opposition perspective, this motion${domainPhrase} signals a policy alternative and positions the party electorally.${denialNote} Success in passage is unlikely, but the motion builds party profile and accountability record.`;
  }
 
  const scrutiny = containsAny(title, SCRUTINY_KEYWORDS);
  const scrutinyNote = scrutiny ? ' This document creates scrutiny opportunities the opposition should exploit in debates and committee hearings.' : '';
 
  return `From the opposition perspective, this ${docType}${domainPhrase} warrants scrutiny for alignment with citizen welfare and democratic accountability.${scrutinyNote}`.trim();
}
 
// ---------------------------------------------------------------------------
// SWOT contributions
// ---------------------------------------------------------------------------
 
function buildSwotContributions(doc: RawDocument, cia: CIAContext | undefined, lang: Language | string): SwotContribution[] {
  const { stakeholder } = label(lang);
  const contributions: SwotContribution[] = [];
  const docType = doc.doktyp || doc.documentType || '';
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  Iif (docType === 'prop' && containsAny(allText, SCRUTINY_KEYWORDS)) {
    contributions.push({
      quadrant: 'opportunity',
      forStakeholder: stakeholder,
      text: 'Government proposal touches politically sensitive topics, creating scrutiny and differentiation opportunities.',
      impact: 'high',
    });
  }
 
  if (docType === 'mot') {
    contributions.push({
      quadrant: 'strength',
      forStakeholder: stakeholder,
      text: 'Opposition motion on record, building electoral positioning and demonstrating policy alternative.',
      impact: 'medium',
    });
  }
 
  if (cia && cia.coalitionStability.stabilityScore < 50) {
    contributions.push({
      quadrant: 'opportunity',
      forStakeholder: stakeholder,
      text: `Coalition fragility (stability: ${cia.coalitionStability.stabilityScore}) creates defection recruitment opportunities.`,
      impact: 'high',
    });
  }
 
  if (cia && cia.overallMotionDenialRate && cia.overallMotionDenialRate > 95) {
    contributions.push({
      quadrant: 'threat',
      forStakeholder: stakeholder,
      text: `High motion denial rate (${cia.overallMotionDenialRate}%) limits legislative impact of opposition proposals.`,
      impact: 'medium',
    });
  }
 
  return contributions;
}
 
// ---------------------------------------------------------------------------
// Dashboard metrics
// ---------------------------------------------------------------------------
 
function buildDashboardMetrics(_doc: RawDocument, cia: CIAContext | undefined): DashboardMetric[] {
  const metrics: DashboardMetric[] = [];
 
  if (cia) {
    metrics.push({
      metricName: 'Motion Denial Rate',
      value: cia.overallMotionDenialRate ?? 99,
      unit: '%',
    });
    metrics.push({
      metricName: 'Coalition Stability',
      value: cia.coalitionStability.stabilityScore,
      unit: 'score',
    });
  }
 
  return metrics;
}
 
// ---------------------------------------------------------------------------
// Mindmap nodes
// ---------------------------------------------------------------------------
 
function buildMindmapNodes(doc: RawDocument, _lang: Language | string, domains: string[]): MindmapNode[] {
  const nodes: MindmapNode[] = [];
  const docType = doc.doktyp || doc.documentType || '';
 
  nodes.push({
    branch: 'Scrutiny Targets',
    item: docType === 'prop' ? 'Government Bill Accountability' : 'Legislative Oversight',
    weight: docType === 'prop' ? 'critical' : 'significant',
  });
 
  for (const domain of domains.slice(0, 2)) {
    nodes.push({
      branch: 'Electoral Positioning',
      item: domain,
      weight: 'moderate',
    });
  }
 
  return nodes;
}
 
// ---------------------------------------------------------------------------
// Confidence
// ---------------------------------------------------------------------------
 
function computeConfidence(doc: RawDocument, cia: CIAContext | undefined): number {
  let score = 40;
  Iif (doc.fullText || doc.fullContent) score += 20;
  if (doc.summary || doc.notis) score += 10;
  if (cia) score += 20;
  Iif (doc.speeches && doc.speeches.length > 0) score += 10;
  return Math.min(100, score);
}
 
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
 
/**
 * Apply the Opposition analysis lens to a single parliamentary document.
 *
 * Evaluates scrutiny opportunities, alternative policy proposals, electoral
 * positioning impact, and coalition-building potential.
 *
 * @param doc  - The document to analyse
 * @param cia  - Optional CIA context
 * @param lang - Target language
 * @returns    A `PerspectiveAnalysis` for the opposition lens
 */
export function analyzeOppositionPerspective(
  doc: RawDocument,
  cia: CIAContext | undefined,
  lang: Language | string,
  precomputedDomains?: string[],
): PerspectiveAnalysis {
  // Key actors: opposition parties in parliament
  const keyActors: string[] = ['Opposition Leader', 'Shadow Cabinet'];
  if (cia) {
    // Include parties that are not the largest governing bloc
    const parties = cia.partyPerformance.slice(0, 4).map(p => p.partyName);
    keyActors.push(...parties);
  }
  Eif (doc.parti) keyActors.unshift(doc.parti);
 
  const domains = precomputedDomains ?? detectPolicyDomains(doc, 'en');
  const relatedPolicies = [...domains, 'Electoral Programme', 'Parliamentary Scrutiny'].filter(Boolean);
 
  return {
    lens: 'opposition',
    summary: buildSummary(doc, cia, lang, domains),
    impact: computeImpact(doc),
    sentiment: computeSentiment(doc),
    keyActors: [...new Set(keyActors)].slice(0, 5),
    relatedPolicies: relatedPolicies.slice(0, 5),
    swotContribution: buildSwotContributions(doc, cia, lang),
    dashboardMetrics: buildDashboardMetrics(doc, cia),
    mindmapNodes: buildMindmapNodes(doc, lang, domains),
    confidence: computeConfidence(doc, cia),
  };
}