
    Ng3                         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dS )    N)ListUnionDictr   Any   )BaseLayoutElement	TextBlockLayoutBASECOORD_ELEMENT_NAMEMAPfilenamereturnc                     t          | d          5 }t          j        |          }ddd           n# 1 swxY w Y   t          |          S )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/html/ai-engine/env/lib/python3.11/site-packages/layoutparser/io/basic.py	load_jsonr      s     
h		 imm               S>>s   266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                  
                               S t           t                    rt          d  D                       S t          d          )	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 V  	d S N ).0eler   s     r   	<genexpr>zload_dict.<locals>.<genexpr>O   s'      JJssd{JJJJJJ    c                 ,    g | ]}t          |          S r   )r   )r    r!   s     r   
<listcomp>zload_dict.<locals>.<listcomp>V   s    666#y~~666r#   zInvalid input JSON structure.)
isinstancedictr
   r   _blocksr   
ValueErroranyr	   	_features	from_dictlist)r   is_textblocks   ` r   r   r   /   s   , $ ;$)DN33;tKGXYYYY L!)BBB !KtL7I!K!KLLL JJJJi6IJJJJJL U *40000l1CDNNtTTT	D$		 ;66666777 9:::r#   r   c                 H    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_csvr3   \   s!    $ "+h//JGGGGr#   dfc                    |                                  } d| j        v r5| d         j        t          k    r| d                             d           | d<   |d| j        vrt          d          n|| d<   t          d | j        D                       rd| j        vr
| j        | d<   t          | 	                    d d	
          
                                          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                 V    t          j        |           st          j        |           n| S r   )r1   isnaastliteral_evalxs    r   <lambda>z load_dataframe.<locals>.<lambda>   s#    RWQZZF#*1---Q r#   Nr   z:`block_type` not specified both in dataframe and argumentsc              3   2   K   | ]}|t           j        v V  d S r   )r	   r+   )r    cols     r   r"   z!load_dataframe.<locals>.<genexpr>   s*      
<
<#3)%%
<
<
<
<
<
<r#   idc                 N    |                                                                  S r   )dropnato_dictr;   s    r   r=   z load_dataframe.<locals>.<lambda>   s    

(:(:(<(< r#      )axis)copycolumnsdtypeobjectmapr)   r*   indexr   applyto_list)r4   r   s     r   r0   r0   q   s     
B2:h<''h<++FF BxL rz))L   *
 &<

<
<
<
<
<<<  rz!!xBtHRXX<<1XEEMMOOPPPr#   r   )r9   r   typingr   r   r   r   pandasr1   elementsr   r	   r
   r   strr   r   r3   	DataFramer0   r   r#   r   <module>rS      sn   


  / / / / / / / / / / / / / /                &7&? @    $*;E$T
*+ *;6G6O0P *; *; *; *;ZH Hs H Hv H H H H*#Q #Qr| #Q #Q #Q #Q #Q #Q #Q #Qr#   