From 2638a6883163d521dea6bc3de92c3bd8b3799356 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Thu, 16 Apr 2026 11:45:57 -0300 Subject: [PATCH] Fix parsers: laboratorio y estado acido-base --- src/sections/HistoriaClinica.tsx | 95 +++++++++++++++++--------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/src/sections/HistoriaClinica.tsx b/src/sections/HistoriaClinica.tsx index 6ecd764..236f91c 100644 --- a/src/sections/HistoriaClinica.tsx +++ b/src/sections/HistoriaClinica.tsx @@ -722,21 +722,22 @@ return res; const lineas = texto.split('\n'); - for (const linea of lineas) { +for (const linea of lineas) { const lineaLower = linea.toLowerCase().trim(); 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; };