
    Mi/                       % S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
JrJrJrJr  SSKJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  \" S5      r  " S S\5      r!S"S jr"S#S jr#S$S jr$\RJ                  " SS9 " S S5      5       r&\&" \5      \&" \5      S.r'S\(S'   S%S jr)S&S jr*S'S jr+S(S jr,S)S jr-S*S jr.S+S  jr/S,S! jr0g)-zHandles 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)cached_property)AnyCallableIterableOptionalProtocol)ParserErrortostring)fragment_fromstring)	ParamSpec)chunk_elements)chunk_by_title)ElementTable
TableChunk)get_call_args_applying_defaults_Pc                  .    \ rS rSrSr      SS jrSrg)Chunker   z*Abstract interface for chunking functions.c                   g)zA 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 )selfelementsmax_characterss      x/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/unstructured/chunking/dispatch.py__call__Chunker.__call__   s     	    r   N)r   Iterable[Element]r   zOptional[int]returnlist[Element])__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r    r   r   r      s"    4	)	>K			r    r   c                   ^  T R                   (       a?  ST R                  R                  ;   a%  ST R                   ;  a  T =R                   S-  sl         [        R                  " T 5      SU 4S jj5       nU$ )zDecorator 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.
		repeat_table_headers
			Default: True. Repeat detected table headers on continuation
			table chunks. Set to False to opt out.
		skip_table_chunking
			Default: False. When True, Table elements are passed through
			unchanged without being split into TableChunk elements.c                 x   > T" U 0 UD6n[        T/U Q70 UD6nUR                  SS5      nUc  U$ [        X$40 UD6$ )z1The decorated function is replaced with this one.r+   N)r   popchunk)argskwargsr   	call_argsr+   funcs        r   wrapper&add_chunking_strategy.<locals>.wrapperO   s[    
 (( 4DJ4J6J	%MM*=tD $O X>I>>r    )r/   z_P.argsr0   z	_P.kwargsr"   r#   )r(   __code__co_varnames	functoolswraps)r2   r3   s   ` r   add_chunking_strategyr9   +   se     ||t}}888=PX\XdXd=dP	
. __T? ?" Nr    c                    [         R                  U5      nUc  [        S[        U5       35      eUR	                  5        VVs0 s H  u  pEXCR
                  ;   d  M  XE_M     nnnUR                  " U 40 UD6$ s  snnf )zQDispatch chunking of `elements` to the chunking function for `chunking_strategy`.zunrecognized chunking strategy )_chunker_registryget
ValueErrorrepritemskw_arg_nameschunker)r   r+   r0   chunker_speckvchunking_kwargss          r   r.   r.   d   s|    $(():;L:4@Q;R:STUU )/Y!?X?X:XtqtOY<O<< Zs   A8A8c                (    [        U5      [        U '   g)zVMake chunker available by using `name` as `chunking_strategy` arg in partitioner call.N)_ChunkerSpecr;   )namerA   s     r   register_chunking_strategyrI   r   s    *73dr    T)frozenc                  :    \ rS rSr% SrS\S'    \SS j5       rSrg)	rG   w   zA registry entry for a chunker.r   rA   c                z    [         R                  " U R                  5      n[        S UR                   5       5      $ )zKeyword arguments supported by this chunker.

These are all arguments other than the required `elements: list[Element]` first parameter.
c              3  4   #    U  H  oS :w  d  M
  Uv   M     g7f)r   Nr   ).0keys     r   	<genexpr>,_ChunkerSpec.kw_arg_names.<locals>.<genexpr>   s     HNSZ6GSSNs   		)inspect	signaturerA   tuple
parameters)r   sigs     r   r@   _ChunkerSpec.kw_arg_names~   s-     -HCNNHHHr    r   N)r"   tuple[str, ...])	r$   r%   r&   r'   r(   __annotations__r   r@   r)   r   r    r   rG   rG   w   s"    )NI Ir    rG   )basicby_titlezdict[str, _ChunkerSpec]r;   c                   U  Vs/ s H  n[        U[        5      (       d  M  UPM     nnU(       d  / $ 0 nU H:  nUR                  R                  nUc  M  XS;  a  / X5'   X5   R	                  U5        M<     / nSS jnUR                  5        H,  nUR                  US9  UR	                  [        U5      5        M.     U$ s  snf )aX  Reconstruct original tables from a mixed list of chunked elements.

Filters `TableChunk` elements, groups them by `table_id`, orders by `chunk_index`, and
merges each group into a single `Table` with combined text and HTML. Non-`TableChunk`
elements are ignored. Returns reconstructed tables in reading order (order of first chunk
appearance).
c                F    U R                   R                  nUS L Uc  S4$ U4$ )Nr   )metadatachunk_index)r.   r`   s     r   _chunk_sort_key6reconstruct_table_from_chunks.<locals>._chunk_sort_key   s.    nn00t#+*=QOO;OOr    )rP   )r.   r   r"   ztuple[bool, int])
isinstancer   r_   table_idappendvaluessort_merge_table_chunks)	r   etable_chunksgroupsr.   tidtablesra   groups	            r   reconstruct_table_from_chunksro      s      (Ex!:a+DAxLE	 +-Fnn%%;FK5!  FP 


')%01 ! M3 Fs
   B>B>c                  ^ SR                  U4S jU  5       5      n[        R                  " U S   R                  5      nSUl        SUl        SUl        SUl        [        S U  5       5      (       a  [        S5      n[        U 5      u  pEU(       a6  [        S5      nU H  nUR                  U5        M     UR                  U5        U  Hl  n[        UR                  R                  5      n	[        U5      n
U	R                  S5      nU
nXS   L a	  U(       a  UnXS  H  nUR                  U5        M     Mn     [        U[         S	9Ul        OSUl        [#        XS
9$ )zMMerge an ordered list of TableChunks from the same table into a single Table. c              3  P   >#    U  H  n[        U5      =m(       d  M  Tv   M     g 7fN)_strip_carried_over_header_text)rO   r.   
chunk_texts     r   rQ   &_merge_table_chunks.<locals>.<genexpr>   s'       &u9XY^9_+_:+_

s   &	&r   Nc              3  L   #    U  H  oR                   R                  v   M     g 7frs   )r_   text_as_html)rO   cs     r   rQ   rv      s     
3Fq::""Fs   "$z<table></table>z<thead></thead>+./tr | ./thead/tr | ./tbody/tr | ./tfoot/tr)encoding)textr_   )joincopydeepcopyr_   is_continuationrd   r`   num_carried_over_header_rowsallr   _first_carried_header_rowsre   rx   _num_carried_over_header_rowsxpathr   strr   )chunksr|   r_   combinedcanonical_header_row_countcanonical_header_rowstheadrowry   parsedcarried_over_header_rowsrows
skip_countru   s                @r   rh   rh      sD    88  & D
 }}VAY//0H#HHH,0H) 
3F
333&'89<VW]<^9" '(9:E,S! -OOE"A()@)@AF'DQ'G$<< MND1J1I~"<7
K($ )  !)C @ $d..r    c                D    U R                   R                  nU=(       d    S$ )zHeader rows prepended synthetically to this chunk.

Reconstruction can be called on user-provided/deserialized chunks, so treat missing values as
"no carried header rows."
r   )r_   r   )r.   values     r   r   r      s     NN77E:Ar    c                   [        U S   R                  R                  5      nUc  S/ 4$ U  H  n[        U5      nUS::  a  M  [        UR                  R                  5      nUc  M:  U[	        U5      :  a  MK  USU n[        X5      (       d  Mb  X4SU  Vs/ s H  n[        R                  " U5      PM     sn4s  $    S/ 4$ s  snf )zLHeader rows from first continuation chunk carrying repeated headers, if any.r   N)_top_level_table_rowsr_   rx   r   len_leading_row_texts_matchr~   r   )r   first_chunk_rowsr.   carried_row_countr   carried_rowsr   s          r   r   r      s    ,VAY-?-?-L-LM"u9%@!$U^^%@%@A<s4y(../'(8GG FXGXAY"ZAY#4==#5AY"ZZZ! $ b5L #[s    Cc                x    U (       d  g [        U 5      nUR                  S5      $ ! [        [        4 a     gf = f)zBTop-level rows from a table fragment, preserving section ordering.Nrz   )r   r
   r=   r   )rx   r   s     r   r   r      sB    $\2 <<EFF $ s   & 99c                    [        U 5      [        U5      :  a  g[        X5       H  u  p#[        U5      [        U5      :w  d  M    g   g)zPTrue when carried rows match first chunk's leading rows by normalized cell text.FT)r   zip_row_text_signature)r   r   	first_rowcarried_rows       r   r   r     sG    
s<00"%&6"E	y)-@-MM #F r    c                F    [        S U R                  SS5       5       5      $ )z%Normalized cell text tuple for a row.c              3  |   #    U  H2  nS R                  UR                  5       R                  5       5      v   M4     g7frq   Nr}   text_contentsplitrO   cells     r   rQ   &_row_text_signature.<locals>.<genexpr>  s0     XCW4$++-33566CW   :<tdth)rU   iter)r   s    r   r   r     s    X388DRVCWXXXr    c                   [        U 5      nUS:X  a  U R                  $ U R                  R                  nU(       d  U R                  $  [	        U5      nUR                  S5      nU[        U5      :  a  U R                  $ SR                  S USU  5       5      nU(       d  U R                  $ U R                  R                  5       nXe:X  a  gUR                  U S35      (       a  U[        U5      S-   S $ UR                  U5      (       a  U[        U5      S R                  5       $ U R                  $ ! [
        [        4 a    U R                  s $ f = f)zFStrip synthetic carried-over header text from continuation chunk text.r   rz   rq   c              3  z   #    U  H1  nS  UR                  SS5       5         H  nU(       d  M  Uv   M     M3     g7f)c              3  |   #    U  H2  nS R                  UR                  5       R                  5       5      v   M4     g7fr   r   r   s     r   rQ   <_strip_carried_over_header_text.<locals>.<genexpr>.<genexpr>4  s0     \G[tSXXd//1779::G[r   r   r   N)r   )rO   r   r|   s      r   rQ   2_strip_carried_over_header_text.<locals>.<genexpr>1  s;      #+C\sxxPTVZG[\D 	\ 	+s   (;;N    )r   r|   r_   rx   r   r
   r=   r   r   r}   lstrip
startswith)r.   r   rx   r   r   carried_header_textru   s          r   rt   rt     sL   5e<Azz>>..Lzz$\2 <<EFD3t9$zz(( #**+#  zz""$J( 34A677#12Q6899011#1245<<>>::/ $ zzs   D5 5EE)r2   Callable[_P, list[Element]]r"   r   )r   r!   r+   r   r0   r   r"   r#   )rH   r   rA   r   r"   None)r   r!   r"   zlist[Table])r   list[TableChunk]r"   r   )r.   r   r"   int)r   r   r"   ztuple[int, list[Any]])rx   z
str | Noner"   zlist[Any] | None)r   	list[Any]r   r   r"   bool)r   r   r"   rY   )r.   r   r"   r   )1r(   
__future__r   r~   dataclassesdcr7   rS   r   typingr   r   r   r   r	   
lxml.etreer
   r   	lxml.htmlr   typing_extensionsr   unstructured.chunking.basicr   unstructured.chunking.titler   unstructured.documents.elementsr   r   r   unstructured.utilsr   r   r   r9   r.   rI   	dataclassrG   r;   rZ   ro   rh   r   r   r   r   r   rt   r   r    r   <module>r      s    #     % > > , ) ' 6 6 F F >t_h 6r=4
 TI I I" .)^,. * "J%/P6
G	Y
#r    