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

94.44% Statements 68/72
84% Branches 42/50
100% Functions 17/17
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                                                1x             1x                 1x                     1x                                   68x       556x 7400x               68x   68x 331x     68x 6x 6x 2x       68x     68x           68x   17x     51x               68x 68x   68x 68x 68x   68x 68x   68x               68x 68x 68x   331x 66x               68x 1x               68x 49x               68x               68x 68x   68x   1768x       68x   1428x       68x               68x 68x   68x           68x 49x     68x 1x     68x               68x 68x 68x 68x 68x                                               68x 68x 68x   68x                          
/**
 * @module analysis-framework/lenses/citizen
 * @description Citizen perspective lens for parliamentary document analysis.
 *
 * Evaluates every document from the citizen's vantage point:
 * - Service delivery impact
 * - Rights and freedoms implications
 * - Cost of living effects
 * - Democratic participation impact
 *
 * @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';
 
// ---------------------------------------------------------------------------
// Domain to citizen service mapping
// ---------------------------------------------------------------------------
 
/** Policy domains with direct citizen service-delivery impact */
const HIGH_CITIZEN_IMPACT_DOMAINS: readonly string[] = [
  'healthcare policy', 'education policy', 'housing', 'labour', 'fiscal policy',
  'justice', 'migration', 'hälso- och sjukvårdspolitik', 'utbildningspolitik',
  'arbetsmarknad', 'bostad', 'rättvisa', 'migration',
];
 
/** Keywords indicating direct cost-of-living impact */
const COST_OF_LIVING_KEYWORDS: readonly string[] = [
  'skatt', 'tax', 'avgift', 'fee', 'hyra', 'rent', 'prisökning', 'price increase',
  'inflation', 'köpkraft', 'purchasing power', 'levnadskostnad', 'cost of living',
  'energipris', 'energy price', 'bensinpris', 'fuel price', 'matpris', 'food price',
  'barnbidrag', 'child benefit', 'sjukpenning', 'sick pay', 'a-kassa', 'unemployment benefit',
  'pension',
];
 
/** Keywords indicating rights or freedoms impact */
const RIGHTS_KEYWORDS: readonly string[] = [
  'rättighet', 'right', 'frihet', 'freedom', 'integritet', 'privacy', 'gdpr',
  'diskriminering', 'discrimination', 'jämlikhet', 'equality', 'tillgänglighet', 'accessibility',
  'tryckfrihet', 'press freedom', 'yttrandefrihet', 'freedom of expression',
  'dataskydd', 'data protection', 'personuppgifter', 'personal data',
];
 
// ---------------------------------------------------------------------------
// Localised labels
// ---------------------------------------------------------------------------
 
const LENS_LABELS: Readonly<Record<string, { lensName: string; stakeholder: string }>> = {
  en: { lensName: 'Citizen Perspective', stakeholder: 'Citizens' },
  sv: { lensName: 'Medborgarperspektiv', stakeholder: 'Medborgarna' },
  da: { lensName: 'Borgerperspektiv', stakeholder: 'Borgerne' },
  no: { lensName: 'Borgerperspektiv', stakeholder: 'Borgerne' },
  fi: { lensName: 'Kansalaisnäkökulma', stakeholder: 'Kansalaiset' },
  de: { lensName: 'Bürgerperspektive', stakeholder: 'Bürgerinnen und Bürger' },
  fr: { lensName: 'Perspective citoyenne', stakeholder: 'Citoyens' },
  es: { lensName: 'Perspectiva ciudadana', stakeholder: 'Ciudadanos' },
  nl: { lensName: 'Burgerperspectief', stakeholder: 'Burgers' },
  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, domains: string[]): ImpactLevel {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  const hasCitizenDomain = domains.some(d =>
    HIGH_CITIZEN_IMPACT_DOMAINS.some(cd => d.toLowerCase().includes(cd.toLowerCase()))
  );
 
  if (hasCitizenDomain && (doc.doktyp === 'prop' || doc.doktyp === 'bet')) return 'high';
  Iif (containsAny(allText, COST_OF_LIVING_KEYWORDS) || containsAny(allText, RIGHTS_KEYWORDS)) return 'high';
  if (hasCitizenDomain) return 'medium';
  return 'low';
}
 
function computeSentiment(doc: RawDocument): Sentiment {
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  // Any rights-reducing language → negative
  Iif (containsAny(allText, ['neddragning', 'nedskärning', 'cut', 'reduction', 'restriction',
    'begränsning', 'inskränkning', 'avgiftshöjning', 'fee increase'])) {
    return 'negative';
  }
 
  // Investment in services → positive
  if (containsAny(allText, ['satsning', 'investment', 'ökning', 'increase', 'förbättring',
    'improvement', 'förstärkning', 'strengthening', 'bidrag', 'grant', 'stöd', 'support'])) {
    return 'positive';
  }
 
  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 hasCostImpact = containsAny(allText, COST_OF_LIVING_KEYWORDS);
  const hasRightsImpact = containsAny(allText, RIGHTS_KEYWORDS);
  const domainPhrase = domains.length > 0 ? ` affecting ${domains.slice(0, 2).join(' and ')}` : '';
 
  const costNote = hasCostImpact ? ' Citizens should assess potential cost-of-living implications closely.' : '';
  const rightsNote = hasRightsImpact ? ' Rights and freedoms dimensions of this document warrant public attention.' : '';
 
  return `From the citizen perspective, this ${docType}${domainPhrase} has direct implications for service delivery and everyday life.${costNote}${rightsNote}`.trim();
}
 
// ---------------------------------------------------------------------------
// SWOT contributions
// ---------------------------------------------------------------------------
 
function buildSwotContributions(doc: RawDocument, _cia: CIAContext | undefined, lang: Language | string, domains: string[]): SwotContribution[] {
  const { stakeholder } = label(lang);
  const contributions: SwotContribution[] = [];
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  if (domains.some(d => HIGH_CITIZEN_IMPACT_DOMAINS.some(cd => d.toLowerCase().includes(cd.toLowerCase())))) {
    contributions.push({
      quadrant: 'opportunity',
      forStakeholder: stakeholder,
      text: 'Document addresses areas with high citizen service delivery impact.',
      impact: 'high',
    });
  }
 
  if (containsAny(allText, RIGHTS_KEYWORDS)) {
    contributions.push({
      quadrant: 'threat',
      forStakeholder: stakeholder,
      text: 'Document may affect citizen rights and freedoms — requires civil society monitoring.',
      impact: 'high',
    });
  }
 
  if (containsAny(allText, COST_OF_LIVING_KEYWORDS)) {
    contributions.push({
      quadrant: 'weakness',
      forStakeholder: stakeholder,
      text: 'Possible cost-of-living implications need transparent impact assessment for vulnerable groups.',
      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(' ');
  const metrics: DashboardMetric[] = [];
 
  metrics.push({
    metricName: 'Cost-of-Living Keywords',
    value: COST_OF_LIVING_KEYWORDS.filter(kw => allText.toLowerCase().includes(kw.toLowerCase())).length,
    unit: 'hits',
  });
 
  metrics.push({
    metricName: 'Rights Keywords',
    value: RIGHTS_KEYWORDS.filter(kw => allText.toLowerCase().includes(kw.toLowerCase())).length,
    unit: 'hits',
  });
 
  return metrics;
}
 
// ---------------------------------------------------------------------------
// Mindmap nodes
// ---------------------------------------------------------------------------
 
function buildMindmapNodes(doc: RawDocument, _lang: Language | string): MindmapNode[] {
  const nodes: MindmapNode[] = [];
  const allText = [doc.titel, doc.title, doc.summary, doc.notis].filter(Boolean).join(' ');
 
  nodes.push({
    branch: 'Service Delivery',
    item: 'Direct Citizen Impact',
    weight: 'significant',
  });
 
  if (containsAny(allText, COST_OF_LIVING_KEYWORDS)) {
    nodes.push({ branch: 'Economic Wellbeing', item: 'Cost-of-Living Effect', weight: 'critical' });
  }
 
  if (containsAny(allText, RIGHTS_KEYWORDS)) {
    nodes.push({ branch: 'Rights & Freedoms', item: 'Civil Liberties Review', weight: 'critical' });
  }
 
  return nodes;
}
 
// ---------------------------------------------------------------------------
// Confidence
// ---------------------------------------------------------------------------
 
function computeConfidence(doc: RawDocument): number {
  let score = 40;
  Iif (doc.fullText || doc.fullContent) score += 25;
  if (doc.summary || doc.notis) score += 15;
  Iif (doc.speeches && doc.speeches.length > 0) score += 10;
  return Math.min(100, score);
}
 
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
 
/**
 * Apply the Citizen analysis lens to a single parliamentary document.
 *
 * Evaluates service delivery impact, rights and freedoms implications,
 * cost of living effects, and democratic participation impact.
 *
 * @param doc  - The document to analyse
 * @param cia  - Optional CIA context
 * @param lang - Target language
 * @returns    A `PerspectiveAnalysis` for the citizen lens
 */
export function analyzeCitizenPerspective(
  doc: RawDocument,
  cia: CIAContext | undefined,
  lang: Language | string,
  precomputedDomains?: string[],
): PerspectiveAnalysis {
  const keyActors = ['Civil Society', 'Trade Unions', 'Consumer Groups', 'Ombudsmen'];
  const domains = precomputedDomains ?? detectPolicyDomains(doc, 'en');
  const relatedPolicies = [...domains, 'Public Services Charter', 'Rights Framework'].filter(Boolean);
 
  return {
    lens: 'citizen',
    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, domains),
    dashboardMetrics: buildDashboardMetrics(doc, cia),
    mindmapNodes: buildMindmapNodes(doc, lang),
    confidence: computeConfidence(doc),
  };
}