| 585 | |
| 586 | |
| 587 | class WordExtractor: |
| 588 | def __init__( |
| 589 | self, |
| 590 | x_tolerance=DEFAULT_X_TOLERANCE, |
| 591 | y_tolerance=DEFAULT_Y_TOLERANCE, |
| 592 | keep_blank_chars: bool = False, |
| 593 | use_text_flow=False, |
| 594 | horizontal_ltr=True, # Should words be read left-to-right? |
| 595 | vertical_ttb=False, # Should vertical words be read top-to-bottom? |
| 596 | extra_attrs=None, |
| 597 | split_at_punctuation=False, |
| 598 | expand_ligatures=True, |
| 599 | ): |
| 600 | self.x_tolerance = x_tolerance |
| 601 | self.y_tolerance = y_tolerance |
| 602 | self.keep_blank_chars = keep_blank_chars |
| 603 | self.use_text_flow = use_text_flow |
| 604 | self.horizontal_ltr = horizontal_ltr |
| 605 | self.vertical_ttb = vertical_ttb |
| 606 | self.extra_attrs = [] if extra_attrs is None else extra_attrs |
| 607 | |
| 608 | # Note: string.punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' |
| 609 | self.split_at_punctuation = ( |
| 610 | string.punctuation |
| 611 | if split_at_punctuation is True |
| 612 | else (split_at_punctuation or "") |
| 613 | ) |
| 614 | |
| 615 | self.expansions = LIGATURES if expand_ligatures else {} |
| 616 | |
| 617 | def merge_chars(self, ordered_chars: list): |
| 618 | x0, top, x1, bottom = objects_to_bbox(ordered_chars) |
| 619 | doctop_adj = ordered_chars[0]["doctop"] - ordered_chars[0]["top"] |
| 620 | upright = ordered_chars[0]["upright"] |
| 621 | direction = 1 if (self.horizontal_ltr if upright else self.vertical_ttb) else -1 |
| 622 | |
| 623 | matrix = ordered_chars[0]["matrix"] |
| 624 | |
| 625 | rotation = 0 |
| 626 | if not upright and matrix[1] < 0: |
| 627 | ordered_chars = reversed(ordered_chars) |
| 628 | rotation = 270 |
| 629 | |
| 630 | if matrix[0] < 0 and matrix[3] < 0: |
| 631 | rotation = 180 |
| 632 | elif matrix[1] > 0: |
| 633 | rotation = 90 |
| 634 | |
| 635 | word = { |
| 636 | "text": "".join( |
| 637 | self.expansions.get(c["text"], c["text"]) for c in ordered_chars |
| 638 | ), |
| 639 | "x0": x0, |
| 640 | "x1": x1, |
| 641 | "top": top, |
| 642 | "doctop": top + doctop_adj, |
| 643 | "bottom": bottom, |
| 644 | "upright": upright, |
no outgoing calls
no test coverage detected
searching dependent graphs…