a
    ¡˜ëjô  ã                	   @  sÎ   d dl mZ d dlmZmZ d dlZd dlmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZ erpd dl	mZ dd	d	d	d	d
dddœdd„Zd dd
dd
ddœdd„Zd!dd
dddœdd„Zdddœdd„ZdS )"é    )Úannotations)ÚTYPE_CHECKINGÚOptionalN)ÚSource)Ú
TextRegionÚTextRegions)ÚLayoutElementÚLayoutElementsÚpartition_groups_from_regions)ÚElementType)Ú	Rectanglezint | floatzOptional[str]zOptional[Source]r   )Úx1Úy1Úx2Úy2ÚtextÚsourceÚreturnc                 C  s   t j| |||||d�S )Ú )r   r   )r   Úfrom_coords)r   r   r   r   r   r   © r   úo/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/pdf_image/inference_utils.pyÚbuild_text_region_from_coords   s    	r   z'Rectangle'r   )Úbboxr   r   Úelement_typer   c                 C  s   t | |||d�S )r   )r   r   r   Útype)r   )r   r   r   r   r   r   r   Úbuild_layout_element    s    r   Fr   Úboolr	   )Úocr_regionsÚocr_textÚgroup_by_ocr_textr   c                 C  sú   g }|r¶|  d¡}t | jj¡ t¡}t t|ƒ¡}|D ]z}g }| 	dd¡  ¡ }	t
| j| ƒD ]4\}
}|	sn q”||	v r^| || |
 ¡ |	 |¡ q^|sšq8d||< | |  |¡¡ q8nt| ƒ}t dd„ |D ƒ¡}t|j|j|jt |jj¡dtjid�S )	z.
    Get layout elements from OCR regions
    z

Ú
ú Fc                 S  s   g | ]}t |ƒ‘qS r   )Úmerge_text_regions)Ú.0Úgroupr   r   r   Ú
<listcomp>K   ó    z:build_layout_elements_from_ocr_regions.<locals>.<listcomp>r   )Úelement_coordsÚtextsÚsourcesZelement_class_idsZelement_class_id_map)ÚsplitÚnpZonesr)   ÚshapeÚastyper   ZarangeÚlenÚreplaceÚ	enumerateÚappendÚremoveÚslicer
   r   Ú	from_listr	   r(   r*   Úzerosr   ZUNCATEGORIZED_TEXT)r   r   r    Zgrouped_regionsZtext_sectionsÚmaskÚindicesZtext_sectionÚregionsÚwordsÚir   Zmerged_regionsr   r   r   Ú&build_layout_elements_from_ocr_regions+   s6    	
ûr<   )r9   r   c                 C  s€   | st dƒ‚| j ¡  t¡}| j ¡  t¡}| j ¡  t¡}| j ¡  t¡}d 	dd„ | j
D ƒ¡}| jd }t ||||||¡S )zá
    Merge a list of TextRegion objects into a single TextRegion.

    Parameters:
    - group (TextRegions): A group of TextRegion objects to be merged.

    Returns:
    - TextRegion: A single merged TextRegion object.
    z/The text regions to be merged must be provided.r"   c                 S  s   g | ]}|r|‘qS r   r   )r$   r   r   r   r   r&   h   r'   z&merge_text_regions.<locals>.<listcomp>r   )Ú
ValueErrorr   Úminr.   Úfloatr   r   Úmaxr   Újoinr)   r*   r   r   )r9   Zmin_x1Zmin_y1Zmax_x2Zmax_y2Zmerged_textr   r   r   r   r#   U   s    
r#   )NN)NNN)NF)Ú
__future__r   Útypingr   r   Únumpyr,   Z unstructured_inference.constantsr   Z)unstructured_inference.inference.elementsr   r   Z.unstructured_inference.inference.layoutelementr   r	   r
   Zunstructured.documents.elementsr   r   r   r   r<   r#   r   r   r   r   Ú<module>   s&     ú   ü  ý*