table
autocorpus.table
¤
Tables-JSON top-level builder script.
Functions¤
__check_superrow(cells)
¤
Check if the current row is a superrow.
Superrows contain cells that are split and contain more values than other cells on the same row.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cells
|
list[str]
|
Cells in row |
required |
Source code in autocorpus/table.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
__get_headers(t, config)
¤
Identify headers from a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
BeautifulSoup
|
BeautifulSoup object of table |
required |
config
|
dict[str, Any]
|
Configuration dictionary |
required |
Returns:
Type | Description |
---|---|
list[int]
|
List of header indexes |
Raises:
Type | Description |
---|---|
KeyError
|
Missing element |
Source code in autocorpus/table.py
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 |
|
__is_mix(s)
¤
Check if input string is a mix of number and text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
input string |
required |
Returns:
Type | Description |
---|---|
bool
|
True/False |
Source code in autocorpus/table.py
161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
__is_number(s)
¤
Check if input string is a number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
input string |
required |
Returns:
Type | Description |
---|---|
bool
|
True/False |
Source code in autocorpus/table.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
__is_text(s)
¤
Check if input string is all text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
input string |
required |
Returns:
Type | Description |
---|---|
bool
|
True/False |
Source code in autocorpus/table.py
176 177 178 179 180 181 182 183 184 185 186 |
|
__table2json(table_2d, header_idx, subheader_idx, superrow_idx, table_num, title, footer, caption)
¤
Transform tables from nested lists to JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_2d
|
list
|
nested list tables |
required |
header_idx
|
list
|
list of header indices |
required |
subheader_idx
|
list
|
list of subheader indices |
required |
superrow_idx
|
list
|
list of superrow indices |
required |
table_num
|
int
|
table number |
required |
title
|
str
|
table title |
required |
footer
|
str
|
table footer |
required |
caption
|
str
|
table caption |
required |
Returns:
Name | Type | Description |
---|---|---|
tables |
list
|
tables in JSON format |
Source code in autocorpus/table.py
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 |
|
__table_to_2d(t)
¤
Transform tables from nested lists to JSON.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
BeautifulSoup
|
HTML table |
required |
Returns:
Type | Description |
---|---|
list[list[str]]
|
Table structure as a nested list |
Source code in autocorpus/table.py
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 |
|
get_table_json(soup, config, file_path)
¤
Extracts and processes tables from an HTML document.
This is done using BeautifulSoup and a configuration dictionary.
The function performs the following steps: 1. Extracts tables from the HTML document based on the provided configuration. 2. Removes empty tables and tables with specific classes (e.g., "table-group"). 3. Identifies and processes table headers, superrows, and subheaders. 4. Converts tables into a 2D format and processes cell data types (e.g., numeric, text, mixed). 5. Converts the processed table data into a JSON-compatible format. 6. Merges headers and formats the final table data for output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
soup
|
BeautifulSoup
|
A BeautifulSoup object representing the parsed HTML document. |
required |
config
|
dict[str, Any]
|
A dictionary containing configuration options for table processing. |
required |
file_path
|
Path
|
The file name or path of the HTML document being processed. |
required |
Returns:
Type | Description |
---|---|
tuple[dict[str, Any], list[dict[str, Any]]]
|
A dictionary containing the processed table data in JSON format and a list of dictionaries representing empty tables. |
Source code in autocorpus/table.py
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 |
|