(chars: list, **kwargs)
| 822 | |
| 823 | |
| 824 | def extract_text(chars: list, **kwargs) -> str: |
| 825 | chars = to_list(chars) |
| 826 | if len(chars) == 0: |
| 827 | return "" |
| 828 | |
| 829 | if kwargs.get("layout"): |
| 830 | return chars_to_textmap(chars, **kwargs).as_string |
| 831 | else: |
| 832 | y_tolerance = kwargs.get("y_tolerance", DEFAULT_Y_TOLERANCE) |
| 833 | extractor = WordExtractor( |
| 834 | **{k: kwargs[k] for k in WORD_EXTRACTOR_KWARGS if k in kwargs} |
| 835 | ) |
| 836 | words = extractor.extract_words(chars) |
| 837 | if words: |
| 838 | rotation = words[0]["rotation"] # rotation cannot change within a cell |
| 839 | else: |
| 840 | rotation = 0 |
| 841 | |
| 842 | if rotation == 90: |
| 843 | words.sort(key=lambda w: (w["x1"], -w["top"])) |
| 844 | lines = " ".join([w["text"] for w in words]) |
| 845 | elif rotation == 270: |
| 846 | words.sort(key=lambda w: (-w["x1"], w["top"])) |
| 847 | lines = " ".join([w["text"] for w in words]) |
| 848 | else: |
| 849 | lines = cluster_objects(words, itemgetter("doctop"), y_tolerance) |
| 850 | lines = "\n".join(" ".join(word["text"] for word in line) for line in lines) |
| 851 | if rotation == 180: # needs extra treatment |
| 852 | lines = "".join([(c if c != "\n" else " ") for c in reversed(lines)]) |
| 853 | |
| 854 | return lines |
| 855 | |
| 856 | |
| 857 | def collate_line( |
no test coverage detected
searching dependent graphs…