a
    ¡˜ëjû%  ã                   @  sê   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 ddddœZ
ddœd	d
„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zdddœdd„Zd)ddddœdd „Zd!d"d#œd$d%„Zd!dd#œd&d'„Zd(S )*é    )Úannotations)ÚAnyÚDictÚList)ÚBeautifulSoup)Úcells_to_htmlÚ ©Ú	row_indexÚ	col_indexÚcontentzList[Dict[str, Any]])Úcellsc                   s  t | dd„ d�}tƒ }|D ]æ}|d dks6|d dkrœt|d |d |d  ƒD ]J}t|d |d |d  ƒD ]*}||f|d |d fkrn| ||f¡ qnqP|d |d f|v r|d |d  ‰‰ ‡ ‡fd	d
„|D ƒ}|D ]}|d  d7  < qÚ| ˆˆ f¡ qœq|S )a  Move cells to the right if spanned cells have an influence on the rendering.

    Args:
        cells: List of cells in the table in Deckerd format.

    Returns:
        List of cells in the table in Deckerd format with cells moved to the right if spanned.
    c                 S  s   | d | d fS )NÚyÚx© )r   r   r   új/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/metrics/table/table_extraction.pyÚ<lambda>   ó    z/_move_cells_for_spanned_cells.<locals>.<lambda>©ÚkeyÚwé   Úhr   r   c                   s(   g | ] }|d  ˆkr|d ˆ kr|‘qS )r   r   r   )Ú.0Úc©Zcell_xZcell_yr   r   Ú
<listcomp>"   r   z1_move_cells_for_spanned_cells.<locals>.<listcomp>)ÚsortedÚsetÚrangeÚaddÚremove)r   Úsorted_cellsZcells_occupied_by_spannedÚcellÚiÚjZcells_to_the_rightZcell_to_mover   r   r   Ú_move_cells_for_spanned_cells   s    	r&   Ústr)r   Úreturnc              
   C  s–   t | dƒ}| d¡}| dg¡}g }t|ƒD ]`\}}| ddg¡}t|ƒD ]@\}}	||t|	j dd¡ƒt|	j dd¡ƒ|	jd	œ}
| |
¡ qJq,t	|ƒS )
zÝConvert html format to Deckerd table structure.

    Args:
        content: The html content with a table to extract.

    Returns:
        A list of dictionaries where each dictionary represents a cell in the table.
    zhtml.parserÚtableÚtrÚthÚtdZcolspanr   Zrowspan)r   r   r   r   r   )
r   ÚfindZfind_allÚ	enumerateÚintÚattrsÚgetÚtextÚappendr&   )r   Zsoupr)   ÚrowsÚ
table_datar$   Úrowr   r%   Ú	cell_datar#   r   r   r   Úhtml_table_to_deckerd)   s    


ûr8   )r   r(   c                 C  sª   g }dd„ | D ƒ}t dd„ |D ƒƒ}tt|ƒƒ}| D ]h}tt|d |d |d  ƒƒtt|d |d |d  ƒƒ|d |d |d	 |d |v d
œ}| |¡ q4t|ƒ}|S )zÜConvert Deckerd table structure to html format.

    Args:
        cells: List of dictionaries where each dictionary represents a cell in the table.

    Returns:
        A string with the html content of the table.
    c                 S  s   g | ]}|d  dkr|‘qS )r   r   r   ©r   r#   r   r   r   r   R   r   z)deckerd_table_to_html.<locals>.<listcomp>c                 s  s   | ]}|d  V  qdS )r   Nr   r9   r   r   r   Ú	<genexpr>S   r   z(deckerd_table_to_html.<locals>.<genexpr>r   r   r   r   r   )Zrow_numsZcolumn_numsr   r   z	cell textzcolumn header)Úmaxr   r   Úlistr3   r   )r   Ztransformer_cellsZfirst_row_cellsÚheader_lengthZheader_rowsr#   r7   r)   r   r   r   Údeckerd_table_to_htmlF   s    	
úr>   c                 C  s   t | ƒ}t|ƒS )a7  Convert html format to table structure. As a middle step it converts
    html to the Deckerd format as it's more convenient to work with.

    Args:
        content: The html content with a table to extract.

    Returns:
        A list of dictionaries where each dictionary represents a cell in the table.
    )r8   Ú_convert_table_from_deckerd)r   Zdeckerd_cellsr   r   r   Ú_convert_table_from_htmld   s    
r@   c              	   C  sd   g }| D ]V}z|d |d |d dœ}W n* t y>   t}Y n tyR   t}Y n0 | |¡ q|S )záConvert deckerd format to table structure.

    Args:
      content: The deckerd formatted content with a table to extract.

    Returns:
      A list of dictionaries where each dictionary represents a cell in the table.
    r   r   r   r	   )ÚKeyErrorÚ
EMPTY_CELLÚ	TypeErrorr3   )r   r5   r)   r7   r   r   r   r?   r   s    	ý

r?   zList[List[Dict[str, Any]]])r5   r(   c                 C  s   t | dd„ d�S )Nc                 S  s   | d | d fS )Nr
   r   r   )r#   r   r   r   r   Œ   r   z#_sort_table_cells.<locals>.<lambda>r   )r   )r5   r   r   r   Ú_sort_table_cells‹   s    rD   )Úfile_elementsr(   c                 C  sŠ   g }| D ]|}d|v r|d dkrd|v rzt |d ƒ}| t|ƒ¡ W q ty‚ } z$td|› �ƒ | i ¡ W Y d}~qd}~0 0 q|S )a  Extracts and converts tables data to a structured format based on the specified table type.

    Args:
        file_elements: List of elements from the ground truth file.

    Returns:
        A list of tables with each table represented as a list of cell data dictionaries.

    ÚtypeÚTabler2   z$Error converting ground truth data: N)r?   r3   rD   Ú	ExceptionÚprint)rE   Zground_truth_table_dataÚelementZconverted_dataÚer   r   r   Ú,extract_and_convert_tables_from_ground_truth�   s    ÿ"rL   Úhtml)rE   Úsource_typer(   c           	      C  s„   t tdœ}||vr"td|› d�ƒ‚|| }|dkr6tnt }g }| D ]<}| d¡dkrB||ƒ}|sh||ƒ}|rBt|ƒ}| |¡ qB|S )ap  Extracts and converts table data to a structured format

    Args:
      file_elements: List of elements from the file.
      source_type: 'cells' or 'html'. 'cells' refers to reading 'table_as_cells' field while
        'html' is extracted from 'text_as_html'

    Returns:
      A list of tables with each table represented as a list of cell data dictionaries.

    )rM   r   zsource_type z: is not valid. Allowed source_types are "html" and "cells"r   rF   rG   )Úextract_cells_from_text_as_htmlÚ!extract_cells_from_table_as_cellsÚ
ValueErrorr1   rD   r3   )	rE   rN   Z$source_type_to_extraction_strategiesZextract_cells_fnZfallback_extract_cells_fnZpredicted_table_datarJ   Zextracted_cellsr"   r   r   r   Ú*extract_and_convert_tables_from_predictionª   s,    þ
ÿÿýrR   zDict[str, Any]zList[Dict[str, Any]] | None)rJ   r(   c              
   C  sf   | d   d¡}|rd|vrdS d}zt|ƒ}W n2 ty` } ztd|› �ƒ W Y d}~n
d}~0 0 |S )aé  Extracts and parse cells from "text_as_html" field in Element structure

    Args:
        element: Example element:
        {
            "type": "Table",
            "metadata": {
                "text_as_html": "<table>
                                    <thead>
                                        <tr>
                                            <th>Month A.</th>
                                        </tr>
                                    </thead>
                                    </tbody>
                                        <tr>
                                            <td>22</td><
                                        </tr>
                                    </tbody>
                                </table>"
            }
        }

    Returns:
        List of extracted cells in a format:
        [
            {
                "row_index": 0,
                "col_index": 0,
                "content": "Month A.",
            },
            ...,
        ]
    ÚmetadataZtext_as_htmlz<table>Nz*Error converting Unstructured table data: )r1   r@   rH   rI   )rJ   ÚvalÚpredicted_cellsrK   r   r   r   rO   Õ   s    "$rO   c                 C  s"   | d   d¡}d}|rt|ƒ}|S )aj  Extracts and parse cells from "table_as_cells" field in Element structure

    Args:
        element: Example element:
        {
            "type": "Table",
            "metadata": {
                "table_as_cells": [{"x": 0, "y": 0, "w": 1, "h": 1, "content": "Month A."},
                                   {"x": 0, "y": 1, "w": 1, "h": 1, "content": "22"}]
            }
        }

    Returns:
        List of extracted cells in a format:
        [
            {
                "row_index": 0,
                "col_index": 0,
                "content": "Month A.",
            },
            ...,
        ]
    rS   Ztable_as_cellsN)r1   r?   )rJ   rU   Zconverted_cellsr   r   r   rP     s
    rP   N)rM   )Ú
__future__r   Útypingr   r   r   Zbs4r   Z$unstructured_inference.models.tablesr   rB   r&   r8   r>   r@   r?   rD   rL   rR   rO   rP   r   r   r   r   Ú<module>   s$   ý ÿ+/