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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | 11x 11x 11x 11x 11x 11x 11x 11x 1312x 139x 328x 328x 2296x 2296x 137x 328x 328x 328x 328x 328x 328x 328x 328x 328x 294x 294x 34x 34x 1x 328x 1x 3x 1x 1x 1x 1x 328x 328x 328x 328x 328x 328x 160x 168x 168x 131x 328x 328x 328x 904x 934x 328x 186x 142x 328x 328x 328x 328x 328x 328x 2624x 328x 328x 2296x 328x 328x 1968x 328x 328x 2624x 328x 328x 328x 328x 328x 328x 328x 328x 325x 325x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 183x 145x 328x 1051x 165x 165x 163x 328x 1476x 41x 41x 287x 328x 1722x 169x 169x 159x 328x 328x 328x 279x 279x 49x 49x 328x 328x 328x 328x 328x 328x 328x 328x 34x 294x 328x 328x 328x 328x 278x 50x 328x 328x 278x 278x 50x 328x 328x 328x 328x 328x 169278x 328x 328x 328x 328x 136x 328x 304x 304x 294x 294x 10x 24x 24x 328x 328x 328x 328x 283x 45x 328x 328x 328x 1968x 1086x 328x 328x 328x 328x 328x 328x 328x 788x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 328x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 281x 281x 1032x 1032x 282x 281x 281x 843x 249x 249x 249x 249x 1x 1x 1x 249x 249x 249x 242x 7x | /**
* @module ai-analysis/quality-assessor
* @description Multi-dimensional quality assessment for generated news articles.
*
* Implements a 6-dimension quality scoring framework:
* 1. Factual Accuracy (25%) — document references match source material
* 2. Stakeholder Coverage (20%) — all relevant perspectives represented
* 3. Analytical Depth (20%) — substance beyond surface description
* 4. Editorial Consistency (15%) — consistent tone, structure, headings
* 5. Evidence Quality (10%) — assertions backed by document references
* 6. Language Quality (10%) — word count, clarity, readability
*
* The assessor runs as two passes:
* Pass 1 — compute dimension scores from HTML content
* Pass 2 — aggregate, flag issues, build suggestions list
*
* @author Hack23 AB
* @license Apache-2.0
*/
import { extractPartyMentions } from '../party-variants.js';
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** Score for a single quality dimension */
export interface DimensionScore {
/** 0–100 score for this dimension */
score: number;
/** Evidence items used to compute the score */
evidence: string[];
/** Suggested improvements for this dimension */
improvements: string[];
}
/** Severity of a quality issue */
export type QualityIssueSeverity = 'critical' | 'major' | 'minor';
/** A single quality problem found in the article */
export interface QualityIssue {
severity: QualityIssueSeverity;
dimension: string;
description: string;
suggestedFix: string;
}
/** Full multi-dimensional quality assessment result */
export interface MultiDimensionalQualityAssessment {
/** 0–100 weighted overall score */
overallScore: number;
/** Per-dimension scores */
dimensions: {
factualAccuracy: DimensionScore;
stakeholderCoverage: DimensionScore;
analyticalDepth: DimensionScore;
editorialConsistency: DimensionScore;
evidenceQuality: DimensionScore;
languageQuality: DimensionScore;
};
/** Issues detected (sorted: critical → major → minor) */
issues: QualityIssue[];
/** Suggested improvements */
suggestions: string[];
/** Whether the article passes the quality threshold */
passesThreshold: boolean;
/** Number of assessment passes performed (always ≥ 2) */
assessmentPasses: number;
}
// ---------------------------------------------------------------------------
// Internal constants
// ---------------------------------------------------------------------------
/** Dimension weights — must sum to 1.0 */
const DIMENSION_WEIGHTS = {
factualAccuracy: 0.25,
stakeholderCoverage: 0.20,
analyticalDepth: 0.20,
editorialConsistency: 0.15,
evidenceQuality: 0.10,
languageQuality: 0.10,
} as const;
/**
* Riksdag/Regering document ID patterns.
* Committee codes may contain non-ASCII Swedish letters (e.g. FöU, CU),
* so we use Unicode-aware boundaries via the `u` flag and explicit non-word
* guards `(?:^|[^\p{L}\p{N}_])` / `(?:$|[^\p{L}\p{N}_])` because the
* standard `\b` anchor is ASCII-only and mishandles ÅÄÖ.
*/
const UNICODE_WORD_START = '(?:^|[^\\p{L}\\p{N}_])'; // Unicode-aware start boundary
const UNICODE_WORD_END = '(?:$|[^\\p{L}\\p{N}_])'; // Unicode-aware end boundary
const DOCUMENT_ID_PATTERNS: readonly RegExp[] = [
new RegExp(`${UNICODE_WORD_START}([\\p{L}]{1,4}\\d{1,4}/\\d{2}:\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(\\d{4}/\\d{2}:[\\p{L}]{1,4}\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(Prop\\.\\s*\\d{4}/\\d{2}:\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(Bet\\.\\s*\\d{4}/\\d{2}:[\\p{L}]{1,4}\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(Mot\\.\\s*\\d{4}/\\d{2}:\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(IP\\s*\\d{4}/\\d{2}:\\d+)${UNICODE_WORD_END}`, 'giu'),
new RegExp(`${UNICODE_WORD_START}(Fr\\.\\s*\\d{4}/\\d{2}:\\d+)${UNICODE_WORD_END}`, 'giu'),
];
/** Words / phrases indicating causal or analytical reasoning */
const CAUSAL_WORDS: readonly string[] = [
'because', 'therefore', 'as a result', 'consequently',
'due to', 'leads to', 'caused by', 'results in',
];
const COMPARATIVE_WORDS: readonly string[] = [
'compared to', 'in contrast', 'while', 'whereas',
'on the other hand', 'however', 'unlike',
];
const TREND_WORDS: readonly string[] = [
'trend', 'pattern', 'shift', 'change', 'evolution', 'development',
];
const EVIDENCE_WORDS: readonly string[] = [
'data shows', 'according to', 'study', 'report', 'statistics',
'evidence', 'figures', 'statistics show',
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function stripHtml(html: string): string {
return html
.replace(/<script[\s>][\s\S]*?<\/script>/gi, ' ')
.replace(/<style[\s>][\s\S]*?<\/style>/gi, ' ')
.replace(/<[^>]*>/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
/** Normalize a document ID for deduplication and comparison.
* Strips all whitespace so format variants like `Prop. 2024/25:1`
* and `Prop.2024/25:1` collapse to the same canonical form.
*/
function normalizeDocId(id: string): string {
return id.replace(/\s+/g, '').toUpperCase();
}
function countDocumentIds(html: string): Set<string> {
const ids = new Set<string>();
for (const pattern of DOCUMENT_ID_PATTERNS) {
const re = new RegExp(pattern.source, pattern.flags.replace('g', '') + 'g');
for (const m of html.matchAll(re)) {
// Group 1 contains the actual document ID; the boundary guards are
// non-capturing context. Fallback to m[0] is a safety net for any
// future pattern without a capture group — should never trigger for
// the current DOCUMENT_ID_PATTERNS.
ids.add(normalizeDocId(m[1] ?? m[0]));
}
}
return ids;
}
/**
* Count how many of the 8 Swedish parliamentary parties are mentioned.
*
* Delegates to the canonical `extractPartyMentions()` from
* `scripts/party-variants.ts` which uses Unicode-aware regex boundaries
* (`\p{L}`, `\p{N}`) for correct matching of Swedish names like
* `Vänsterpartiet` and `Miljöpartiet`.
*/
function countParties(html: string): Set<string> {
const codes = extractPartyMentions(html);
return new Set<string>(codes);
}
// ---------------------------------------------------------------------------
// Pass 1 — dimension score computation
// ---------------------------------------------------------------------------
/**
* Factual Accuracy dimension (25 %)
*
* Measures whether the article references source documents.
* High score: ≥ 5 unique document IDs cited.
* Low score: generic text with no Riksdag/Regering document references.
*/
function assessFactualAccuracy(
html: string,
sourceDocIds: readonly string[],
precomputedDocIds?: Set<string>,
): DimensionScore {
const foundIds = precomputedDocIds ?? countDocumentIds(html);
const evidence: string[] = [];
const improvements: string[] = [];
// Base score from raw document-ID count
const rawCount = foundIds.size;
let score = Math.min(100, Math.round((rawCount / 5) * 100));
if (rawCount === 0) {
improvements.push('Add at least 3–5 references to specific Riksdag documents (e.g. Prop. 2024/25:1, Bet. 2024/25:FiU10)');
score = 0;
} else {
evidence.push(`${rawCount} document ID(s) cited: ${[...foundIds].slice(0, 5).join(', ')}`);
if (rawCount < 3) {
improvements.push('Add more document references for stronger factual grounding');
}
}
// Bonus: verify cited IDs against source list — reward matches, don't penalise
if (sourceDocIds.length > 0 && foundIds.size > 0) {
const normalizedSourceSet = new Set(sourceDocIds.map(normalizeDocId));
const matched = [...foundIds].filter(id => normalizedSourceSet.has(id)).length;
const matchRatio = matched / foundIds.size;
// Add up to 20 bonus points when all cited IDs match source docs
const bonus = Math.round(20 * matchRatio);
score = Math.min(100, score + bonus);
evidence.push(`${matched}/${foundIds.size} cited IDs verified against source documents (+${bonus} bonus)`);
}
return { score, evidence, improvements };
}
/**
* Stakeholder Coverage dimension (20 %)
*
* Swedish parliament has 8 parties; all perspectives should be represented.
* High score: ≥ 6 parties mentioned.
*/
function assessStakeholderCoverage(html: string): DimensionScore {
const parties = countParties(html);
const evidence: string[] = [];
const improvements: string[] = [];
const score = Math.min(100, Math.round((parties.size / 6) * 100));
if (parties.size === 0) {
improvements.push('Include perspectives from at least 4 Swedish parliamentary parties');
} else {
evidence.push(`${parties.size}/8 parties represented: ${[...parties].join(', ')}`);
if (parties.size < 4) {
improvements.push(`Add perspectives from more parties (currently ${parties.size}/8)`);
}
}
// Check for government/opposition balance
const text = stripHtml(html).toLowerCase();
const govTerms = ['government', 'minister', 'statsminister', 'riksdag'];
const oppTerms = ['opposition', 'motions', 'critics', 'debate'];
const hasGov = govTerms.some(t => text.includes(t));
const hasOpp = oppTerms.some(t => text.includes(t));
if (!hasGov || !hasOpp) {
improvements.push('Ensure both government and opposition perspectives are represented');
} else {
evidence.push('Both government and opposition perspectives present');
}
return { score, evidence, improvements };
}
/**
* Analytical Depth dimension (20 %)
*
* Measures substantive analysis beyond surface-level description.
*/
function assessAnalyticalDepth(html: string): DimensionScore {
const strippedText = stripHtml(html);
const text = strippedText.toLowerCase();
const evidence: string[] = [];
const improvements: string[] = [];
let raw = 0;
const causal = CAUSAL_WORDS.filter(w => text.includes(w)).length;
raw += Math.min(causal * 4, 20);
if (causal > 0) evidence.push(`${causal} causal-reasoning indicator(s)`);
const comparative = COMPARATIVE_WORDS.filter(w => text.includes(w)).length;
raw += Math.min(comparative * 4, 20);
if (comparative > 0) evidence.push(`${comparative} comparative-analysis indicator(s)`);
const trend = TREND_WORDS.filter(w => text.includes(w)).length;
raw += Math.min(trend * 4, 20);
if (trend > 0) evidence.push(`${trend} trend-analysis indicator(s)`);
const evidenceW = EVIDENCE_WORDS.filter(w => text.includes(w)).length;
raw += Math.min(evidenceW * 4, 20);
if (evidenceW > 0) evidence.push(`${evidenceW} evidence-based indicator(s)`);
// Multiple perspectives (blockquotes or attributed quotes)
// Count blockquotes plus quoted spans after normalizing common quote forms
// so multilingual text (e.g., curly quotes or ") isn't undercounted.
const normalizedQuoteText = strippedText
.replace(/"/g, '"')
.replace(/[“”„‟«»]/g, '"');
const quotes = (html.match(/<blockquote[\s>]/gi) || []).length +
(normalizedQuoteText.match(/"[^"]{2,}"/g) || []).length;
raw += Math.min(quotes * 4, 20);
if (quotes > 0) evidence.push(`${quotes} attributed quote(s) or blockquote(s)`);
const score = Math.min(100, raw);
if (score < 40) {
improvements.push('Add causal reasoning — explain WHY events are happening, not just WHAT happened');
improvements.push('Include comparative context: how does this compare to previous periods or other countries?');
}
Eif (score < 70) {
improvements.push('Add trend analysis or pattern recognition');
}
return { score, evidence, improvements };
}
/**
* Editorial Consistency dimension (15 %)
*
* Checks for required structural elements: h2 sections, "Why it matters",
* historical context, forward-looking assessment.
*/
function assessEditorialConsistency(html: string): DimensionScore {
const text = stripHtml(html).toLowerCase();
const evidence: string[] = [];
const improvements: string[] = [];
let score = 0;
// h2 analytical sections (30 pts for ≥ 3)
const h2Count = (html.match(/<h2[\s>]/gi) || []).length;
const sectionPts = Math.min(30, Math.round((h2Count / 3) * 30));
score += sectionPts;
if (h2Count >= 3) {
evidence.push(`${h2Count} analytical h2 sections`);
} else {
improvements.push(`Add more analytical sections (have ${h2Count}, need ≥ 3 h2 headings)`);
}
// "Why it matters" (20 pts)
const whyPatterns = [/why\s+this\s+matters/i, /varför\s+detta/i, /implications/i, /konsekvenser/i];
if (whyPatterns.some(p => p.test(html))) {
score += 20;
evidence.push('"Why it matters" section present');
} else {
improvements.push('Add a "Why This Matters" section explaining policy implications');
}
// Historical context (20 pts)
const histPatterns = [/historically/i, /in \d{4}/, /since \d{4}/, /tidigare/i, /historiskt/i];
if (histPatterns.some(p => p.test(text))) {
score += 20;
evidence.push('Historical context present');
} else {
improvements.push('Add historical context to provide background');
}
// Forward-looking assessment (15 pts)
const fwdWords = ['next', 'upcoming', 'future', 'expected', 'will', 'coming weeks', 'framöver', 'nästa'];
if (fwdWords.some(w => text.includes(w))) {
score += 15;
evidence.push('Forward-looking assessment present');
} else {
improvements.push('Add a forward-looking "What happens next" assessment');
}
// Article structure (back-to-news, language switcher — 15 pts)
const hasLanguageSwitcher = /class=["'][^"']*\blanguage-switcher\b/.test(html);
const hasBackToNews = /class=["'][^"']*\bback-to-news\b/.test(html);
if (hasLanguageSwitcher && hasBackToNews) {
score += 15;
evidence.push('Navigation structure complete');
} else {
Eif (!hasLanguageSwitcher) improvements.push('Language switcher nav missing');
Eif (!hasBackToNews) improvements.push('Back-to-news link missing');
}
return { score: Math.min(100, score), evidence, improvements };
}
/**
* Evidence Quality dimension (10 %)
*
* Measures how well assertions are backed by specific document references,
* source links, or official statistics.
*/
function assessEvidenceQuality(html: string, precomputedDocIds?: Set<string>): DimensionScore {
const evidence: string[] = [];
const improvements: string[] = [];
let score = 0;
// Document IDs as evidence anchors (40 pts for ≥ 3)
const docIds = precomputedDocIds ?? countDocumentIds(html);
const docPts = Math.min(40, Math.round((docIds.size / 3) * 40));
score += docPts;
if (docIds.size > 0) {
evidence.push(`${docIds.size} document reference(s)`);
} else {
improvements.push('Add specific document references (Riksdag/Regering IDs)');
}
// Source links in article (30 pts for ≥ 2 external hrefs)
const externalLinks = (html.match(/href="https?:\/\//g) || []).length;
const linkPts = Math.min(30, Math.round((externalLinks / 2) * 30));
score += linkPts;
if (externalLinks > 0) {
evidence.push(`${externalLinks} source link(s)`);
} else {
improvements.push('Add source links to official documents or press releases');
}
// Sources section (30 pts) — match the canonical `article-sources` container
// and common class/heading variants used by templates and localizations.
const hasSources = /class=["'][^"']*\barticle-sources\b/.test(html) || /class=["'][^"']*\bsources\b/.test(html) || /<h[23][^>]*>\s*Sources/i.test(html);
if (hasSources) {
score += 30;
evidence.push('Sources section present');
} else {
improvements.push('Add a dedicated Sources section listing all cited documents');
}
return { score: Math.min(100, score), evidence, improvements };
}
/**
* Language Quality dimension (10 %)
*
* Measures readability and clarity: word count, paragraph structure,
* absence of untranslated markers.
*/
function assessLanguageQuality(html: string, lang: string): DimensionScore {
const text = stripHtml(html);
const evidence: string[] = [];
const improvements: string[] = [];
let score = 0;
// Word count (40 pts for ≥ 1 000 words)
const wordCount = text.split(/\s+/).filter(w => w.length > 0).length;
const wordPts = Math.min(40, Math.round((wordCount / 1000) * 40));
score += wordPts;
evidence.push(`${wordCount} words`);
if (wordCount < 400) {
improvements.push('Expand article to at least 600–1 000 words for adequate depth');
}
// Translation completeness — non-Swedish articles should have no data-translate markers (30 pts)
if (lang !== 'sv') {
const untranslated = (html.match(/data-translate="true"/g) || []).length;
if (untranslated === 0) {
score += 30;
evidence.push('No untranslated markers');
} else {
improvements.push(`${untranslated} untranslated Swedish span(s) — complete translation`);
}
} else {
score += 30;
evidence.push('Swedish source language — translation check not applicable');
}
// Paragraph structure (30 pts for ≥ 5 paragraphs)
const paragraphs = (html.match(/<p[\s>]/gi) || []).length;
const paraPts = Math.min(30, Math.round((paragraphs / 5) * 30));
score += paraPts;
if (paragraphs >= 5) {
evidence.push(`${paragraphs} paragraph(s)`);
} else {
improvements.push(`Add more paragraphs (have ${paragraphs}, need ≥ 5)`);
}
return { score: Math.min(100, score), evidence, improvements };
}
// ---------------------------------------------------------------------------
// Pass 2 — aggregation + issue detection
// ---------------------------------------------------------------------------
function buildIssues(
dims: MultiDimensionalQualityAssessment['dimensions'],
threshold: number,
): QualityIssue[] {
const issues: QualityIssue[] = [];
const add = (
severity: QualityIssueSeverity,
dimension: string,
score: number,
description: string,
suggestedFix: string,
) => {
if (score < threshold) {
issues.push({ severity, dimension, description, suggestedFix });
}
};
add('critical', 'factualAccuracy', dims.factualAccuracy.score, 'Low factual accuracy score — insufficient document references', 'Add Riksdag/Regering document IDs that support each claim');
add('major', 'stakeholderCoverage', dims.stakeholderCoverage.score, 'Inadequate stakeholder coverage — missing party perspectives', 'Include positions of all major parliamentary parties');
add('major', 'analyticalDepth', dims.analyticalDepth.score, 'Insufficient analytical depth — article is descriptive, not analytical', 'Add causal reasoning, comparisons, and trend analysis');
add('major', 'editorialConsistency', dims.editorialConsistency.score, 'Editorial structure incomplete', 'Add h2 sections, "Why it matters", historical context');
add('minor', 'evidenceQuality', dims.evidenceQuality.score, 'Evidence quality below threshold', 'Add source links and a dedicated Sources section');
add('minor', 'languageQuality', dims.languageQuality.score, 'Language quality below threshold', 'Expand word count, fix untranslated markers, add paragraphs');
// Sort: critical → major → minor
const order: Record<QualityIssueSeverity, number> = { critical: 0, major: 1, minor: 2 };
return issues.sort((a, b) => order[a.severity] - order[b.severity]);
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Run multi-dimensional quality assessment on a generated article.
*
* The assessment performs two passes:
* Pass 1 — compute all six dimension scores from the HTML content
* Pass 2 — aggregate dimensions into a weighted overall score and build the issues list
*
* Always returns `assessmentPasses >= 2`.
*
* @param html - Full HTML content of the article
* @param lang - ISO 639-1 language code (e.g. "en")
* @param sourceDocIds - Document IDs available in the source data for cross-checking
* @param passThreshold - Minimum overall score to pass (0–100, default 60)
*/
export function assessArticleQuality(
html: string,
lang: string,
sourceDocIds: readonly string[] = [],
passThreshold = 60,
): MultiDimensionalQualityAssessment {
// ── Pass 1: compute dimension scores ──────────────────────────────────────
// Pre-compute document IDs once so both factualAccuracy and evidenceQuality
// reuse the same set without scanning the HTML twice.
const docIds = countDocumentIds(html);
const factualAccuracy = assessFactualAccuracy(html, sourceDocIds, docIds);
const stakeholderCoverage = assessStakeholderCoverage(html);
const analyticalDepth = assessAnalyticalDepth(html);
const editorialConsistency = assessEditorialConsistency(html);
const evidenceQuality = assessEvidenceQuality(html, docIds);
const languageQuality = assessLanguageQuality(html, lang);
const dimensions = {
factualAccuracy,
stakeholderCoverage,
analyticalDepth,
editorialConsistency,
evidenceQuality,
languageQuality,
};
// ── Pass 2: aggregate + issues ────────────────────────────────────────────
const overallScore = Math.round(
factualAccuracy.score * DIMENSION_WEIGHTS.factualAccuracy +
stakeholderCoverage.score * DIMENSION_WEIGHTS.stakeholderCoverage +
analyticalDepth.score * DIMENSION_WEIGHTS.analyticalDepth +
editorialConsistency.score * DIMENSION_WEIGHTS.editorialConsistency +
evidenceQuality.score * DIMENSION_WEIGHTS.evidenceQuality +
languageQuality.score * DIMENSION_WEIGHTS.languageQuality,
);
const dimensionThreshold = passThreshold * 0.6; // dimension-level warning at 60% of overall threshold
const issues = buildIssues(dimensions, dimensionThreshold);
// Gather all improvement suggestions
const suggestions: string[] = [
...factualAccuracy.improvements,
...stakeholderCoverage.improvements,
...analyticalDepth.improvements,
...editorialConsistency.improvements,
...evidenceQuality.improvements,
...languageQuality.improvements,
];
return {
overallScore,
dimensions,
issues,
suggestions,
passesThreshold: overallScore >= passThreshold,
assessmentPasses: 2, // Two assessment passes: Pass 1 computes dimension scores, Pass 2 aggregates and builds issues
};
}
/**
* Render a quality assessment as a concise console report.
*/
export function printQualityReport(
assessment: MultiDimensionalQualityAssessment,
filename: string,
): void {
const { overallScore, passesThreshold, dimensions, issues, suggestions, assessmentPasses } = assessment;
const label = passesThreshold ? '✅' : '⚠️';
console.log(`\n📊 Multi-Dimensional Quality Report: ${filename.replace(/\.html$/, '')}`);
console.log(` Overall Score: ${overallScore}/100 — ${passesThreshold ? 'PASSED' : 'BELOW THRESHOLD'} ${label}`);
console.log(` Assessment Passes: ${assessmentPasses}`);
console.log(` Factual Accuracy: ${dimensions.factualAccuracy.score}/100 (weight 25%)`);
console.log(` Stakeholder Coverage: ${dimensions.stakeholderCoverage.score}/100 (weight 20%)`);
console.log(` Analytical Depth: ${dimensions.analyticalDepth.score}/100 (weight 20%)`);
console.log(` Editorial Consistency: ${dimensions.editorialConsistency.score}/100 (weight 15%)`);
console.log(` Evidence Quality: ${dimensions.evidenceQuality.score}/100 (weight 10%)`);
console.log(` Language Quality: ${dimensions.languageQuality.score}/100 (weight 10%)`);
if (issues.length > 0) {
console.log(` Issues (${issues.length}):`);
for (const issue of issues.slice(0, 5)) {
const sev = issue.severity === 'critical' ? '🔴' : issue.severity === 'major' ? '🟡' : '🔵';
console.log(` ${sev} [${issue.dimension}] ${issue.description}`);
}
}
if (!passesThreshold && suggestions.length > 0) {
console.log(` Top suggestions:`);
for (const s of suggestions.slice(0, 3)) {
console.log(` → ${s}`);
}
}
}
/**
* Inject quality metadata into article HTML as a `<meta>` tag and,
* optionally, an inline JSON-LD block.
*
* **CSP note:** An inline `<script type="application/ld+json">` element is
* still an inline script from the browser's perspective. If the page's
* Content-Security-Policy sets `script-src` without `'unsafe-inline'`, the
* JSON-LD block will be blocked by the browser. To keep things safe:
*
* - The `<meta name="quality-score">` tag is injected when a `</head>`
* insertion point is found (CSP-safe).
* - The JSON-LD `<script>` block is only injected when `injectJsonLd` is
* `true` (default: `false`). Callers that know their CSP allows inline
* scripts can opt in.
* - If the HTML contains no `</head>` tag the function returns the original
* HTML unchanged (no injection possible without a `<head>` section).
*
* @param html - Original article HTML
* @param assessment - Quality assessment result
* @param injectJsonLd - If `true`, also add the JSON-LD script block
* (requires CSP to permit inline scripts). Default: `false`.
* @returns Modified HTML with quality metadata injected, or the
* original HTML if no `</head>` tag is present.
*/
export function injectQualityMetadata(
html: string,
assessment: MultiDimensionalQualityAssessment,
injectJsonLd = false,
): string {
const { overallScore, dimensions, passesThreshold, assessmentPasses } = assessment;
// Always inject the CSP-safe <meta> tag
const metaTag = ` <meta name="quality-score" content="${overallScore}">`;
let injection = metaTag;
// Only inject inline JSON-LD when explicitly opted in (requires CSP 'unsafe-inline')
if (injectJsonLd) {
const jsonLd = JSON.stringify({
'@context': 'https://schema.org',
'@type': 'Rating',
ratingValue: overallScore,
bestRating: 100,
worstRating: 0,
ratingExplanation: `Multi-dimensional quality assessment (${assessmentPasses} passes)`,
additionalProperty: [
{ '@type': 'PropertyValue', name: 'passesThreshold', value: passesThreshold },
{ '@type': 'PropertyValue', name: 'factualAccuracy', value: dimensions.factualAccuracy.score },
{ '@type': 'PropertyValue', name: 'stakeholderCoverage', value: dimensions.stakeholderCoverage.score },
{ '@type': 'PropertyValue', name: 'analyticalDepth', value: dimensions.analyticalDepth.score },
{ '@type': 'PropertyValue', name: 'editorialConsistency', value: dimensions.editorialConsistency.score },
{ '@type': 'PropertyValue', name: 'evidenceQuality', value: dimensions.evidenceQuality.score },
{ '@type': 'PropertyValue', name: 'languageQuality', value: dimensions.languageQuality.score },
],
});
const jsonLdTag = ` <script type="application/ld+json">\n ${jsonLd}\n </script>`;
injection = `${metaTag}\n${jsonLdTag}`;
}
// Insert before </head> (case-insensitive to handle <\/HEAD> etc.)
const headCloseRe = /<\/head\s*>/i;
const headMatch = headCloseRe.exec(html);
if (headMatch) {
return html.replace(headMatch[0], `${injection}\n${headMatch[0]}`);
}
return html;
}
|