Team Ai
Apppublic

documentExtractionag051/ExtractDocument

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
geometry_utils.cpython-313.pyc155 linesDownload Raw Back to __pycache__
1�

2a�_i�=��r�SSKrSSKrSSKrSSKrSSKJr "SS5r"SS5r"SS5rS	r	g)3�N)�Listc��\rSrSrSrSSjr\S5r\S5r\S5r	\S5r4S	rS5rSr
SrS
rSrSrg)�Rect�rNc��[RUl[=RS-
slXlX lX0lX@lXPlX`l[U=(d [R"55R5Ul6g)N�)
r�_id�id�x1�y1�x2�y2�7block_type�8confidence�str�uuid�uuid4�upper)�selfrrr
rrr�9uuid_values        �fC:\Users\poona\OneDrive\Documents\nextlifyai\Document_Automation\Document_Extraction\geometry_utils.py�__init__�
Rect.__init__sX���(�(������A�
����������$��$���10�2�d�j�j�l�3�9�9�;��	�c�:�URUR-S-$�N��r
r�rs r�xcenter�Rect.xcenter������$�'�'�!�Q�&�&rc�:�URUR-S-$r�rrrs r�ycenter�Rect.ycenterr"rc�4�URUR-11$�Nrrs r�width�12Rect.width����w�w���� � rc�4�URUR-13$r(r$rs r�height�Rect.height#r+rc��[UR5[UR5[UR5[UR5S.$)N�rrr
r)�intrrr
rrs r�get_geometry�Rect.get_geometry's8���d�g�g�,��d�g�g�,��d�g�g�,��d�g�g�,�	14�	15rc�z�[UR5URUR5URS.$)N)r16�	blockType�geometryr)rrrr2rrs r�as_block�
Rect.as_block/s1���d�i�i�.�����)�)�+��/�/�	17�	18rc	���[URUR5n[URUR5n[URUR5n[UR19UR205n[
[[XB-21S45[XS-22S45-5S5nUS:XagURUR-nUS:Xag[
U[U5-S-S5nU$)zx23Given two bounding boxes, it returns the percentage of the boxB within this box.24 25Returns:26    (int) Percent IOU score27rr�d��maxrr�minr
r�round�absr)r-�float�	r�boxB�xA�yA�xB�yB�	interArea�	boxB_area�ious	         r�intersection_pct�Rect.intersection_pct7s�������$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"���#�c�2�7�A�,�/�#�r�w��l�2C�C�D�a�H�	���>���J�J����,�	���>���Y��y�!1�1�S�8�!�<���28rc��[URUR5n[URUR5n[X2-29S5nUS:XagURnUS:Xag[U[
U5-S-S5nU$�z�30Measures the percentage of vertical (y-axis) overlap between two boxes,31relative to the height of boxB.32 33Returns:34    (float) percentage of vertical alignment35r�r:r�r<rr=rr-r>r@�rrBrDrF�interHeight�boxB_height�vals       r�vertical_alignment_pct�Rect.vertical_alignment_pctQ�x������$�'�'�
"��
����$�'�'�
"���"�'�1�o���!����k�k���!����[�5��#5�5��<�a�@���36rc��[URUR5n[URUR5n[X2-37S5nUS:XagURnUS:Xag[U[
U5-S-S5nU$�z�38Measures the percentage of horizontal (x-axis) overlap between two boxes,39relative to the width of boxB.40 41Returns:42    (float) percentage of horizontal alignment43rrNr:r�r<rr=r
r)r>r@�rrBrCrE�44interWidth�45boxB_widthrSs       r�horizontal_alignment_pct�Rect.horizontal_alignment_pcth�v������$�'�'�
"��
����$�'�'�
"�����!�_�46���?���Z�Z�47���?���Z�%�48�"3�3�s�:�A�>���49rc	��[URUR5n[URUR5n[URUR5n[UR50UR515n[
[[XB-52S45[XS-53S45-5S5nUS:Xag[URUR5n[URUR5n[URUR5n[UR54UR555n[
[XB-56S45[XS-57S45-S5nUS:Xag[
U[U5-S-S5nU$)z{58Given two bounding boxes, it returns the percentage of the intersection over union.59 60Returns:61    (int) Percent IOU score62rrr:)	r<rrr=r
rr>r?r@)	rrBrCrDrErFrG�63union_arearIs	         r�iou_pct�Rect.iou_pcts/������$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"���#�c�2�7�A�,�/�#�r�w��l�2C�C�D�a�H�	���>������$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"���3����|�,�s�B�G�Q�<�/@�@�!�D�64���?���Y��z�!2�2�c�9�1�=���65r)rrr66rrr
rr)rrrrNrNN)�__name__�67__module__�__qualname__�__firstlineno__r	r�propertyr r%r)r-r2r7rJrTr]rb�__static_attributes__�rrrrsz��68�C�69<��'��'��'��'��!��!��!��!�70�71��4�.�.rrc��\rSrSrSr\S5r\S5r\S5rSr	Sr72SrS	rS73r
SSjrSSjrSS
jrSSjrSrSrSrg)�TextRect�c	�d�XlX lX0lX@lXPlX`lXplX�lg)a�Initialize a TextRect object.74 75Args:76    x1 (int): The x-coordinate of the top-left corner.77    y1 (int): The y-coordinate of the top-left corner.78    x2 (int): The x-coordinate of the bottom-right corner.79    y2 (int): The y-coordinate of the bottom-right corner.80    confidence (float): The confidence score of the element.81    block_type (str): The type of block (e.g., 'text', 'image').82    text (str): The text content of the element.83    page_num (int): The page number where the element is located.84N�rrr
rrr�text�page_num)	rrrr
rrrrprqs	         rr�TextRect.__init__�s,����������$��$���	� �
rc�4�URUR-85$r(rrs rr)�TextRect.width�r+rc�4�URUR-86$r(r$rs rr-�TextRect.height�r+rc�r�URUR-S-URUR-S-4$r)rr
rrrs r�midpoint�TextRect.midpoint�s1�����4�7�7�"�a�'�$�'�'�D�G�G�*;�q�)@�A�Arc	���[URUR5n[URUR5n[URUR5n[UR87UR885n[
[[XB-89S45[XS-90S45-5S5nUS:XagURUR-nUS:Xag[
U[U5-S-S5nU$)z�Calculate the percentage of boxB within the current box.91 92Args:93    boxB (TextRect): Another bounding box.94 95Returns:96    float: The percentage of boxB within the current box.97rrr:r;rAs	         rrJ�TextRect.intersection_pct�s�������$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"��
����$�'�'�
"���#�c�2�7�A�,�/�#�r�w��l�2C�C�D�a�H�	���>���J�J����,�	���>���Y��y�!1�1�S�8�!�<���98rc��[URUR5n[URUR5n[X2-99S5nUS:XagURnUS:Xag[U[
U5-S-S5nU$rMrOrPs       rrT�TextRect.vertical_alignment_pct�rVrc��[URUR5n[URUR5n[X2-100S5nUS:XagURnUS:Xag[U[
U5-S-S5nU$rXrYrZs       rr]�!TextRect.horizontal_alignment_pct�r_rc���SURSURSURSURSURSUR101SURSURS	3$)102NzTextRect(x1=z, y1=z, x2=z, y2=z
, confidence=z
, block_type=z, text=z, page_num=�)rors r�__repr__�TextRect.__repr__s����d�g�g�Y�e�D�G�G�9�E�$�'�'��%����y�P]�^b�^m�^m�]n�n{�}A�}L�}L�|M�MT�UY�U^�U^�T_�_j�ko�kx�kx�jy�yz�{�	{rc��/nUGHnURUR:waM US:XaURUR:�aMBUS:XaURUR:aMdUS:XaURUR:�aM�US:XaURUR:aM�US:aURU5 M�US;a�US:XaNURUR:a1URUR:�aURU5 GMGMGMURU-103nURU-nURU:a'URU:�aURU5 GMmGMpGMsUS:XaNURUR:a1URUR:�aURU5 GM�GM�GM�URU-104nURU-n	URU	:dGM�URU:�dGMURU5 GM US:a U(aUR
U5n105U106bU107/$/$U$)a$Common helper for finding words in any direction.108 109Args:110    words: List of TextRect objects to search through111    threshold: Search threshold (0, positive, or negative)112    direction: One of 'left', 'right', 'above', 'below'113 114Returns:115    List of TextRect objects in the specified direction116�left�right�above�belowr)r�r�)rqr
rrr�append�find_nearest_word)r�words�	threshold�	direction�result_words�word�expanded_y1�expanded_y2�expanded_x1�expanded_x2�nearests           r�_find_words_in_direction�!TextRect._find_words_in_directions������D��}�}��
�
�-���F�"�t�w�w����'8���g�%�$�'�'�D�G�G�*;���g�%�$�'�'�D�G�G�*;���g�%�$�'�'�D�G�G�*;���1�}��#�#�D�)���-�-���>��w�w����(�T�W�W�t�w�w�->�$�+�+�D�1�.?�(�#'�'�'�I�"5�K�"&�'�'�I�"5�K��w�w��,����;�1F�$�+�+�D�1�2G�,���>��w�w����(�T�W�W�t�w�w�->�$�+�+�D�1�.?�(�#'�'�'�I�"5�K�"&�'�'�I�"5�K��w�w��,����;�1F�$�+�+�D�1�O�T�q�=�\��,�,�\�:�G� '� 3�G�9�;��;��rc�&�URXS5$)z+Find words to the left of the current text.r��r��rr�r�s   r�find_words_to_the_left_of_text�'TextRect.find_words_to_the_left_of_textHs���,�,�U�v�F�Frc�&�URXS5$)z,Find words to the right of the current text.r�r�r�s   r�find_words_to_the_right_of_text�(TextRect.find_words_to_the_right_of_textL����,�,�U�w�G�Grc�&�URXS5$)z"Find words above the current text.r�r�r�s   r�find_words_above_the_text�"TextRect.find_words_above_the_textPr�rc�&�URXS5$)z"Find words below the current text.r�r�r�s   r�find_words_below_the_text�"TextRect.find_words_below_the_textTr�rc�^�URURURUR4$r(r0rs r�get_coordinates�TextRect.get_coordinatesXs!��������$�'�'�4�7�7�3�3rc���Sn[S5nUHfnURUR:waMURUR-117S-URUR-118S--S-nXS:dMbUnUnMh U$)N�infrg�?)r@rqrr)rr��nearest_word�nearest_distancer��distances      rr��TextRect.find_nearest_word[s{���� ��<���D��}�}��
�
�-�����4�7�7�*�q�0�D�G�G�d�g�g�4E�!�3K�K�PS�S�H��*�#+� �#����r)rrrqrprr
rrN)r)rdrerfrgrrhr)r-rxrJrTr]r�r�r�r�r�r�r�r�rirjrrrlrl�s���!�,�!��!��!��!��B��B��2�.�.{�;�zG�H�H�H�4�
rrlc���\rSrSr\S5r\S5r\SS\\4Sjj5r	\S5r119\S5r\S5rS	\S\S120-4Sjr
\S\S\\4S
j5rSrg121)�
DocumentWordsikc�>�UR5R5$r()�strip�lower)r�s r�_convert_lower�DocumentWords._convert_lowerms���z�z�|�!�!�#�#rc���/nUH\nURU:�dMURU::dM'URU:�dM9URU::dMKUR	U5 M^ U$r(�rr
rrr�)r�rrr
r�122area_wordsr�s       r�find_words_in_area� DocumentWords.find_words_in_areaqsQ���123��D��w�w�"�}����B��4�7�7�b�=�T�W�W�PR�]��!�!�$�'���r�returnc���[U[5(aU/n/nUHZnUHQnURUR5URU5:XdM3UR	U5 U(dMMUs s $ M\ U$r()�124isinstancerr�rpr�)r�125word_textsr��126only_first�all_matchesr��	word_texts       r�find_words_on_document�$DocumentWords.find_words_on_documentysr���j�#�&�&�$��J����D�'�	��&�&�t�y�y�1�T�5H�5H��5S�S��&�&�t�,�!�z�*�*�	(���rc��/nUH8nURU:�dMURU::dM'URU5 M: U$r()rr
r�)rr
r��column_wordsr�s     r�find_words_in_column�"DocumentWords.find_words_in_column�s<�����D��w�w�"�}����B���#�#�D�)���rc��/nUH8nURU:�dMURU::dM'URU5 M: U$r()rrr�)rrr��	row_wordsr�s     r�find_words_in_row�DocumentWords.find_words_in_row�s<���	��D��w�w�"�}����B��� � ��&���rc���/nUH\nURU:�dMURU::dM'URU:�dM9URU::dMKUR	U5 M^ U$r(r�)rrr
rr��	box_wordsr�s       r�find_in_box_words�DocumentWords.find_in_box_words�sQ���	��D��w�w�"�}����B��4�7�7�b�=�T�W�W�PR�]�� � ��&���r�key_wordNc���Sn[S5nUH_nURUR:waMURUR:�dM;URUR-127nXd:dM[UnUnMa U$)Nr�)r@rqrr
)rr��value_candidates�
closest_value�closest_distance�128value_wordr�s       r�find_value_for_key� DocumentWords.find_value_for_key�so���
� ��<��*�J��"�"�h�&7�&7�7���}�}����+�%�=�=�8�;�;�6���.�'/�$�$.�M�+��r�patternc��[R"U5n/nUH6nURUR5(dM%UR	U5 M8 U$r()�re�compile�matchrpr�)r�r��regex�
matched_wordsr�s     r�find_words_by_regex�!DocumentWords.find_words_by_regex�sH���129�130�7�#���
��D��{�{�4�9�9�%�%��$�$�T�*���rrj)F)rdrerfrg�staticmethodr�r�rrlr�r�r�r�r�rr�rirjrrr�r�ks����$��$������131�4�PX�>�132��133��������������8���QU�
��"��S��$�x�.���rr�c�*�S/nUSSHnURUSU-5 M [URS-134S5nX$nURnURU-nUR135UR-136nURUR-137nXgX�4$)ap138Compute global bounds for an object whose y-coordinates are page-relative.139 140Parameters141----------142obj : Any object with attributes:143    - page_num144    - x1, y1145    - x2, y2146page_heights : list[int|float]147    A list of heights of each document page in order.148 149Returns150-------151(x, y, w, h) : tuple[float]152    Global coordinates with y adjusted using cumulative height.153rN�����r)r�r<rqrrr
r)	�obj�page_heights�154cumulative�h�pg_idx�y_offset�x�y�ws	         r�compute_global_boundsr��s���(��J�
�#�2�
�����*�R�.�1�,�-�������!�1�
%�F��!�H����A������A��������A��������A���:�r)155�json�osrr��typingrrrlr�r�rjrr�<module>r�s@���	��	��T�T�nJ�J�ZK�K�\ r