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

95.4% Statements 83/87
85.71% Branches 48/56
100% Functions 18/18
100% Lines 71/71

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                                                1x               1x                 1x                       1x                                   68x       1020x 25160x       476x               68x   68x 67x 85x       68x 68x 68x   68x   68x 6x 6x 3x       68x 68x 68x   68x 67x 66x               68x 68x   68x 197x 68x   68x 68x 68x 68x   68x   68x               68x 68x 68x   68x 1x               68x 1x               68x 2x               68x               68x 68x                       68x 68x   68x 1x   68x 2x   68x 1x   68x 463x   64x     68x               68x 68x 68x 68x 68x 68x                                               68x 68x 68x   68x                          
/**
 * @module analysis-framework/lenses/economic
 * @description Economic perspective lens for parliamentary document analysis.
 *
 * Evaluates every document from the economic vantage point:
 * - GDP/employment implications
 * - Business regulatory impact
 * - Investment climate effects
 * - Trade and competitiveness
 *
 * @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';
 
// ---------------------------------------------------------------------------
// Economic keyword banks
// ---------------------------------------------------------------------------
 
/** Keywords indicating positive economic signals */
const POSITIVE_ECON_KEYWORDS: readonly string[] = [
  'tillväxt', 'growth', 'sysselsättning', 'employment', 'investering', 'investment',
  'konkurrenskraft', 'competitiveness', 'export', 'innovation', 'produktivitet', 'productivity',
  'avreglering', 'deregulation', 'skattelättnader', 'tax relief', 'incitament', 'incentive',
  'frihandel', 'free trade', 'handelsliberalisering', 'trade liberalisation',
];
 
/** Keywords indicating negative economic signals */
const NEGATIVE_ECON_KEYWORDS: readonly string[] = [
  'recession', 'lågkonjunktur', 'downturn', 'nedgång', 'nedskärning', 'cut',
  'avskedning', 'layoff', 'varsel', 'redundancy', 'konkurs', 'bankruptcy',
  'skuld', 'debt', 'underskott', 'deficit', 'protektionism', 'protectionism',
  'handelshinder', 'trade barrier', 'importrestriktion', 'import restriction',
  'strejk', 'strike', 'lockout', 'arbetskonflikt', 'labour dispute',
];
 
/** Keywords indicating business regulation impact */
const REGULATION_KEYWORDS: readonly string[] = [
  'tillstånd', 'permit', 'reglering', 'regulation', 'compliance', 'direktiv', 'directive',
  'rapporteringskrav', 'reporting requirement', 'bokföring', 'accounting', 'redovisning',
  'hållbarhetsredovisning', 'sustainability reporting', 'kapitalkrav', 'capital requirement',
  'tillsyn', 'supervision', 'tillsynsmyndighet', 'regulatory authority',
  'sanktionsavgift', 'sanction fee', 'böter', 'fine', 'straffavgift',
];
 
// ---------------------------------------------------------------------------
// Localised labels
// ---------------------------------------------------------------------------
 
const LENS_LABELS: Readonly<Record<string, { lensName: string; stakeholder: string }>> = {
  en: { lensName: 'Economic Perspective', stakeholder: 'Economy & Business' },
  sv: { lensName: 'Ekonomiperspektiv', stakeholder: 'Ekonomi och näringsliv' },
  da: { lensName: 'Økonomiperspektiv', stakeholder: 'Økonomi og erhvervsliv' },
  no: { lensName: 'Økonomiperspektiv', stakeholder: 'Økonomi og næringsliv' },
  fi: { lensName: 'Talousnäkökulma', stakeholder: 'Talous ja elinkeinoelämä' },
  de: { lensName: 'Wirtschaftsperspektive', stakeholder: 'Wirtschaft' },
  fr: { lensName: 'Perspective économique', stakeholder: 'Économie' },
  es: { lensName: 'Perspectiva económica', stakeholder: 'Economía' },
  nl: { lensName: 'Economisch perspectief', stakeholder: 'Economie' },
  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 countKeywords(text: string, keywords: readonly string[]): number {
  const lower = text.toLowerCase();
  return keywords.filter(kw => lower.includes(kw.toLowerCase())).length;
}
 
function containsAny(text: string, keywords: readonly string[]): boolean {
  return countKeywords(text, keywords) > 0;
}
 
// ---------------------------------------------------------------------------
// Scoring helpers
// ---------------------------------------------------------------------------
 
function computeImpact(doc: RawDocument, domains: string[]): ImpactLevel {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  const hasEconDomain = domains.some(d =>
    ['fiscal', 'trade', 'labour', 'housing', 'fiscal policy', 'trade', 'labour market'].some(
      ed => d.toLowerCase().includes(ed)
    )
  );
 
  const posCount = countKeywords(allText, POSITIVE_ECON_KEYWORDS);
  const negCount = countKeywords(allText, NEGATIVE_ECON_KEYWORDS);
  const regCount = countKeywords(allText, REGULATION_KEYWORDS);
 
  const totalSignals = posCount + negCount + regCount;
 
  if (hasEconDomain && (doc.doktyp === 'prop' || doc.doktyp === 'bet')) return 'high';
  Iif (totalSignals >= 3) return 'high';
  if (totalSignals >= 1 || hasEconDomain) return 'medium';
  return 'low';
}
 
function computeSentiment(doc: RawDocument): Sentiment {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
  const posCount = countKeywords(allText, POSITIVE_ECON_KEYWORDS);
  const negCount = countKeywords(allText, NEGATIVE_ECON_KEYWORDS);
 
  if (posCount > negCount + 1) return 'positive';
  if (negCount > posCount + 1) return 'negative';
  return 'neutral';
}
 
// ---------------------------------------------------------------------------
// Summary generation
// ---------------------------------------------------------------------------
 
function buildSummary(doc: RawDocument, _cia: CIAContext | undefined, _lang: Language | string, domains: string[]): string {
  const docType = doc.doktyp || doc.documentType || 'document';
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  const hasReg = containsAny(allText, REGULATION_KEYWORDS);
  const hasTrade = domains.some(d => d.toLowerCase().includes('trade'));
  const hasFiscal = domains.some(d => d.toLowerCase().includes('fiscal'));
 
  const notes: string[] = [];
  if (hasReg) notes.push('regulatory compliance costs require business impact assessment');
  Iif (hasTrade) notes.push('trade and competitiveness implications should be modelled');
  if (hasFiscal) notes.push('fiscal multiplier effects on GDP and employment warrant quantification');
 
  const notePhrase = notes.length > 0 ? ` Specifically, ${notes.join('; ')}.` : '';
 
  return `From the economic perspective, this ${docType} may affect business environment, employment levels, and investment climate.${notePhrase}`.trim();
}
 
// ---------------------------------------------------------------------------
// SWOT contributions
// ---------------------------------------------------------------------------
 
function buildSwotContributions(doc: RawDocument, _cia: CIAContext | undefined, lang: Language | string): SwotContribution[] {
  const { stakeholder } = label(lang);
  const contributions: SwotContribution[] = [];
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  if (containsAny(allText, POSITIVE_ECON_KEYWORDS)) {
    contributions.push({
      quadrant: 'opportunity',
      forStakeholder: stakeholder,
      text: 'Document signals economic growth-oriented policy with potential investment and employment benefits.',
      impact: 'high',
    });
  }
 
  if (containsAny(allText, NEGATIVE_ECON_KEYWORDS)) {
    contributions.push({
      quadrant: 'threat',
      forStakeholder: stakeholder,
      text: 'Economic contraction indicators detected — assess potential GDP and employment downside risk.',
      impact: 'high',
    });
  }
 
  if (containsAny(allText, REGULATION_KEYWORDS)) {
    contributions.push({
      quadrant: 'weakness',
      forStakeholder: stakeholder,
      text: 'New regulatory requirements may increase business compliance costs and reduce competitiveness.',
      impact: 'medium',
    });
  }
 
  return contributions;
}
 
// ---------------------------------------------------------------------------
// Dashboard metrics
// ---------------------------------------------------------------------------
 
function buildDashboardMetrics(doc: RawDocument, _cia: CIAContext | undefined): DashboardMetric[] {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
  return [
    { metricName: 'Growth Signals', value: countKeywords(allText, POSITIVE_ECON_KEYWORDS), unit: 'hits' },
    { metricName: 'Contraction Signals', value: countKeywords(allText, NEGATIVE_ECON_KEYWORDS), unit: 'hits' },
    { metricName: 'Regulatory Keywords', value: countKeywords(allText, REGULATION_KEYWORDS), unit: 'hits' },
  ];
}
 
// ---------------------------------------------------------------------------
// Mindmap nodes
// ---------------------------------------------------------------------------
 
function buildMindmapNodes(doc: RawDocument, _lang: Language | string, domains: string[]): MindmapNode[] {
  const nodes: MindmapNode[] = [];
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  if (containsAny(allText, POSITIVE_ECON_KEYWORDS)) {
    nodes.push({ branch: 'Growth Opportunities', item: 'Investment & Employment', weight: 'critical' });
  }
  if (containsAny(allText, REGULATION_KEYWORDS)) {
    nodes.push({ branch: 'Regulatory Environment', item: 'Business Compliance', weight: 'significant' });
  }
  if (containsAny(allText, NEGATIVE_ECON_KEYWORDS)) {
    nodes.push({ branch: 'Economic Risks', item: 'Contraction Indicators', weight: 'critical' });
  }
  for (const d of domains.filter(d =>
    ['fiscal', 'trade', 'labour'].some(e => d.toLowerCase().includes(e))
  ).slice(0, 2)) {
    nodes.push({ branch: 'Policy Domains', item: d, 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 Economic analysis lens to a single parliamentary document.
 *
 * Evaluates GDP/employment implications, business regulatory impact,
 * investment climate effects, and trade and competitiveness.
 *
 * @param doc  - The document to analyse
 * @param cia  - Optional CIA context
 * @param lang - Target language
 * @returns    A `PerspectiveAnalysis` for the economic lens
 */
export function analyzeEconomicPerspective(
  doc: RawDocument,
  cia: CIAContext | undefined,
  lang: Language | string,
  precomputedDomains?: string[],
): PerspectiveAnalysis {
  const keyActors = ['Finance Ministry', 'Riksbank', 'Employers Federation', 'Trade Unions', 'Chamber of Commerce'];
  const domains = precomputedDomains ?? detectPolicyDomains(doc, 'en');
  const relatedPolicies = [...domains, 'Economic Policy Framework', 'Growth Strategy'].filter(Boolean);
 
  return {
    lens: 'economic',
    summary: buildSummary(doc, cia, lang, domains),
    impact: computeImpact(doc, domains),
    sentiment: computeSentiment(doc),
    keyActors: 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),
  };
}