Removes duplicate chars — those sharing the same text, fontname, size, and positioning (within `tolerance`) as other characters in the set.
(chars: list, tolerance=1)
| 869 | |
| 870 | |
| 871 | def dedupe_chars(chars: list, tolerance=1) -> list: |
| 872 | """ |
| 873 | Removes duplicate chars — those sharing the same text, fontname, size, |
| 874 | and positioning (within `tolerance`) as other characters in the set. |
| 875 | """ |
| 876 | key = itemgetter("fontname", "size", "upright", "text") |
| 877 | pos_key = itemgetter("doctop", "x0") |
| 878 | |
| 879 | def yield_unique_chars(chars: list): |
| 880 | sorted_chars = sorted(chars, key=key) |
| 881 | for grp, grp_chars in itertools.groupby(sorted_chars, key=key): |
| 882 | for y_cluster in cluster_objects( |
| 883 | list(grp_chars), itemgetter("doctop"), tolerance |
| 884 | ): |
| 885 | for x_cluster in cluster_objects( |
| 886 | y_cluster, itemgetter("x0"), tolerance |
| 887 | ): |
| 888 | yield sorted(x_cluster, key=pos_key)[0] |
| 889 | |
| 890 | deduped = yield_unique_chars(chars) |
| 891 | return sorted(deduped, key=chars.index) |
| 892 | |
| 893 | |
| 894 | def line_to_edge(line): |
nothing calls this directly
no test coverage detected
searching dependent graphs…