File size: 23,173 Bytes
b837da3 6c2a241 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 6c2a241 b837da3 6c2a241 b837da3 6c2a241 b837da3 6c2a241 b837da3 1e9a6e5 b837da3 6c2a241 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 b837da3 1e9a6e5 | 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | """
Most of the code in this file is derived from the paper "Image-based table recognition: data, model, and evaluation".
The original paper can be accessed at: https://arxiv.org/pdf/1911.10683.
The code is available at: https://github.com/ibm-aur-nlp/PubTabNet.
A slight modification has been added to the code to improve the evaluation process.
"""
import re
import distance
import numpy as np
from concurrent.futures import ThreadPoolExecutor
try:
from scipy.optimize import linear_sum_assignment
except Exception as _e:
linear_sum_assignment = None
from lxml import etree, html
from collections import deque
from apted.helpers import Tree
from apted import APTED, Config
class TableTree(Tree):
"""Table Tree class for APTED"""
def __init__(self, tag, colspan=None, rowspan=None, content=None, *children):
self.tag = tag
self.colspan = colspan
self.rowspan = rowspan
self.content = content
self.children = list(children)
def bracket(self):
"""Show tree using brackets notation"""
if self.tag == 'td':
result = '"tag": %s, "colspan": %d, "rowspan": %d, "text": %s' % \
(self.tag, self.colspan, self.rowspan, self.content)
else:
result = '"tag": %s' % self.tag
for child in self.children:
result += child.bracket()
return "{{{}}}".format(result)
def visualize(self, indent=0, prefix="", is_last=True):
"""Visualize tree structure in ASCII art format
Args:
indent (int): Current indentation level
prefix (str): Prefix for tree branches
is_last (bool): Whether this is the last child of its parent
Returns:
str: ASCII tree visualization
"""
# Prepare node information
if self.tag == 'td':
content_preview = ''
if self.content:
content_str = ''.join(self.content) if isinstance(self.content, list) else str(self.content)
content_preview = content_str[:30] + '...' if len(content_str) > 30 else content_str
content_preview = f' "{content_preview}"' if content_preview else ''
node_info = f"{self.tag}"
attrs = []
if self.colspan and self.colspan > 1:
attrs.append(f"colspan={self.colspan}")
if self.rowspan and self.rowspan > 1:
attrs.append(f"rowspan={self.rowspan}")
if attrs:
node_info += f" [{', '.join(attrs)}]"
if content_preview:
node_info += content_preview
else:
node_info = self.tag
# Build the tree line
if indent == 0:
result = f"{node_info}\n"
else:
connector = "└── " if is_last else "├── "
result = f"{prefix}{connector}{node_info}\n"
# Process children
for i, child in enumerate(self.children):
is_last_child = (i == len(self.children) - 1)
if indent == 0:
child_prefix = ""
else:
child_prefix = prefix + (" " if is_last else "│ ")
result += child.visualize(indent + 1, child_prefix, is_last_child)
return result
class CustomConfig(Config):
"""Custom Configuration for APTED"""
@staticmethod
def maximum(*sequences):
"""Get maximum possible value"""
return max(map(len, sequences))
def normalized_distance(self, *sequences):
"""Get distance from 0 to 1"""
return float(distance.levenshtein(*sequences)) / self.maximum(*sequences)
def rename(self, node1, node2):
"""Compares attributes of trees"""
if (node1.tag != node2.tag) or \
(node1.colspan != node2.colspan) or \
(node1.rowspan != node2.rowspan):
return 1.
if node1.tag == 'td':
if node1.content or node2.content:
return self.normalized_distance(
node1.content, node2.content
)
return 0.
class TEDSEvaluator(object):
"""Tree Edit Distance basead Similarity"""
def __init__(
self,
structure_only=False,
n_jobs=1,
ignore_nodes=None,
):
assert isinstance(n_jobs, int) and (n_jobs >= 1), (
'n_jobs must be an integer greather than 1'
)
self.structure_only = structure_only
self.n_jobs = n_jobs
self.ignore_nodes = ignore_nodes
self.__tokens__ = []
def tokenize(self, node):
"""Tokenizes table cells"""
self.__tokens__.append('<%s>' % node.tag)
if node.text is not None:
self.__tokens__ += list(node.text)
for n in node.getchildren():
self.tokenize(n)
if node.tag != 'unk':
self.__tokens__.append('</%s>' % node.tag)
if node.tag != 'td' and node.tail is not None:
self.__tokens__ += list(node.tail)
def load_html_tree(self, node, parent=None):
"""Converts HTML tree to the format required by apted
This version treats nested tables as separate tree nodes rather than content.
"""
global __tokens__
if node.tag == 'td':
# Check if td contains nested table(s)
nested_tables = [n for n in node.getchildren() if n.tag == 'table']
if nested_tables:
# td has nested table(s) - create td node and add tables as children
if self.structure_only:
cell = []
else:
self.__tokens__ = []
if node.text is not None:
self.__tokens__ += list(node.text)
for n in node.getchildren():
if n.tag != 'table':
self.tokenize(n)
if n.tail is not None:
self.__tokens__ += list(n.tail)
cell = self.__tokens__.copy() if self.__tokens__ else []
new_node = TableTree(
node.tag,
int(node.attrib.get('colspan', '1')),
int(node.attrib.get('rowspan', '1')),
cell, *deque()
)
# Add nested tables as children
if parent is not None:
parent.children.append(new_node)
for table in nested_tables:
self.load_html_tree(table, new_node)
else:
if self.structure_only:
cell = []
else:
self.__tokens__ = []
self.tokenize(node)
cell = self.__tokens__[1:-1].copy()
new_node = TableTree(
node.tag,
int(node.attrib.get('colspan', '1')),
int(node.attrib.get('rowspan', '1')),
cell, *deque()
)
if parent is not None:
parent.children.append(new_node)
else:
new_node = TableTree(node.tag, None, None, None, *deque())
if parent is not None:
parent.children.append(new_node)
for n in node.getchildren():
self.load_html_tree(n, new_node)
if parent is None:
return new_node
def evaluate(self, pred, true):
"""Computes TEDS score between the prediction and the ground truth of a given sample"""
if (not pred) or (not true):
return 0.0
parser = html.HTMLParser(remove_comments=True, encoding='utf-8')
pred = html.fromstring(pred, parser=parser)
true = html.fromstring(true, parser=parser)
if pred.xpath('body/table') and true.xpath('body/table'):
pred_tables = pred.xpath('body/table')
true_tables = true.xpath('body/table')
# Default behavior: if multiple tables present, compare concatenated wrappers by matching counts
# Here keep legacy single-table by choosing the first only when both are singletons
if len(pred_tables) == 1 and len(true_tables) == 1:
pred = pred_tables[0]
true = true_tables[0]
else:
# Fallback: wrap the entire body as a single root for structural comparison
pred = pred.xpath('body')[0]
true = true.xpath('body')[0]
if self.ignore_nodes:
etree.strip_tags(pred, *self.ignore_nodes)
etree.strip_tags(true, *self.ignore_nodes)
n_nodes_pred = len(pred.xpath('.//*'))
n_nodes_true = len(true.xpath('.//*'))
n_nodes = max(n_nodes_pred, n_nodes_true)
tree_pred = self.load_html_tree(pred)
tree_true = self.load_html_tree(true)
distance = APTED(tree_pred, tree_true, CustomConfig()).compute_edit_distance()
return 1.0 - (float(distance) / n_nodes)
else:
return 0.0
def get_table_contents(text):
# Regular expression to capture content within <table ...> and </table> tags
table_contents = re.findall(r'<table[^>]*?>(.*?)</table>', text, flags=re.DOTALL)
if len(table_contents) == 0:
table_contents = [text]
return table_contents
def extract_tables(data : dict) -> str:
"""Extract tables from the dictionary data.
Args:
data (dict): The data to extract tables from.
Returns:
str: The extracted tables from the data and a boolean indicating if the data has a table.
"""
# return as is if data is a string
html = '<html><body>'
for elem in data['elements']:
if elem['category'].lower() == 'table':
table_html_elements = get_table_contents(elem['content']['html'])
for table_html in table_html_elements:
html += f'<table>{table_html}</table>'
html += '</body></html>'
return html
def extract_table_list(data: dict):
"""Return a list of individual <table>...</table> HTML snippets from a doc dict.
Each returned entry is a complete single-table HTML fragment (wrapped with <table> tags).
"""
tables: list = []
elements = (data or {}).get('elements', []) or []
for elem in elements:
try:
category = (elem or {}).get('category', '')
if isinstance(category, str) and category.lower() == 'table':
html_contents = ((elem or {}).get('content', {}) or {}).get('html') or ''
if isinstance(html_contents, str):
tables.append(html_contents)
elif isinstance(html_contents, list):
for html_content in html_contents:
if isinstance(html_content, str):
tables.append(html_content)
except Exception:
continue
return tables
def _simplify_single_table(table_elem):
"""
Simplify a single table element. (Recursive handling of nested tables)
Args:
table_elem: lxml element - MUST be a <table> element
Returns:
lxml element: The simplified table element
"""
# 1. Remove all attributes of the table element
table_elem.attrib.clear()
# 2. Remove thead, tbody, tfoot wrappers (keep their children)
# Only process wrappers that belong to this table, not nested tables
for wrapper_tag in ('thead', 'tbody', 'tfoot'):
# Find all wrappers but only process those directly under this table
for wrapper in list(table_elem.xpath(f'./{wrapper_tag}')):
parent = wrapper.getparent()
index = list(parent).index(wrapper)
for child in list(wrapper):
parent.insert(index, child)
index += 1
parent.remove(wrapper)
# 3. Get direct tr children, excluding those in nested tables
direct_rows = []
for tr in table_elem.xpath('.//tr'):
# Find the closest table ancestor
parent = tr.getparent()
while parent is not None and parent.tag != 'table':
parent = parent.getparent()
# Only include if the closest table ancestor is our table_elem
if parent is table_elem:
direct_rows.append(tr)
# 4. Remove attributes of the tr and cell except for colspan and rowspan
for tr in direct_rows:
tr.attrib.clear()
for cell in tr:
if cell.tag in ('th', 'td'): # Replace th with td
if cell.tag == 'th':
cell.tag = 'td'
# Keep only colspan and rowspan attributes
new_attrib = {}
if 'colspan' in cell.attrib:
new_attrib['colspan'] = cell.attrib['colspan']
if 'rowspan' in cell.attrib:
new_attrib['rowspan'] = cell.attrib['rowspan']
cell.attrib.clear()
cell.attrib.update(new_attrib)
# Check if there is a nested table
nested_tables = cell.xpath('.//table')
# Recursively handle nested tables
for nested in nested_tables:
_simplify_single_table(nested)
# Remove unnecessary tags (keep content, remove tag wrapper)
# These tags are stripped but their text content is preserved
unnecessary_tags = [
'div', 'span', 'p', 'br', 'b', 'i', 'strong', 'em', 'u',
'font', 'a', 'sup', 'sub', 'small', 'big', 'center',
'label', 'section', 'article', 'header', 'footer', 'nav'
]
etree.strip_tags(cell, *unnecessary_tags)
# Get text content (include text of all sub-tags)
text_content = cell.text_content()
if text_content:
text_content = text_content.strip().replace('\xa0', '').replace(' ', '').strip()
# If completely empty, set it as an empty cell (no text and no nested table)
if (not text_content or text_content == '') and not nested_tables:
for child in list(cell):
cell.remove(child)
cell.text = ''
cell.tail = None
return table_elem
def preprocess_table(table_html_list):
"""
Preprocess the HTML table list to the basic structure.
Recursively handle nested tables (TINT).
Args:
table_html_list (list): List of HTML table strings
Returns:
list: Simplified list of HTML table strings
"""
preprocessed_tables = []
for html_string in table_html_list:
try:
parser = html.HTMLParser(remove_comments=True, encoding='utf-8')
# Extract outermost <table>...</table> if exists, otherwise wrap with <table>
table_start = html_string.find('<table')
table_end = html_string.rfind('</table>')
if table_start != -1 and table_end != -1:
# Extract the outermost table
html_string = html_string[table_start:table_end + len('</table>')]
else:
# No table tag found, wrap content with <table>
html_string = f'<table>{html_string}</table>'
root = html.fromstring(html_string, parser=parser)
# root itself might be the table element
if root.tag == 'table':
table = root
else:
table = root.xpath('.//table')[0]
# Simplify the table
table = _simplify_single_table(table)
table_string = etree.tostring(table, encoding='unicode', method='html')
table_string = '<html><body>' + re.sub(r'>\s+<', '><', table_string).strip() + '</body></html>'
preprocessed_tables.append(table_string)
except Exception as e:
print(f"[WARNING] Failed to simplify table: {e}, {html_string}")
preprocessed_tables.append(html_string)
return preprocessed_tables
def _compute_single_pair_score(args):
"""Helper function to compute score for a single (i, j) pair."""
i, j, gt_table, pred_table, evaluator = args
try:
s = float(evaluator.evaluate(pred_table, gt_table))
except Exception:
s = 0.0
return i, j, s
def _compute_teds_s_score(args):
"""Helper function to compute TEDS-S score for a matched pair."""
gt_table, pred_table, evaluator = args
try:
return float(evaluator.evaluate(pred_table, gt_table))
except Exception:
return 0.0
def _hungarian_match_tables_by_score(
gt_tables: list,
pred_tables: list,
evaluator: TEDSEvaluator,
min_match_score: float = 0.1,
max_workers: int = 1,
):
"""Hungarian one-to-one matching of GT and Pred tables using the provided evaluator.
Returns list of tuples: (gt_idx, pred_idx, score)
"""
matches: list = []
if not gt_tables or not pred_tables:
return matches
if linear_sum_assignment is None:
# Fallback: no scipy available, return empty
return matches
n = max(len(gt_tables), len(pred_tables))
cost = np.zeros((n, n), dtype=float)
score_mat = np.zeros((n, n), dtype=float)
# Initialize all costs to 1.0 (dummy pairs)
cost.fill(1.0)
# Build list of valid (i, j) pairs to compute
tasks = [
(i, j, gt_tables[i], pred_tables[j], evaluator)
for i in range(len(gt_tables))
for j in range(len(pred_tables))
]
# Use ThreadPoolExecutor for parallel score computation within this process
if tasks:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
results = list(executor.map(_compute_single_pair_score, tasks))
for i, j, s in results:
score_mat[i, j] = s
cost[i, j] = 1.0 - s
row_ind, col_ind = linear_sum_assignment(cost)
for i, j in zip(row_ind, col_ind):
if i < len(gt_tables) and j < len(pred_tables):
s = float(score_mat[i, j])
if s >= min_match_score:
matches.append((i, j, s))
return matches
def has_table_content(html_data : str) -> bool:
"""Check if the table has content between <html><body> and </body></html>.
Args:
html_data (str): The html data to check.
Returns:
bool: True if the table has content, False otherwise
"""
has_content = True
if html_data.replace('<html><body>', '').replace('</body></html>', '') == '':
has_content = False
return has_content
def prepare_table_dataset(gt_data, pred_data):
"""Prepare the tables for evaluation.
Args:
gt_data (dict): The ground truth dataset to evaluate.
pred_data (dict): The predicted dataset to evaluate.
Returns:
tuple (list, list): The list of ground truth and predicted tables.
"""
gt_table_list = []
pred_table_list = []
for image_key in gt_data.keys():
gt_elem = gt_data.get(image_key)
pred_elem = pred_data.get(image_key)
gt_tables = extract_tables(gt_elem)
pred_tables = extract_tables(pred_elem)
if not has_table_content(gt_tables):
continue
gt_table_list.append(gt_tables)
pred_table_list.append(pred_tables)
return gt_table_list, pred_table_list
def evaluate_table(
gt : dict,
pred : dict,
min_match_score: float = 0.0,
max_workers: int = 1,
) -> tuple:
"""Evaluate the table of the gt against the pred.
Args:
gt (dict): The gt layout to evaluate.
pred (dict): The pred layout to evaluate against.
Returns:
tuple(float, float, float, dict): The Table F1, TEDS, TEDS-S scores and per-image results.
"""
avg_teds_score = 0.0
avg_teds_s_score = 0.0
eval_s = TEDSEvaluator(structure_only=True)
eval_full = TEDSEvaluator(structure_only=False)
n_gt_tables = 0
n_pred_tables = 0
n_matched_tables = 0
teds_scores = []
teds_s_scores = []
per_image_scores = {}
for image_key in gt.keys():
gt_elem = gt.get(image_key)
pred_elem = pred.get(image_key)
gt_tables = extract_table_list(gt_elem)
pred_tables = extract_table_list(pred_elem)
n_gt_tables += len(gt_tables)
n_pred_tables += len(pred_tables)
# Initialize per-image result
per_image_scores[image_key] = {
"n_gt_tables": int(len(gt_tables)),
"n_pred_tables": int(len(pred_tables)),
"n_matched_tables": 0,
"matched_tables": []
}
if not gt_tables and not pred_tables:
continue
# Simplify tables before comparison
gt_tables = preprocess_table(gt_tables)
pred_tables = preprocess_table(pred_tables)
# TEDS (structure+content) for matching via Hungarian
matches = _hungarian_match_tables_by_score(
gt_tables, pred_tables, eval_full,
min_match_score=min_match_score,
max_workers=max_workers,
)
if matches:
n_matched_tables += len(matches)
per_image_scores[image_key]["n_matched_tables"] = int(len(matches))
# Extract TEDS scores from matches
teds_scores.extend([s for _, _, s in matches])
# Parallel computation of TEDS-S scores for matched pairs
teds_s_tasks = [(gt_tables[i], pred_tables[j], eval_s) for i, j, _ in matches]
with ThreadPoolExecutor(max_workers=max_workers) as executor:
teds_s_results = list(executor.map(_compute_teds_s_score, teds_s_tasks))
# Store results and per-image details
for (i, j, teds_score), teds_s_score in zip(matches, teds_s_results):
teds_s_scores.append(teds_s_score)
per_image_scores[image_key]["matched_tables"].append({
"gt_table_idx": int(i),
"pred_table_idx": int(j),
"teds_score": float(teds_score),
"teds_s_score": float(teds_s_score)
})
if len(teds_scores) > 0:
table_f1_score = 2 * n_matched_tables / (n_gt_tables + n_pred_tables)
avg_teds_score = sum(teds_scores) / len(teds_scores)
avg_teds_s_score = sum(teds_s_scores) / len(teds_s_scores)
else:
print('[Warning] No matched tables found in the ground truth and prediction datasets.')
table_f1_score = 0.0
avg_teds_score = 0.0
avg_teds_s_score = 0.0
return table_f1_score, avg_teds_score, avg_teds_s_score, per_image_scores
|