mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* refactor(country-maps): consolidate country name/ISO maps Expand shared/country-names.json from 265 to 309 entries by merging geojson names, COUNTRY_ALIAS_MAP, upstream API variants (World Bank, WHO, UN, FAO), and seed-correlation extras. Add ISO3 map generator (generate-iso3-maps.cjs) producing iso3-to-iso2.json (239 entries) and iso2-to-iso3.json (239 entries) with TWN and XKX supplements. Add build-country-names.cjs for reproducible expansion from all sources. Sync scripts/shared/ copies for edge-function test compatibility. * refactor: consolidate country name/code mappings into single canonical sources Eliminates fragmented country mapping across the repo. Every feature (resilience, conflict, correlation, intelligence) was maintaining its own partial alias map. Data consolidation: - Expand shared/country-names.json from 265 to 302 entries covering World Bank, WHO, UN, FAO, and correlation script naming variants - Generate shared/iso3-to-iso2.json (239 entries) and shared/iso2-to-iso3.json from countries.geojson + supplements (Taiwan TWN, Kosovo XKX) Consumer migrations: - _country-resolver.mjs: delete COUNTRY_ALIAS_MAP (37 entries), replace 2MB geojson parse with 5KB iso3-to-iso2.json - conflict/_shared.ts: replace 33-entry ISO2_TO_ISO3 literal - seed-conflict-intel.mjs: replace 20-entry ISO2_TO_ISO3 literal - _dimension-scorers.ts: replace geojson-based ISO3 construction - get-risk-scores.ts: replace 31-entry ISO3_TO_ISO2 literal - seed-correlation.mjs: replace 102-entry COUNTRY_NAME_TO_ISO2 and 90-entry ISO3_TO_ISO2, use resolveIso2() from canonical resolver, lower short-alias threshold to 2 chars with word boundary matching, export matchCountryNamesInText(), add isMain guard Tests: - New tests/country-resolver.test.mjs with structural validation, parity regression for all 37 old aliases, ISO3 bidirectional consistency, and Taiwan/Kosovo assertions - Updated resilience seed test for new resolver signature Net: -190 lines, 0 hardcoded country maps remaining * fix: normalize raw text before country name matching Text matchers (geo-extract, seed-security-advisories, seed-correlation) were matching normalized keys against raw text containing diacritics and punctuation. "Curaçao", "Timor-Leste", "Hong Kong S.A.R." all failed to resolve after country-names.json keys were normalized. Fix: apply NFKD + diacritic stripping + punctuation normalization to input text before matching, same transform used on the keys. Also add "hong kong" and "sao tome" as short-form keys for bigram headline matching in geo-extract. * fix: remove 'u s' alias that caused US/VI misattribution 'u s' in country-names.json matched before 'u s virgin islands' in geo-extract's bigram scanner, attributing Virgin Islands headlines to US. Removed since 'usa', 'united states', and the uppercase US expansion already cover the United States.
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const root = path.resolve(__dirname, '..');
|
|
const geojson = JSON.parse(fs.readFileSync(path.join(root, 'public', 'data', 'countries.geojson'), 'utf8'));
|
|
|
|
const iso3ToIso2 = {};
|
|
const discrepancies = [];
|
|
|
|
for (const f of geojson.features) {
|
|
const props = f.properties || {};
|
|
const iso2 = String(props['ISO3166-1-Alpha-2'] || '').trim();
|
|
const iso3 = String(props['ISO3166-1-Alpha-3'] || '').trim();
|
|
|
|
if (!/^[A-Z]{2}$/.test(iso2)) {
|
|
if (/^[A-Z]{3}$/.test(iso3)) {
|
|
discrepancies.push(`Skipped ${iso3} (${props.name}): invalid ISO2 "${props['ISO3166-1-Alpha-2']}"`);
|
|
}
|
|
continue;
|
|
}
|
|
if (!/^[A-Z]{3}$/.test(iso3)) {
|
|
discrepancies.push(`Skipped ${props.name} (${iso2}): invalid ISO3 "${props['ISO3166-1-Alpha-3']}"`);
|
|
continue;
|
|
}
|
|
iso3ToIso2[iso3] = iso2;
|
|
}
|
|
|
|
// Supplements for missing/invalid entries
|
|
if (!iso3ToIso2['TWN']) {
|
|
iso3ToIso2['TWN'] = 'TW';
|
|
console.log('Added supplement: TWN → TW (Taiwan has CN-TW in geojson)');
|
|
}
|
|
if (!iso3ToIso2['XKX']) {
|
|
iso3ToIso2['XKX'] = 'XK';
|
|
console.log('Added supplement: XKX → XK (Kosovo absent from geojson)');
|
|
}
|
|
|
|
// Sort by key
|
|
const sorted3to2 = Object.fromEntries(
|
|
Object.entries(iso3ToIso2).sort(([a], [b]) => a.localeCompare(b))
|
|
);
|
|
|
|
// Invert: ISO2 → ISO3
|
|
const iso2ToIso3 = {};
|
|
for (const [iso3, iso2] of Object.entries(sorted3to2)) {
|
|
if (!iso2ToIso3[iso2]) {
|
|
iso2ToIso3[iso2] = iso3;
|
|
}
|
|
}
|
|
const sorted2to3 = Object.fromEntries(
|
|
Object.entries(iso2ToIso3).sort(([a], [b]) => a.localeCompare(b))
|
|
);
|
|
|
|
// Write files
|
|
const out3to2 = path.join(root, 'shared', 'iso3-to-iso2.json');
|
|
fs.writeFileSync(out3to2, JSON.stringify(sorted3to2, null, 2) + '\n');
|
|
console.log(`Wrote ${Object.keys(sorted3to2).length} entries to ${out3to2}`);
|
|
|
|
const out2to3 = path.join(root, 'shared', 'iso2-to-iso3.json');
|
|
fs.writeFileSync(out2to3, JSON.stringify(sorted2to3, null, 2) + '\n');
|
|
console.log(`Wrote ${Object.keys(sorted2to3).length} entries to ${out2to3}`);
|
|
|
|
if (discrepancies.length) {
|
|
console.log(`\nDiscrepancies (${discrepancies.length}):`);
|
|
for (const d of discrepancies) console.log(` ${d}`);
|
|
}
|