
    kh3                         d dl Z d dlZd dlmZmZmZmZmZ d dlZddl	m
Z
mZmZmZ dedee
ef   fdZdeeee   f   dee
ef   fd	Zdded
edefdZddej$                  d
edefdZy)    N)ListUnionDictr   Any   )BaseLayoutElement	TextBlockLayoutBASECOORD_ELEMENT_NAMEMAPfilenamereturnc                     t        | d      5 }t        j                  |      }ddd       t        |      S # 1 sw Y   t              S xY w)aT  Load a JSON file and save it as a layout object with appropriate data types.

    Args:
        filename (str):
            The name of the JSON file.

    Returns:
        Union[BaseLayoutElement, Layout]:
            Based on the JSON file format, it will automatically parse
            the type of the data and load it accordingly.
    rN)openjsonload	load_dict)r   fpress      Q/var/www/teggl/fontify/venv/lib/python3.12/site-packages/layoutparser/io/basic.py	load_jsonr      sE     
h	 iim S> S>s	   6A	datac                     t         t              rd v r&t        t         d         j                   d         S  d   t
        vrt        d d          t         fdt        j                  D              }|rt        j                         S t
         d      j                         S t         t              r"t         D cg c]  }t        |       c}      S t        d      c c}w )a  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:
        ValueError:
            If the data format is incompatible with the layout-data-JSON format,
            raise a `ValueError`.
        ValueError:
            If any `block_type` name is not in the available list of layout element
            names defined in `BASECOORD_ELEMENT_NAMEMAP`, raise a `ValueError`.

    Returns:
        Union[BaseLayoutElement, Layout]:
            Based on the dict format, it will automatically parse the type of
            the data and load it accordingly.
    	page_datablocks)r   
block_typezInvalid block_type c              3   &   K   | ]  }|v  
 y wN ).0eler   s     r   	<genexpr>zload_dict.<locals>.<genexpr>O   s     Jssd{Js   zInvalid input JSON structure.)
isinstancedictr
   r   _blocksr   
ValueErroranyr	   	_features	from_dictlist)r   is_textblockr!   s   `  r   r   r   /   s    , $$)DN3;;tKGXYY L!)BB #6tL7I6J!KLL Ji6I6IJJL **4000l1CDNNtTT	D$	6#y~677 8:: 7s   C)r   c                 B    t        t        j                  |       |      S )a  Load the Layout object from the given CSV file.

    Args:
        filename (str):
            The name of the CSV file. A row of the table represents
            an individual layout element.

        block_type (str):
            If there's no block_type column in the CSV file,
            you must pass in a block_type variable such that layout parser
            can appropriately detect the type of the layout elements.

    Returns:
        Layout:
            The parsed Layout object from the CSV file.
    )r   )load_dataframepdread_csv)r   r   s     r   load_csvr0   \   s    $ "++h/JGG    dfc                    | j                         } d| j                  v r.| d   j                  t        k(  r| d   j	                  d       | d<   |d| j                  vrt        d      || d<   t        d | j                  D              rd| j                  vr| j                  | d<   t        | j                  d d	      j                               S )
a  Load the Layout object from the given dataframe.

    Args:
        df (pd.DataFrame):

        block_type (str):
            If there's no block_type column in the CSV file,
            you must pass in a block_type variable such that layout parser
            can appropriately detect the type of the layout elements.

    Returns:
        Layout:
            The parsed Layout object from the CSV file.
    pointsc                 Z    t        j                  |       st        j                  |       S | S r   )r.   isnaastliteral_evalxs    r   <lambda>z load_dataframe.<locals>.<lambda>   s    RWWQZ#**1- Q r1   r   z:`block_type` not specified both in dataframe and argumentsc              3   @   K   | ]  }|t         j                  v   y wr   )r	   r(   )r    cols     r   r"   z!load_dataframe.<locals>.<genexpr>   s     
<#3)%%%
<s   idc                 >    | j                         j                         S r   )dropnato_dictr9   s    r   r;   z load_dataframe.<locals>.<lambda>   s    
(:(:(< r1      )axis)copycolumnsdtypeobjectmapr&   r'   indexr   applyto_list)r2   r   s     r   r-   r-   q   s     
B2::h<'h<++FBxL rzz)L  &<

<
<<rzz!xxBtHRXX<1XEMMOPPr1   r   )r7   r   typingr   r   r   r   pandasr.   elementsr   r	   r
   r   strr   r   r0   	DataFramer-   r   r1   r   <module>rQ      s      / /   &7&? @ $*;E$T
*+ *;6G6O0P *;ZHs H Hv H*#Qr|| #Q #Q #Qr1   