a
    ¡˜ëjE  ã                   @  s  U d Z ddlmZ ddlZddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ ed
ƒZG dd„ deƒZdddœdd„Zdddddœdd„Zddddœdd„Zejdd�G dd„ dƒƒZeeƒeeƒd œZd!e d"< dS )#z³Handles dispatch of elements to a chunking-strategy by name.

Also provides the `@add_chunking_strategy` decorator which is the chief current user of "by-name"
chunking dispatch.
é    )ÚannotationsN)ÚAnyÚCallableÚIterableÚOptionalÚProtocol)Ú	ParamSpec)Úchunk_elements)Úchunk_by_title)ÚElement)Úget_call_args_applying_defaultsÚlazypropertyÚ_Pc                   @  s"   e Zd ZdZddddœdd„ZdS )	ÚChunkerz*Abstract interface for chunking functions.úIterable[Element]zOptional[int]úlist[Element])ÚelementsÚmax_charactersÚreturnc                C  s   dS )a  A chunking function must have this signature.

        In particular it must minimally have an `elements` parameter and all chunkers will have a
        `max_characters` parameter (doesn't need to follow `elements` directly). All others can
        vary by chunker.
        N© )Úselfr   r   r   r   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/chunking/dispatch.pyÚ__call__   s    	zChunker.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   zCallable[_P, list[Element]])Úfuncr   c                   sN   ˆ j r*dˆ jjv r*dˆ j vr*ˆ  j d7  _ t ˆ ¡ddddœ‡ fdd„ƒ}|S )	zãDecorator for chunking text.

    Chunks the element sequence produced by the partitioner it decorates when a `chunking_strategy`
    argument is present in the partitioner call and it names an available chunking strategy.
    Úchunking_strategya…  
chunking_strategy
	Strategy used for chunking text into larger or smaller elements.
	Defaults to `None` with optional arg of 'basic' or 'by_title'.
	Additional Parameters:
		multipage_sections
			If True, sections can span multiple pages. Defaults to True.
		combine_text_under_n_chars
			Combines elements (for example a series of titles) until a section
			reaches a length of n characters. Only applies to 'by_title' strategy.
		new_after_n_chars
			Cuts off chunks once they reach a length of n characters; a soft max.
		max_characters
			Chunks elements text and text_as_html (if present) into chunks
			of length n characters, a hard max.z_P.argsz	_P.kwargsr   )ÚargsÚkwargsr   c                    sN   ˆ | i |¤Ž}t ˆ g| ¢R i |¤Ž}| dd¡}|du r<|S t||fi |¤ŽS )z1The decorated function is replaced with this one.r   N)r   ÚpopÚchunk)r   r    r   Z	call_argsr   ©r   r   r   ÚwrapperE   s    z&add_chunking_strategy.<locals>.wrapper)r   Ú__code__Úco_varnamesÚ	functoolsÚwraps)r   r$   r   r#   r   Úadd_chunking_strategy'   s    

ÿÿÿr)   r   Ústrr   r   )r   r   r    r   c                   sL   t  |¡‰ ˆ du r$tdt|ƒ› �ƒ‚‡ fdd„| ¡ D ƒ}ˆ j| fi |¤ŽS )zQDispatch chunking of `elements` to the chunking function for `chunking_strategy`.Nzunrecognized chunking strategy c                   s    i | ]\}}|ˆ j v r||“qS r   )Úkw_arg_names)Ú.0ÚkÚv©Zchunker_specr   r   Ú
<dictcomp>c   ó    zchunk.<locals>.<dictcomp>)Ú_chunker_registryÚgetÚ
ValueErrorÚreprÚitemsÚchunker)r   r   r    Zchunking_kwargsr   r/   r   r"   Z   s
    
r"   ÚNone)Únamer7   r   c                 C  s   t |ƒt| < dS )zVMake chunker available by using `name` as `chunking_strategy` arg in partitioner call.N)Ú_ChunkerSpecr2   )r9   r7   r   r   r   Úregister_chunking_strategyh   s    r;   T)Úfrozenc                   @  s,   e Zd ZU dZded< eddœdd„ƒZdS )	r:   zA registry entry for a chunker.r   r7   ztuple[str, ...])r   c                 C  s    t  | j¡}tdd„ |jD ƒƒS )z™Keyword arguments supported by this chunker.

        These are all arguments other than the required `elements: list[Element]` first parameter.
        c                 s  s   | ]}|d kr|V  qdS )r   Nr   )r,   Úkeyr   r   r   Ú	<genexpr>{   r1   z,_ChunkerSpec.kw_arg_names.<locals>.<genexpr>)ÚinspectÚ	signaturer7   ÚtupleÚ
parameters)r   Úsigr   r   r   r+   t   s    z_ChunkerSpec.kw_arg_namesN)r   r   r   r   Ú__annotations__r   r+   r   r   r   r   r:   m   s   
r:   )ÚbasicZby_titlezdict[str, _ChunkerSpec]r2   )!r   Ú
__future__r   ZdataclassesÚdcr'   r?   Útypingr   r   r   r   r   Ztyping_extensionsr   Zunstructured.chunking.basicr	   Zunstructured.chunking.titler
   Zunstructured.documents.elementsr   Zunstructured.utilsr   r   r   r   r)   r"   r;   Z	dataclassr:   r2   rD   r   r   r   r   Ú<module>   s(   3
þ