Spaces:
Sleeping
Sleeping
File size: 3,864 Bytes
df4a1a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | export function clamp(value, min, max) {
return Math.min(Math.max(value, min), max)
}
export function cleanCellText(value) {
return String(value ?? '').replace(/\s+/g, ' ').trim()
}
export function getPages(annotation) {
if (annotation?.pages?.length) {
return annotation.pages
}
return [{ page_index: 0, image_size: annotation?.image_size || [0, 0] }]
}
export function getTablesForPage(annotation, pageIndex) {
return (annotation?.tables || [])
.filter((table) => (table.page || 0) === pageIndex)
.sort((a, b) => a.table_id - b.table_id)
}
export function getTableById(annotation, tableId) {
return (annotation?.tables || []).find((table) => table.table_id === tableId) || null
}
export function getCellKey(tableId, row, col) {
return `${tableId}:${row}:${col}`
}
export function isSameCell(a, b) {
if (!a || !b) {
return false
}
return a.tableId === b.tableId && a.row === b.row && a.col === b.col
}
export function findCell(annotation, target) {
if (!target) {
return null
}
const table = getTableById(annotation, target.tableId)
if (!table) {
return null
}
const cell = (table.cells || []).find((item) => item.row === target.row && item.col === target.col)
if (!cell) {
return null
}
return { table, cell }
}
export function updateAnnotationCell(annotation, target, text) {
return {
...annotation,
tables: (annotation.tables || []).map((table) => {
if (table.table_id !== target.tableId) {
return table
}
return {
...table,
cells: (table.cells || []).map((cell) => (
cell.row === target.row && cell.col === target.col
? { ...cell, text }
: cell
)),
}
}),
}
}
export function confidenceStroke(score) {
if (score === null || score === undefined) {
return '#9CA3AF'
}
if (score >= 0.8) {
return '#16A34A'
}
if (score >= 0.5) {
return '#D97706'
}
return '#DC2626'
}
export function heatmapColor(score, alpha = 0.18) {
if (score === null || score === undefined) {
return `rgba(229, 231, 235, ${alpha})`
}
if (score >= 0.8) {
return `rgba(22, 163, 74, ${alpha})`
}
if (score >= 0.5) {
return `rgba(217, 119, 6, ${alpha})`
}
return `rgba(220, 38, 38, ${alpha})`
}
export function buildTableModel(table) {
const cells = table?.cells || []
const rowCount = cells.length ? Math.max(...cells.map((cell) => cell.row + cell.row_span)) : 0
const colCount = cells.length ? Math.max(...cells.map((cell) => cell.col + cell.col_span)) : 0
const anchors = new Map()
const covered = new Set()
const occupancy = new Map()
for (const cell of cells) {
const anchorKey = `${cell.row},${cell.col}`
anchors.set(anchorKey, cell)
for (let row = cell.row; row < cell.row + cell.row_span; row += 1) {
for (let col = cell.col; col < cell.col + cell.col_span; col += 1) {
const positionKey = `${row},${col}`
occupancy.set(positionKey, cell)
if (row !== cell.row || col !== cell.col) {
covered.add(positionKey)
}
}
}
}
return {
rowCount,
colCount,
anchors,
covered,
occupancy,
}
}
export function findAdjacentCell(model, row, col, direction) {
if (!model) {
return null
}
const deltas = {
up: [-1, 0],
down: [1, 0],
left: [0, -1],
right: [0, 1],
}
const delta = deltas[direction]
if (!delta) {
return null
}
let nextRow = row + delta[0]
let nextCol = col + delta[1]
while (nextRow >= 0 && nextCol >= 0 && nextRow < model.rowCount && nextCol < model.colCount) {
const candidate = model.occupancy.get(`${nextRow},${nextCol}`)
if (candidate && (candidate.row !== row || candidate.col !== col)) {
return candidate
}
nextRow += delta[0]
nextCol += delta[1]
}
return null
}
|