MCPcopy Create free account
hub / github.com/Layout-Parser/layout-parser / load_dict

Function load_dict

src/layoutparser/io/basic.py:47–89  ·  view source on GitHub ↗

Load a dict of list of dict representations of some layout data, automatically parse its type, and save it as any of BaseLayoutElement or Layout datatype. Args: data (Union[Dict, List]): A dict of list of dict representations of the layout data Raises: V

(data: Union[Dict, List[Dict]])

Source from the content-addressed store, hash-verified

45
46
47def load_dict(data: Union[Dict, List[Dict]]) -> Union[BaseLayoutElement, Layout]:
48 """Load a dict of list of dict representations of some layout data,
49 automatically parse its type, and save it as any of BaseLayoutElement
50 or Layout datatype.
51
52 Args:
53 data (Union[Dict, List]):
54 A dict of list of dict representations of the layout data
55
56 Raises:
57 ValueError:
58 If the data format is incompatible with the layout-data-JSON format,
59 raise a `ValueError`.
60 ValueError:
61 If any `block_type` name is not in the available list of layout element
62 names defined in `BASECOORD_ELEMENT_NAMEMAP`, raise a `ValueError`.
63
64 Returns:
65 Union[BaseLayoutElement, Layout]:
66 Based on the dict format, it will automatically parse the type of
67 the data and load it accordingly.
68 """
69 if isinstance(data, dict):
70 if "page_data" in data:
71 # It is a layout instance
72 return Layout(load_dict(data["blocks"])._blocks, page_data=data["page_data"])
73 else:
74
75 if data["block_type"] not in BASECOORD_ELEMENT_NAMEMAP:
76 raise ValueError(f"Invalid block_type {data['block_type']}")
77
78 # Check if it is a textblock
79 is_textblock = any(ele in data for ele in TextBlock._features)
80 if is_textblock:
81 return TextBlock.from_dict(data)
82 else:
83 return BASECOORD_ELEMENT_NAMEMAP[data["block_type"]].from_dict(data)
84
85 elif isinstance(data, list):
86 return Layout([load_dict(ele) for ele in data])
87
88 else:
89 raise ValueError(f"Invalid input JSON structure.")
90
91
92def load_csv(filename: str, block_type: str = None) -> Layout:

Callers 3

test_jsonFunction · 0.90
load_jsonFunction · 0.85
load_dataframeFunction · 0.85

Calls 2

LayoutClass · 0.85
from_dictMethod · 0.45

Tested by 1

test_jsonFunction · 0.72