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 | 24x 24x 24x 24x 88x 88x 88x 30x 29x 29x 44x 44x 44x 29x 30x 29x 29x 29x 44x 44x 29x 29x 30x 44x 30x 7x 7x 13x 13x 9x 7x 5x 5x 5x 5x 30x 29x 29x 29x 29x 44x 44x 44x 44x 7x 2x 2x 44x 29x 29x 38x 36x 33x 14x 30x 30x 30x 30x 30x 120x 30x 30x | /**
* @module ai-analysis/political-significance
* @description Political significance scoring for news generation pipelines.
*
* Produces a 0–100 score reflecting a document set's overall political
* significance, plus an editorial urgency label. Used by:
* - Real-time monitor to gate article generation (≥ 60 threshold)
* - Breaking news generator to classify urgency
* - Workflow-state coordinator to prioritise high-significance articles
* - Article template for machine-readable metadata
*
* Scoring signals:
* 1. Document type weight (40 %)
* 2. Volume / party breadth (20 %)
* 3. Opposition pressure (20 %)
* 4. Historical rarity / topic (20 %)
*
* The function is **pure** — deterministic for the same input, no randomness.
*
* @author Hack23 AB
* @license Apache-2.0
*/
import type { RawDocument } from '../data-transformers/types.js';
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
/** Urgency label derived from the significance score */
export type UrgencyLabel = 'breaking' | 'major' | 'standard' | 'background';
/** Contribution of a single scoring signal to the overall score */
export interface SignalContribution {
/** Human-readable signal name */
signal: string;
/** Weight (0-1) of this signal in the composite score */
weight: number;
/** Raw value (0-100) before weighting */
value: number;
}
/** Full significance assessment for a set of documents */
export interface SignificanceScore {
/** Composite score 0-100 */
score: number;
/** Editorial urgency label */
urgency: UrgencyLabel;
/** Per-signal breakdown */
signals: SignalContribution[];
}
// ---------------------------------------------------------------------------
// Default threshold
// ---------------------------------------------------------------------------
/** Default minimum significance score for breaking-news generation */
export const BREAKING_NEWS_THRESHOLD = 60;
// ---------------------------------------------------------------------------
// Document type weights (normalised to 0-100 scale)
// ---------------------------------------------------------------------------
/**
* Base weight per document type, reflecting parliamentary significance.
* Values are on a 0-100 scale.
*/
const DOC_TYPE_WEIGHTS: Readonly<Record<string, number>> = {
prop: 60, // Government bill
bet: 70, // Committee report (pending vote)
prot: 55, // Plenary minutes
ip: 45, // Interpellation
mot: 35, // Motion
skr: 50, // Government communication
sou: 55, // Government official report (SOU)
ds: 40, // Departmental series
dir: 35, // Committee directive
fr: 25, // Written question
frs: 25, // Response to written question
sfs: 65, // Swedish Code of Statutes
fpm: 40, // EU factual memorandum
};
const DEFAULT_DOC_TYPE_WEIGHT = 30;
const DOC_TYPE_ALIASES: Readonly<Record<string, string>> = {
proposition: 'prop',
propositioner: 'prop',
betankande: 'bet',
betankanden: 'bet',
kommittebetankande: 'bet',
protokoll: 'prot',
interpellation: 'ip',
interpellationer: 'ip',
motion: 'mot',
motioner: 'mot',
skrivelse: 'skr',
fragor: 'fr',
fraga: 'fr',
fraga_svar: 'frs',
question: 'fr',
committee_report: 'bet',
government_bill: 'prop',
};
function normalizeDocType(doc: RawDocument): string {
const fallbackType = (doc as RawDocument & { type?: string }).type;
const rawDocType: string = String(doc.doktyp ?? doc.documentType ?? fallbackType ?? '').trim().toLowerCase();
return DOC_TYPE_ALIASES[rawDocType] ?? rawDocType;
}
// ---------------------------------------------------------------------------
// Signal scorers (each returns 0-100)
// ---------------------------------------------------------------------------
/**
* Score based on the most significant document type in the set.
* Uses the maximum type weight across all documents.
*/
function scoreDocumentType(docs: RawDocument[]): number {
if (docs.length === 0) return 0;
let maxWeight = 0;
for (const doc of docs) {
const docType = normalizeDocType(doc);
const weight = DOC_TYPE_WEIGHTS[docType] ?? DEFAULT_DOC_TYPE_WEIGHT;
if (weight > maxWeight) maxWeight = weight;
}
return maxWeight;
}
/**
* Score based on the volume of documents and breadth of parties involved.
* - Multiple documents increase significance (diminishing returns)
* - Multiple parties (signatories) increase significance
*/
function scoreVolume(docs: RawDocument[]): number {
if (docs.length === 0) return 0;
// Volume: logarithmic scaling for document count
const volumeScore = Math.min(50, Math.round(Math.log2(docs.length + 1) * 15));
// Party breadth: unique parties involved
const parties = new Set<string>();
for (const doc of docs) {
const party = doc.parti;
if (party) parties.add(party.toUpperCase());
}
const partyScore = Math.min(50, parties.size * 10);
return Math.min(100, volumeScore + partyScore);
}
/**
* Score based on opposition pressure: interpellations per unique minister.
* High interpellation density (multiple IPs to same minister) signals
* concentrated political pressure.
*/
function scoreOppositionPressure(docs: RawDocument[]): number {
const interpellations = docs.filter(d =>
normalizeDocType(d) === 'ip'
);
if (interpellations.length === 0) return 0;
// Count IPs per minister (mottagare)
const ministerCounts = new Map<string, number>();
for (const ip of interpellations) {
const minister = (ip.mottagare ?? '').trim();
if (!minister) continue;
ministerCounts.set(minister, (ministerCounts.get(minister) || 0) + 1);
}
if (ministerCounts.size === 0) return 0;
// Max pressure on any single minister
let maxPressure = 0;
for (const count of ministerCounts.values()) {
Eif (count > maxPressure) maxPressure = count;
}
// Scale: 1 IP = 20, 2 = 40, 3 = 60, 4 = 80, 5+ = 100
return Math.min(100, maxPressure * 20);
}
/**
* Score based on historical rarity: whether documents' topics appear
* in a set of recently covered topics.
*
* Documents covering topics NOT seen in the recent window score higher
* (novel topics are more newsworthy).
*
* @param docs - Current document set
* @param recentTopics - Topics covered in the last 30 days (titles/keywords)
*/
function scoreTopicRarity(docs: RawDocument[], recentTopics: string[]): number {
if (docs.length === 0) return 0;
const recentLower = new Set(recentTopics.map(t => t.toLowerCase()));
// Minimum length for a recent topic to be considered for substring matching.
// Very short strings (≤ 3 chars) cause false positives (e.g. "tax" matching "syntax").
const MIN_TOPIC_LENGTH_FOR_MATCH = 4;
let novelCount = 0;
for (const doc of docs) {
const title = (doc.titel || doc.title || '').toLowerCase();
Iif (title.length === 0) continue;
// If none of the recent topics appear as a substring, it's novel
let isNovel = true;
for (const recent of recentLower) {
if (recent.length >= MIN_TOPIC_LENGTH_FOR_MATCH && title.includes(recent)) {
isNovel = false;
break;
}
}
if (isNovel) novelCount++;
}
const novelRatio = novelCount / docs.length;
return Math.round(novelRatio * 100);
}
// ---------------------------------------------------------------------------
// Urgency classification
// ---------------------------------------------------------------------------
/**
* Map a 0-100 score to an editorial urgency label.
*/
export function classifyUrgency(score: number): UrgencyLabel {
if (score >= 80) return 'breaking';
if (score >= 60) return 'major';
if (score >= 40) return 'standard';
return 'background';
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Score a set of parliamentary documents for political significance.
*
* The function is **deterministic**: identical inputs always produce
* identical outputs (no randomness, no date-dependent logic beyond
* the explicit `recentTopics` parameter).
*
* @param docs - Documents to score
* @param recentTopics - Topics covered in the last 30 days (for rarity signal).
* Pass an empty array when historical context is unavailable.
* @returns Composite significance score (0-100), urgency label, and signal breakdown
*/
export function scoreDocuments(
docs: RawDocument[],
recentTopics: string[] = [],
): SignificanceScore {
const docTypeValue = scoreDocumentType(docs);
const volumeValue = scoreVolume(docs);
const pressureValue = scoreOppositionPressure(docs);
const rarityValue = scoreTopicRarity(docs, recentTopics);
const signals: SignalContribution[] = [
{ signal: 'documentType', weight: 0.40, value: docTypeValue },
{ signal: 'volume', weight: 0.20, value: volumeValue },
{ signal: 'oppositionPressure', weight: 0.20, value: pressureValue },
{ signal: 'topicRarity', weight: 0.20, value: rarityValue },
];
const raw = signals.reduce((sum, s) => sum + s.value * s.weight, 0);
const score = Math.max(0, Math.min(100, Math.round(raw)));
return {
score,
urgency: classifyUrgency(score),
signals,
};
}
|