a
    ¡˜ëjó  ã                   @   s&  d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZ d eeeejdœdd„Zd!eeeeedœd	d
„Zd"eeeeedœdd„Zeee dœdd„Zeee dœdd„Zeee dœdd„Zeee dœdd„Zeee j  dœdd„Zedœdd„Zedœdd„Zeee dœdd„ZdS )#é    N)ÚListÚOptional)ÚEMAIL_ADDRESS_PATTERNÚEMAIL_DATETIMETZ_PATTERNÚIMAGE_URL_PATTERNÚIP_ADDRESS_NAME_PATTERNÚIP_ADDRESS_PATTERN_REÚMAPI_ID_PATTERNÚUS_PHONE_NUMBERS_RE)ÚtextÚpatternÚindexÚreturnc                 C   sn   t |tƒr|dk r"td|› d�ƒ‚d }tt || ¡ƒD ]\}}||kr6|}q6|d u rjtd|› d|› d�ƒ‚|S )Nr   zThe index is z'. Index must be a non-negative integer.zResult with index z& was not found. The largest index was Ú.)Ú
isinstanceÚintÚ
ValueErrorÚ	enumerateÚreÚfinditer)r   r   r   Úregex_matchÚiÚresult© r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/cleaners/extract.pyÚ_get_indexed_match   s    r   T)r   r   r   Ústripr   c                 C   s4   t | ||ƒ}| ¡ \}}| d|… }|r0| ¡ S |S )a  Extracts texts that occurs before the specified pattern. By default, it will use
    the first occurrence of the pattern (index 0). Use the index kwarg to choose a different
    index.

    Input
    -----
    strip: If True, removes trailing whitespace from the extracted string
    N)r   ÚspanÚrstrip)r   r   r   r   r   ÚstartÚ_Úbefore_textr   r   r   Úextract_text_before   s    	r"   c                 C   s4   t | ||ƒ}| ¡ \}}| |d… }|r0| ¡ S |S )a  Extracts texts that occurs before the specified pattern. By default, it will use
    the first occurrence of the pattern (index 0). Use the index kwarg to choose a different
    index.

    Input
    -----
    strip: If True, removes leading whitespace from the extracted string
    N)r   r   Úlstrip)r   r   r   r   r   r    Úendr!   r   r   r   Úextract_text_after.   s    	r%   )r   r   c                 C   s   t  t|  ¡ ¡S ©N)r   Úfindallr   Úlower©r   r   r   r   Úextract_email_address=   s    r*   c                 C   s   t  t| ¡S r&   )r   r'   r   r)   r   r   r   Úextract_ip_addressA   s    r+   c                 C   s   t  t| ¡S r&   )r   r'   r   r)   r   r   r   Úextract_ip_address_nameE   s    r,   c                 C   s   t  t| ¡}dd„ |D ƒ}|S )Nc                 S   s   g | ]}|  d d¡‘qS )ú;Ú )Úreplace)Ú.0Zmidr   r   r   Ú
<listcomp>K   ó    z#extract_mapi_id.<locals>.<listcomp>)r   r'   r	   )r   Zmapi_idsr   r   r   Úextract_mapi_idI   s    r3   c                 C   s2   t  t| ¡}t|ƒdkr*tj |d d¡S d S d S )Nr   z%a, %d %b %Y %H:%M:%S %z)r   r'   r   ÚlenÚdatetimeÚstrptime)r   Zdate_extractionsr   r   r   Úextract_datetimetzO   s    r7   r)   c                 C   s6   t  | ¡}|du rdS | ¡ \}}| ||… }| ¡ S )a  Extracts a US phone number from a section of text that includes a phone number. If there
    is no phone number present, the result will be an empty string.

    Example
    -------
    extract_phone_number("Phone Number: 215-867-5309") -> "215-867-5309"
    Nr.   )r
   Úsearchr   r   )r   r   r   r$   Zphone_numberr   r   r   Úextract_us_phone_numberW   s    
r9   )r   c                 C   sÌ   d\}}}}|   ¡ }td|d vd|d v gƒr:|||fS tj d|d d�}|d sZ|d= t|d ƒdkrt|||fS |^}}|rÂz|^}}}W n ty¤   |}Y n0 d	 |¡}|r¾d	 |¡nd
}|||fS )a¶  Extracts the start of bulleted text sections bullets
    accounting numeric and alphanumeric types.

    Output
    -----
    tuple(section, sub_section, sub_sub_section): Each bullet partition
    is a string or None if not present.

    Example
    -------
    This is a very important point -> (None, None, None)
    1.1 This is a very important point -> ("1", "1", None)
    a.1 This is a very important point -> ("a", "1", None)
    )NNNNr   r   z..z[\.])r   Ústringéÿÿÿÿé   r.   N)ÚsplitÚanyr   r4   r   Újoin)r   ÚaÚbÚcÚtempZtext_spZbulletr    r   r   r   Úextract_ordered_bulletsh   s$    



rD   c                 C   s   t  t| ¡S r&   )r   r'   r   r)   r   r   r   Úextract_image_urls_from_htmlŽ   s    rE   )r   )r   T)r   T)r5   r   Útypingr   r   Zunstructured.nlp.patternsr   r   r   r   r   r	   r
   Ústrr   ÚMatchr   Úboolr"   r%   r*   r+   r,   r3   r7   r9   ÚtuplerD   rE   r   r   r   r   Ú<module>   s   $&