|
| 1 | +from typing import Dict, List, Sequence |
| 2 | + |
| 3 | +from mindee.documents.custom.custom_v1_fields import ListField, ListFieldValue |
| 4 | +from mindee.geometry import ( |
| 5 | + Quadrilateral, |
| 6 | + get_bounding_box, |
| 7 | + get_min_max_y, |
| 8 | + is_point_in_y, |
| 9 | + merge_polygons, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def _array_product(array: Sequence[float]) -> float: |
| 14 | + """ |
| 15 | + Get the product of a sequence of floats. |
| 16 | +
|
| 17 | + :array: List of floats |
| 18 | + """ |
| 19 | + product = 1.0 |
| 20 | + for k in array: |
| 21 | + product = product * k |
| 22 | + return product |
| 23 | + |
| 24 | + |
| 25 | +def _find_best_anchor(anchors: Sequence[str], fields: Dict[str, ListField]) -> str: |
| 26 | + """ |
| 27 | + Find the anchor with the most rows, in the order specified by `anchors`. |
| 28 | +
|
| 29 | + Anchor will be the name of the field. |
| 30 | + """ |
| 31 | + anchor = "" |
| 32 | + anchor_rows = 0 |
| 33 | + for field in anchors: |
| 34 | + values = fields[field].values |
| 35 | + if len(values) > anchor_rows: |
| 36 | + anchor_rows = len(values) |
| 37 | + anchor = field |
| 38 | + return anchor |
| 39 | + |
| 40 | + |
| 41 | +def _get_empty_field() -> ListFieldValue: |
| 42 | + """Return sample field with empty values.""" |
| 43 | + return ListFieldValue({"content": "", "polygon": [], "confidence": 0.0}) |
| 44 | + |
| 45 | + |
| 46 | +class Line: |
| 47 | + """Represent a single line.""" |
| 48 | + |
| 49 | + row_number: int |
| 50 | + fields: Dict[str, ListFieldValue] |
| 51 | + bounding_box: Quadrilateral |
| 52 | + |
| 53 | + |
| 54 | +def get_line_items( |
| 55 | + anchors: Sequence[str], columns: Sequence[str], fields: Dict[str, ListField] |
| 56 | +) -> List[Line]: |
| 57 | + """ |
| 58 | + Reconstruct line items from fields. |
| 59 | +
|
| 60 | + :anchors: Possible fields to use as an anchor |
| 61 | + :columns: All fields which are columns |
| 62 | + :fields: List of field names to reconstruct table with |
| 63 | + """ |
| 64 | + line_items: List[Line] = [] |
| 65 | + anchor = _find_best_anchor(anchors, fields) |
| 66 | + if not anchor: |
| 67 | + print(Warning("Could not find an anchor!")) |
| 68 | + return line_items |
| 69 | + |
| 70 | + # Loop on anchor items and create an item for each anchor item. |
| 71 | + # This will create all rows with just the anchor column value. |
| 72 | + for item in fields[anchor].values: |
| 73 | + line_item = Line() |
| 74 | + line_item.fields = {f: _get_empty_field() for f in columns} |
| 75 | + line_item.fields[anchor] = item |
| 76 | + line_items.append(line_item) |
| 77 | + |
| 78 | + # Loop on all created rows |
| 79 | + for idx, line in enumerate(line_items): |
| 80 | + # Compute sliding window between anchor item and the next |
| 81 | + min_y, _ = get_min_max_y(line.fields[anchor].polygon) |
| 82 | + if idx != len(line_items) - 1: |
| 83 | + max_y, _ = get_min_max_y(line_items[idx + 1].fields[anchor].polygon) |
| 84 | + else: |
| 85 | + max_y = 1.0 # bottom of page |
| 86 | + # Get candidates of each field included in sliding window and add it in line item |
| 87 | + for field in columns: |
| 88 | + field_words = [ |
| 89 | + word |
| 90 | + for word in fields[field].values |
| 91 | + if is_point_in_y(word.polygon.centroid, min_y, max_y) |
| 92 | + ] |
| 93 | + line.fields[field].content = " ".join([v.content for v in field_words]) |
| 94 | + try: |
| 95 | + line.fields[field].polygon = merge_polygons( |
| 96 | + [v.polygon for v in field_words] |
| 97 | + ) |
| 98 | + except ValueError: |
| 99 | + pass |
| 100 | + line.fields[field].confidence = _array_product( |
| 101 | + [v.confidence for v in field_words] |
| 102 | + ) |
| 103 | + all_polygons = [line.fields[anchor].polygon] |
| 104 | + for field in columns: |
| 105 | + try: |
| 106 | + all_polygons.append(line.fields[field].polygon) |
| 107 | + except IndexError: |
| 108 | + pass |
| 109 | + line.bounding_box = get_bounding_box(merge_polygons(all_polygons)) |
| 110 | + line.row_number = idx |
| 111 | + return line_items |
0 commit comments