Fix parsers: laboratorio y estado acido-base

This commit is contained in:
2026-04-16 11:45:57 -03:00
parent 8070709332
commit 2638a68831
+49 -42
View File
@@ -727,16 +727,17 @@ return res;
for (const [clave, info] of Object.entries(mapeoParametros)) {
if (lineaLower.includes(clave)) {
const cleanLinea = linea.replace(/show_chart|list_alt/g, '').trim();
const partes = cleanLinea.split(/\s+/);
const cleanLinea = linea.replace(/show_chart|list_alt/g, '').replace(/\t+/g, ' ').replace(/\s+/g, ' ').trim();
const partes = cleanLinea.split(' ');
for (let i = 0; i < partes.length; i++) {
const parte = partes[i].replace(',', '.');
const valor = parseFloat(parte);
if (!isNaN(valor) && valor > 0 && valor < 1000) {
const esUnidadSiguiente = partes[i + 1] && /^(10[\^*]?[³6]?[\/]?µl|%|g\/dl|mg\/dl|mEq\/L|u\/l|fL|pg|seg|ng\/ml)?$/i.test(partes[i + 1]);
const sig = partes[i + 1] || '';
const esUnidad = /^(10[^µ]*µl|%|g\/dl|mg\/dl|mEq\/L|u\/l|fL|pg|seg|ng\/ml)/i.test(sig) || /^10[\^*]?[³6]?[\/]?µl$/i.test(sig);
if (!esUnidadSiguiente) {
if (!esUnidad || i === 0) {
const nombreNormalizado = info.nombre;
let valorFinal = valor;
@@ -780,59 +781,65 @@ if (!esUnidadSiguiente) {
let fecha = importFecha;
for (const linea of lineas) {
const lineaLower = linea.toLowerCase().trim();
const cleanLinea = linea.replace(/show_chart|list_alt|\t+/g, ' ').replace(/,/g, '.').replace(/\s+/g, ' ').trim();
const cleanLinea = linea.replace(/show_chart|list_alt|\t+/g, ' ').replace(/,/g, '.').replace(/\s+/g, ' ').trim().toLowerCase();
const getValue = (param: string): number | undefined => {
const idx = cleanLinea.indexOf(param);
if (idx === -1) return undefined;
const after = cleanLinea.slice(idx + param.length).trim();
const match = after.match(/(\d+\.?\d*)/);
return match ? parseFloat(match[1]) : undefined;
const parts = after.split(' ');
for (const p of parts) {
const v = parseFloat(p);
if (!isNaN(v) && v > 0 && v < 1000) return v;
}
return undefined;
};
if (cleanLinea.includes('ph') && !cleanLinea.includes('pco2') && !cleanLinea.includes('pco')) {
ph = getValue('ph');
} else if (cleanLinea.includes('pco2') || cleanLinea.includes('pco₂')) {
pco2 = getValue('pco2') || getValue('pco');
} else if (cleanLinea.includes('po2') || cleanLinea.includes('po₂')) {
po2 = getValue('po2') || getValue('po');
} else if (cleanLinea.includes('hco3')) {
if (cleanLinea.includes('estado') && cleanLinea.includes('ácido') || cleanLinea.includes('base')) {
ph = getValue('ph') || (cleanLinea.match(/ph\s+(\d+\.?\d*)/)?.[1] ? parseFloat(cleanLinea.match(/ph\s+(\d+\.?\d*)/)![1]) : undefined);
pco2 = getValue('pco2') || getValue('pco₂');
po2 = getValue('po2') || getValue('po');
hco3 = getValue('hco3');
} else if (cleanLinea.includes('exceso de base') || cleanLinea.includes('exceso de bases') || cleanLinea.includes('base excess')) {
be = getValue('exceso de base') || getValue('base excess');
} else if (cleanLinea.includes('saturación de oxígeno') || cleanLinea.includes('saturación de oxigeno') || cleanLinea.includes('sato2')) {
sato2 = getValue('saturación de oxígeno') || getValue('saturación de oxigeno') || getValue('sato2');
} else if (cleanLinea.includes('lactato')) {
be = getValue('exceso de base') || getValue('base excess') || getValue('exceso');
sato2 = getValue('saturación') || getValue('sato2') || getValue('sat');
lactato = getValue('lactato');
} else if (cleanLinea.includes('fio2') || cleanLinea.includes('fi o₂')) {
fio2 = getValue('fio2') || getValue('fi o2');
} else if (cleanLinea.match(/\d{4}-\d{2}-\d{2}/)) {
const fechaMatch = cleanLinea.match(/(\d{4}-\d{2}-\d{2})/);
if (fechaMatch) fecha = fechaMatch[1];
fio2 = getValue('fio2');
break;
}
}
console.log('AB parsing:', { ph, pco2, po2, hco3, be, sato2 });
if (ph !== undefined) {
const hora = new Date().toTimeString().slice(0, 5);
return {
pacienteId: patientId,
fecha,
hora,
ph,
pco2: pco2 || 40,
po2: po2 || 85,
hco3: hco3 || 24,
be: be || 0,
sato2: sato2 || 97,
lactato,
fio2,
interpretacion: ''
if (!ph) {
for (const linea of lineas) {
const cleanLinea = linea.replace(/show_chart|list_alt|\t+/g, ' ').replace(/,/g, '.').replace(/\s+/g, ' ').trim().toLowerCase();
const getValue = (param: string): number | undefined => {
const idx = cleanLinea.indexOf(param);
if (idx === -1) return undefined;
const after = cleanLinea.slice(idx + param.length).trim();
const parts = after.split(' ');
for (const p of parts) {
const v = parseFloat(p);
if (!isNaN(v) && v > 0 && v < 1000) return v;
}
return undefined;
};
if (cleanLinea.includes('ph') && !cleanLinea.includes('pco2') && !cleanLinea.includes('pco')) { ph = getValue('ph'); }
else if (cleanLinea.includes('pco2') || cleanLinea.includes('pco₂')) { pco2 = getValue('pco2'); }
else if (cleanLinea.includes('po2') || cleanLinea.includes('po₂')) { po2 = getValue('po2'); }
else if (cleanLinea.includes('hco3')) { hco3 = getValue('hco3'); }
else if (cleanLinea.includes('exceso') || cleanLinea.includes('base excess')) { be = getValue('exceso') || getValue('base'); }
else if (cleanLinea.includes('saturación') || cleanLinea.includes('sato2')) { sato2 = getValue('saturación') || getValue('sat'); }
else if (cleanLinea.includes('lactato')) { lactato = getValue('lactato'); }
else if (cleanLinea.includes('fio2')) { fio2 = getValue('fio2'); }
}
}
console.log('AB:', { ph, pco2, po2, hco3, be, sato2 });
if (ph) {
const hora = new Date().toTimeString().slice(0, 5);
return { pacienteId: patientId, fecha, hora, ph, pco2: pco2 || 40, po2: po2 || 85, hco3: hco3 || 24, be: be || 0, sato2: sato2 || 97, lactato, fio2, interpretacion: '' };
}
return null;
};