Fix parsers: laboratorio y estado acido-base

This commit is contained in:
2026-04-16 11:45:57 -03:00
parent 8070709332
commit 2638a68831
+50 -43
View File
@@ -722,21 +722,22 @@ return res;
const lineas = texto.split('\n'); const lineas = texto.split('\n');
for (const linea of lineas) { for (const linea of lineas) {
const lineaLower = linea.toLowerCase().trim(); const lineaLower = linea.toLowerCase().trim();
for (const [clave, info] of Object.entries(mapeoParametros)) { for (const [clave, info] of Object.entries(mapeoParametros)) {
if (lineaLower.includes(clave)) { if (lineaLower.includes(clave)) {
const cleanLinea = linea.replace(/show_chart|list_alt/g, '').trim(); const cleanLinea = linea.replace(/show_chart|list_alt/g, '').replace(/\t+/g, ' ').replace(/\s+/g, ' ').trim();
const partes = cleanLinea.split(/\s+/); const partes = cleanLinea.split(' ');
for (let i = 0; i < partes.length; i++) { for (let i = 0; i < partes.length; i++) {
const parte = partes[i].replace(',', '.'); const parte = partes[i].replace(',', '.');
const valor = parseFloat(parte); const valor = parseFloat(parte);
if (!isNaN(valor) && valor > 0 && valor < 1000) { 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; const nombreNormalizado = info.nombre;
let valorFinal = valor; let valorFinal = valor;
@@ -780,59 +781,65 @@ if (!esUnidadSiguiente) {
let fecha = importFecha; let fecha = importFecha;
for (const linea of lineas) { 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().toLowerCase();
const cleanLinea = linea.replace(/show_chart|list_alt|\t+/g, ' ').replace(/,/g, '.').replace(/\s+/g, ' ').trim();
const getValue = (param: string): number | undefined => { const getValue = (param: string): number | undefined => {
const idx = cleanLinea.indexOf(param); const idx = cleanLinea.indexOf(param);
if (idx === -1) return undefined; if (idx === -1) return undefined;
const after = cleanLinea.slice(idx + param.length).trim(); const after = cleanLinea.slice(idx + param.length).trim();
const match = after.match(/(\d+\.?\d*)/); const parts = after.split(' ');
return match ? parseFloat(match[1]) : undefined; 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')) { if (cleanLinea.includes('estado') && cleanLinea.includes('ácido') || cleanLinea.includes('base')) {
ph = getValue('ph'); ph = getValue('ph') || (cleanLinea.match(/ph\s+(\d+\.?\d*)/)?.[1] ? parseFloat(cleanLinea.match(/ph\s+(\d+\.?\d*)/)![1]) : undefined);
} else if (cleanLinea.includes('pco2') || cleanLinea.includes('pco₂')) { pco2 = getValue('pco2') || getValue('pco₂');
pco2 = getValue('pco2') || getValue('pco'); po2 = getValue('po2') || getValue('po');
} else if (cleanLinea.includes('po2') || cleanLinea.includes('po₂')) {
po2 = getValue('po2') || getValue('po');
} else if (cleanLinea.includes('hco3')) {
hco3 = getValue('hco3'); 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') || getValue('exceso');
be = getValue('exceso de base') || getValue('base excess'); sato2 = getValue('saturación') || getValue('sato2') || getValue('sat');
} 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')) {
lactato = getValue('lactato'); lactato = getValue('lactato');
} else if (cleanLinea.includes('fio2') || cleanLinea.includes('fi o₂')) { fio2 = getValue('fio2');
fio2 = getValue('fio2') || getValue('fi o2'); break;
} 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];
} }
} }
console.log('AB parsing:', { ph, pco2, po2, hco3, be, sato2 }); if (!ph) {
for (const linea of lineas) {
if (ph !== undefined) { const cleanLinea = linea.replace(/show_chart|list_alt|\t+/g, ' ').replace(/,/g, '.').replace(/\s+/g, ' ').trim().toLowerCase();
const hora = new Date().toTimeString().slice(0, 5); const getValue = (param: string): number | undefined => {
return { const idx = cleanLinea.indexOf(param);
pacienteId: patientId, if (idx === -1) return undefined;
fecha, const after = cleanLinea.slice(idx + param.length).trim();
hora, const parts = after.split(' ');
ph, for (const p of parts) {
pco2: pco2 || 40, const v = parseFloat(p);
po2: po2 || 85, if (!isNaN(v) && v > 0 && v < 1000) return v;
hco3: hco3 || 24, }
be: be || 0, return undefined;
sato2: sato2 || 97,
lactato,
fio2,
interpretacion: ''
}; };
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; return null;
}; };