feat: estado e cidade do filtro e da árvore também a partir de coverage e outras grafias

This commit is contained in:
2026-09-30 19:17:13 +02:00
parent efed9f28a0
commit 4f2ec80ad4
+104 -19
View File
@@ -1448,36 +1448,106 @@ const UF = { AC: 'Acre', AL: 'Alagoas', AP: 'Amapá', AM: 'Amazonas', BA: 'Bahia
RS: 'Rio Grande do Sul', RO: 'Rondônia', RR: 'Roraima', SC: 'Santa Catarina', SP: 'São Paulo', SE: 'Sergipe', TO: 'Tocantins' }; RS: 'Rio Grande do Sul', RO: 'Rondônia', RR: 'Roraima', SC: 'Santa Catarina', SP: 'São Paulo', SE: 'Sergipe', TO: 'Tocantins' };
const STATE_BY_KEY = new Map(Object.values(UF).map(n => [deburr(n), n])); const STATE_BY_KEY = new Map(Object.values(UF).map(n => [deburr(n), n]));
// ── places, from subject AND coverage, in the many ways the catalogue writes them:
// "Porto Velho (RO)" · "Recife, PE" · "Rio de Janeiro - RJ" · "Recife, Pernambuco, Brasil"
// "Bahia (Estado)" · "RJ" · "Centro (Rio de Janeiro, RJ)" · bare "Recife" (learned below)
const UF_CODE = /^[A-Z]{2}$/;
const CITY_UF = new Map();
const CANON = new Map(); // deburred city → its most common spelling ("Rio de janeiro" → "Rio de Janeiro")
const HOOD = new Map(); // deburred bairro → {state, city}, learned from "Bairro (Cidade, UF)"
const FEATURE = /^(?:ba[ií]a|morro|ilha|serra|lago|lagoa|floresta|estrada|avenida|rua|pra[cç]a|largo|parque|jardim|campo|cais|porto|rio|igreja|esta[cç][aã]o|forte|palacio|palácio|centro)\b/i; // deburred city → its UF, learned from the explicit forms
const stateOf = x => UF_CODE.test(x) ? UF[x] : STATE_BY_KEY.get(deburr(x));
function parsePlaceExplicit(raw) {
const t = raw.trim().replace(/\.$/, '').replace(/,\s*(?:brasil|brazil)$/i, '').replace(/^\d{4}(?:-\d{4})?\s*:\s*/, '');
let m;
if ((m = t.match(/^(.+?)\s*\((?:estado|brazil\s*:\s*state)\)$/i)) && stateOf(m[1].trim())) return { state: stateOf(m[1].trim()) };
if (UF_CODE.test(t) && UF[t]) return { state: UF[t] };
if (STATE_BY_KEY.has(deburr(t))) return { state: STATE_BY_KEY.get(deburr(t)), city: /^(rio de janeiro|sao paulo)$/.test(deburr(t)) ? t : null };
// "Bairro (Cidade, UF)"
if ((m = t.match(/^(.+?)\s*\(([^(),]+),\s*([A-Za-z]{2})\)$/)) && UF[m[3].toUpperCase()] && !/\s-\s/.test(m[1]))
return { state: UF[m[3].toUpperCase()], city: m[2].replace(/^\d{4}(?:-\d{4})?\s*:\s*/, '').trim(), sub: m[1].trim() };
if (/\s-\s/.test(t.replace(/\s+-\s+(?=[A-Z]{2}$)/, ''))) return null; // a topic path, handled by the caller
// "Cidade (UF)" · "Cidade, UF" · "Cidade - UF" · "Cidade(UF)"
if ((m = t.match(/^(.+?)\s*(?:\(|,\s*|\s-\s*)([A-Z]{2})\)?$/)) && UF[m[2]]) return { state: UF[m[2]], city: m[1].trim() };
// "Cidade (Estado por extenso)" · "Cidade, Estado por extenso"
if ((m = t.match(/^(.+?)\s*(?:\(|,\s*)([^(),]+)\)?$/)) && STATE_BY_KEY.has(deburr(m[2]))) return { state: STATE_BY_KEY.get(deburr(m[2])), city: m[1].trim() };
return null;
}
function parsePlace(raw) {
const p = parsePlaceExplicit(raw);
if (p) {
if (p.city) p.city = CANON.get(deburr(p.city)) || p.city;
// "Ceará, CE": the state written twice is no city (Rio de Janeiro and São Paulo are both)
if (p.city && deburr(p.city) === deburr(p.state) && !/^(rio de janeiro|sao paulo)$/.test(deburr(p.state))) p.city = null;
return p;
}
const k = deburr(raw.trim().replace(/\.$/, ''));
const uf = CITY_UF.get(k);
return uf ? { state: UF[uf], city: raw.trim().replace(/\.$/, '') } : null;
}
const NO_PLACE = /^\[?s\.?\s?l\.?\]?$|^\[?s\.?\s?n\.?\]?$/i;
// every place-bearing tag of a piece, PT side only
function geoTags(meta) {
const seen = new Set(), out = [];
const add = t => {
const k = subjectKey(t);
if (!k || seen.has(k) || NO_PLACE.test(String(t).trim())) return;
seen.add(k); out.push(String(t).trim());
};
subjectsOf(meta).forEach(add);
const cov = [].concat((meta || {}).coverage || []).map(t => String(t).trim()).filter(t => !EN_WORD.test(t));
cov.forEach(add);
return out;
}
function subjectTree(meta) { function subjectTree(meta) {
const root = { kids: new Map() }; const root = { kids: new Map() };
const put = path => path.reduce((n, label) => { const put = path => path.reduce((n, label) => {
if (!n.kids.has(label)) n.kids.set(label, { kids: new Map(), place: false }); if (!n.kids.has(label)) n.kids.set(label, { kids: new Map(), place: false });
return n.kids.get(label); return n.kids.get(label);
}, root); }, root);
const place = path => path.forEach((_, i) => { put(path.slice(0, i + 1)).place = true; });
const others = []; const others = [];
let hasPlace = false; let hasPlace = false;
subjectsOf(meta).forEach(tag => { const tags = geoTags(meta);
const key = deburr(tag).replace(/\s*\(estado\)$/, ''); // a bairro / feature ("Centro", "Baía de Guanabara") is not a city: it hangs under the
if (key === 'brasil') { put(['Brasil']).place = true; hasPlace = true; return; } // piece's real city, or under the city it was learned to belong to
if (STATE_BY_KEY.has(key)) { const cities = new Set();
const st = STATE_BY_KEY.get(key); tags.forEach(tag => { const p = parsePlace(tag); if (p && p.city && !FEATURE.test(p.city) && !HOOD.has(deburr(p.city))) cities.add(p.state + '|' + p.city); });
put(['Brasil']).place = true; put(['Brasil', st]).place = true; hasPlace = true; return; tags.forEach(tag => {
const key = deburr(tag).replace(/\.$/, '');
if (key === 'brasil' || key === 'brazil') { place(['Brasil']); hasPlace = true; return; }
let p = parsePlace(tag);
if (p && p.city && !p.sub && (FEATURE.test(p.city) || HOOD.has(deburr(p.city)))) {
const h = HOOD.get(deburr(p.city));
const real = h ? { state: h.state, city: h.city } : [...cities].map(c => c.split('|')).filter(c => c[0] === p.state).map(c => ({ state: c[0], city: c[1] }))[0];
p = real ? { state: real.state, city: real.city, sub: p.city } : { state: p.state, city: null, sub: p.city };
} }
const parts = tag.split(/\s+-\s+/); if (p) {
// "Espírito Santo (Estado) - Colônias": a state carrying a topic const path = ['Brasil', p.state].concat(p.city ? [p.city] : []);
const sm = parts[0].match(/^(.+?)\s*\(Estado\)$/i); place(path);
if (sm && STATE_BY_KEY.has(deburr(sm[1]))) { if (p.sub) put(path.concat(p.sub)).place = true;
const path = ['Brasil', STATE_BY_KEY.get(deburr(sm[1]))];
path.forEach((_, i) => { put(path.slice(0, i + 1)).place = true; });
parts.slice(1).forEach((_, i) => put(path.concat(parts.slice(1, i + 2))));
hasPlace = true; return; hasPlace = true; return;
} }
const m = parts[parts.length - 1].match(/^(.+?)\s*\(([A-Z]{2})\)$/); // "Espírito Santo (Estado) - Colônias": a state carrying a topic
if (!m || !UF[m[2]]) { others.push(tag); return; } const parts = tag.split(/\s+-\s+/);
const path = ['Brasil', UF[m[2]], m[1]]; const sm = parts[0].match(/^(.+?)\s*\(Estado\)$/i);
path.slice(0, 3).forEach((_, i) => { put(path.slice(0, i + 1)).place = true; }); if (parts.length > 1 && sm && STATE_BY_KEY.has(deburr(sm[1]))) {
parts.slice(0, -1).forEach((_, i) => { put(path.concat(parts.slice(0, i + 1))); }); const path = ['Brasil', STATE_BY_KEY.get(deburr(sm[1]))];
hasPlace = true; place(path); hasPlace = true;
parts.slice(1).forEach((_, i) => put(path.concat(parts.slice(1, i + 2))));
return;
}
// "Tópico - Cidade (UF)": the last segment carries the place
const lp = parts.length > 1 ? parsePlace(parts[parts.length - 1]) : null;
if (lp && lp.city) {
const path = ['Brasil', lp.state, lp.city];
place(path); hasPlace = true;
parts.slice(0, -1).forEach((_, i) => put(path.concat(parts.slice(0, i + 1))));
return;
}
others.push(tag);
}); });
// a lone "Brasil" is no tree — it reads better as a chip with the rest // a lone "Brasil" is no tree — it reads better as a chip with the rest
if (hasPlace && root.kids.get('Brasil').kids.size === 0) { root.kids.clear(); others.push('Brasil'); } if (hasPlace && root.kids.get('Brasil').kids.size === 0) { root.kids.clear(); others.push('Brasil'); }
@@ -1532,8 +1602,23 @@ function subjectFacets(meta) {
return { st, ct, tp }; return { st, ct, tp };
} }
function learnCities(entries) {
const votes = new Map(), spell = new Map();
entries.forEach(e => geoTags(e.meta).forEach(t => {
const p = parsePlaceExplicit(t);
if (!p || !p.city) return;
{ const k = deburr(p.city), v = spell.get(k) || new Map(); v.set(p.city, (v.get(p.city) || 0) + 1); spell.set(k, v); }
if (p.sub) HOOD.set(deburr(p.sub), { state: p.state, city: p.city });
const uf = Object.keys(UF).find(k => UF[k] === p.state), k = deburr(p.city);
const v = votes.get(k) || new Map(); v.set(uf, (v.get(uf) || 0) + 1); votes.set(k, v);
}));
spell.forEach((v, k) => CANON.set(k, [...v].sort((a, b) => b[1] - a[1])[0][0]));
votes.forEach((v, k) => { const [uf, n] = [...v].sort((a, b) => b[1] - a[1])[0]; if (n >= 2 && !STATE_BY_KEY.has(k)) CITY_UF.set(k, uf); });
}
function setupFilters(mapData) { function setupFilters(mapData) {
const entries = Object.values(mapData.map); const entries = Object.values(mapData.map);
learnCities(entries);
const instCounts = new Map(), typeCounts = new Map(), authorGroups = new Map(); const instCounts = new Map(), typeCounts = new Map(), authorGroups = new Map();
let minYear = Infinity, maxYear = -Infinity; let minYear = Infinity, maxYear = -Infinity;