generate a javascript function that will recreate a table structure from a ocr result…
- User
generate a javascript function that will recreate a table structure from a ocr result that has an array of arrays, try to split the column and rows as good as possible. elements are like this: first element of the array is an array representing the text rectangle, top left, top right, bottom right and bottom left, it looks like this: [ [ [ [207.0, 0.0], [230.0, 0.0], [230.0, 35.0], [207.0, 35.0], ], [‘1’, 0.7348307371139526], ], [ [ [351.0, 0.0], [716.0, 0.0], [716.0, 35.0], [351.0, 35.0], ], [‘ENERGIE ELECTRICA’, 0.9918891787528992], ], [ [ [1377.0, 3.0], [1447.0, 3.0], [1447.0, 43.0], [1377.0, 43.0], ], [‘pers’, 0.9991182684898376], ], [ [ [1550.0, 0.0], [1640.0, 0.0], [1640.0, 40.0], [1550.0, 40.0], ], [‘1,000’, 0.9995460510253906], ], [ [ [1852.0, 3.0], [1971.0, 3.0], [1971.0, 40.0], [1852.0, 40.0], ], [‘10,4758’, 0.9997211694717407], ], [ [ [2214.0, 3.0], [2302.0, 3.0], [2302.0, 43.0], [2214.0, 43.0], ], [‘10,48’, 0.9998644590377808], ], [ [ [202.0, 37.0], [232.0, 37.0], [232.0, 75.0], [202.0, 75.0], ], [‘2’, 0.9995154142379761], ], [ [ [351.0, 40.0], [987.0, 40.0], [987.0, 75.0], [351.0, 75.0], ], [‘ENERGIE ELECTRICA CONSUM PARC’, 0.9777751564979553], ], [ [ [1373.0, 44.0], [1444.0, 36.0], [1448.0, 76.0], [1377.0, 84.0], ], [‘pers’, 0.9989522099494934], ], [ [ [1553.0, 43.0], [1640.0, 43.0], [1640.0, 80.0], [1553.0, 80.0], ], [‘1,000’, 0.9985054731369019], ], [ [ [1855.0, 43.0], [1963.0, 43.0], [1963.0, 80.0], [1855.0, 80.0], ], [‘3,4989’, 0.9985577464103699], ], [ [ [2216.0, 45.0], [2294.0, 45.0], [2294.0, 83.0], [2216.0, 83.0], ], [‘3,50’, 0.9993970394134521], ], [ [ [310.0, 75.0], [592.0, 80.0], [591.0, 118.0], [310.0, 112.0], ], [‘SERVICII PAZA’, 0.9661298394203186], ], [ [ [202.0, 120.0], [232.0, 120.0], [232.0, 158.0], [202.0, 158.0], ], [‘3’, 0.9990814924240112], ], [ [ [344.0, 117.0], [607.0, 120.0], [607.0, 158.0], [343.0, 155.0], ], [’ SERVICII PAZA’, 0.963112473487854], ], [ [ [1390.0, 128.0], [1431.0, 128.0], [1431.0, 163.0], [1390.0, 163.0], ], [‘ap’, 0.9969583749771118], ], [ [ [1550.0, 123.0], [1640.0, 123.0], [1640.0, 160.0], [1550.0, 160.0], ], [‘1,000’, 0.9995529055595398], ], [ [ [1847.0, 123.0], [1968.0, 123.0], [1968.0, 160.0], [1847.0, 160.0], ], [‘64,9621’, 0.9998608827590942], ], [ [ [2211.0, 123.0], [2302.0, 123.0], [2302.0, 163.0], [2211.0, 163.0], ], [‘64,96’, 0.9999416470527649], ], ]
- Assistant
function createTable(ocrResult) { let table = document.createElement("table"); let numRows = ocrResult.length; let numCols = ocrResult[0].length;
// create table rows and columns for (let i = 0; i < numRows; i++) { let tr = document.createElement("tr"); for (let j = 0; j < numCols; j++) { let td = document.createElement("td"); let text = ocrResult[i][j][1];
if (text) { td.innerHTML = text; } else { td.innerHTML = " "; } tr.appendChild(td); } table.appendChild(tr);}
return table; }
- User
i don't have rows and cols, there are just coordinates
- Assistant
In that case, we'll need to do a bit more work to determine the rows and columns. We can first create a 2D array to hold the cells based on their position. We'll loop through each cell in the ocrResult array and determine its position in the table based on its top and left coordinates. Here's an example implementation:
function createTable(ocrResult) { // determine table dimensions let numRows = 0; let numCols = 0; let positions = {}; for (let i = 0; i < ocrResult.length; i++) { let x = ocrResult[i][0][0][0]; let y = ocrResult[i][0][0][1]; if (!positions[x]) { positions[x] = {}; } positions[x][y] = true; numRows = Math.max(numRows, y); numCols = Math.max(numCols, x); } // create table and cells let table = document.createElement("table"); for (let y = 0; y < numRows; y++) { let tr = document.createElement("tr"); for (let x = 0; x < numCols; x++) { let td = document.createElement("td"); if (positions[x] && positions[x][y]) { let text = ocrResult.find((cell) => cell[0][0][0] === x && cell[0][0][1] === y )[1]; td.innerHTML = text ? text : " "; } else { td.innerHTML = " "; } tr.appendChild(td); } table.appendChild(tr); } return table; } - User
some elements can be the same cell
- Assistant
- User
i get invalid character cells.filter(© =>
- Assistant
It looks like there may be some invalid characters in the previous code I provided. Here is the updated code without any invalid characters:
function createTable(ocrResult) { let cells = []; // group overlapping cells for (let i = 0; i < ocrResult.length; i++) { let cell = ocrResult[i]; let x1 = cell[0][0][0]; let y1 = cell[0][0][1]; let x2 = cell[0][2][0]; let y2 = cell[0][2][1]; // check for overlapping cells let overlaps = cells.filter((c) => c.x1 <= x2 && c.x2 >= x1 && c.y1 <= y2 && c.y2 >= y1 ); if (overlaps.length > 0) { // if overlaps, merge with first overlapping cell overlaps[0].text += " " + cell[1]; overlaps[0].x1 = Math.min(overlaps[0].x1, x1); overlaps[0].y1 = Math.min(overlaps[0].y1, y1); overlaps[0].x2 = Math.max(overlaps[0].x2, x2); overlaps[0].y2 = Math.max(overlaps[0].y2, y2); } else { // if no overlaps, create new cell cells.push({ text: cell[1], x1: x1, y1: y1, x2: x2, y2: y2, }); } } // determine table dimensions let numRows = 0; let numCols = 0; for (let i = 0; i < cells.length; i++) { numRows = Math.max(numRows, cells[i].y2); numCols = Math.max(numCols, cells[i].x2); } // create table and cells let table = document.createElement("table"); for (let y = 0; y < numRows; y++) { let tr = document.createElement("tr"); for (let x = 0; x < numCols; x++) { let td = document.createElement("td"); let cell = cells.find( (c) => c.x1 <= x && c.x2 >= x && c.y1 <= y && c.y2 >= y ); if (cell) { td.innerHTML = cell.text; } else { td.innerHTML = " "; } tr.appendChild(td); } table.appendChild(tr); } return table; }