o
    `+ i                     @  sJ   d dl mZ d dlZd dlZd dlmZmZ d dlmZ G dd dZ	dS )    )annotationsN)AnyOptional)Documentc                      s   e Zd ZU dZdZded< 	 dZded< 	 	d1d2 fddZed3ddZ	ed4ddZ
d5ddZ		d6d7dd Z	!d8d9d%d&Z	!	'd:d;d)d*Z	!	'	d<d=d/d0Z  ZS )>RecursiveJsonSplittera  Splits JSON data into smaller, structured chunks while preserving hierarchy.

    This class provides methods to split JSON data into smaller dictionaries or
    JSON-formatted strings based on configurable maximum and minimum chunk sizes.
    It supports nested JSON structures, optionally converts lists into dictionaries
    for better chunking, and allows the creation of document objects for further use.
      intmax_chunk_sizei  min_chunk_sizeNOptional[int]returnNonec                   s6   t    || _|dur|| _dS t|d d| _dS )a  Initialize the chunk size configuration for text processing.

        This constructor sets up the maximum and minimum chunk sizes, ensuring that
        the ``min_chunk_size`` defaults to a value slightly smaller than the
        ``max_chunk_size`` if not explicitly provided.

        Args:
            max_chunk_size: The maximum size for a chunk. Defaults to 2000.
            min_chunk_size: The minimum size for a chunk. If None,
                defaults to the maximum chunk size minus 200, with a lower bound of 50.
        N   2   )super__init__r	   maxr
   )selfr	   r
   	__class__ i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain_text_splitters/json.pyr      s   
zRecursiveJsonSplitter.__init__datadict[str, Any]c                 C  s   t t| S )z1Calculate the size of the serialized JSON object.)lenjsondumps)r   r   r   r   
_json_size/   s   z RecursiveJsonSplitter._json_sizedpath	list[str]valuer   c                 C  s.   |dd D ]}|  |i } q|| |d < dS )z;Set a value in a nested dictionary based on the given path.N)
setdefault)r   r   r!   keyr   r   r   _set_nested_dict4   s   z&RecursiveJsonSplitter._set_nested_dictc                   sD   t |tr fdd| D S t |tr  fddt|D S |S )Nc                   s   i | ]
\}}|  |qS r   )_list_to_dict_preprocessing).0kvr   r   r   
<dictcomp>>   s    zERecursiveJsonSplitter._list_to_dict_preprocessing.<locals>.<dictcomp>c                   s    i | ]\}}t | |qS r   )strr&   )r'   iitemr*   r   r   r+   A   s    )
isinstancedictitemslist	enumerate)r   r   r   r*   r   r&   ;   s   


z1RecursiveJsonSplitter._list_to_dict_preprocessingcurrent_pathOptional[list[str]]chunksOptional[list[dict[str, Any]]]list[dict[str, Any]]c           
      C  s   |pg }|dur
|ni g}t |trU| D ]<\}}g ||}| |d }| ||i}| j| }	||	k rA| |d || q|| jkrK|i  | ||| q|S | |d || |S )zESplit json into maximum size dictionaries while preserving structure.Nr"   )	r/   r0   r1   r   r	   r%   r
   append_json_split)
r   r   r4   r6   r$   r!   new_path
chunk_sizesize	remainingr   r   r   r:   H   s    



z!RecursiveJsonSplitter._json_splitF	json_dataconvert_listsboolc                 C  s4   |r|  | |}n|  |}|d s|  |S )z'Splits JSON into a list of JSON chunks.r"   )r:   r&   pop)r   r?   r@   r6   r   r   r   
split_jsong   s   
z RecursiveJsonSplitter.split_jsonTensure_asciic                   s    | j ||d} fdd|D S )z2Splits JSON into a list of JSON formatted strings.)r?   r@   c                   s   g | ]	}t j| d qS )rD   )r   r   )r'   chunkrE   r   r   
<listcomp>   s    z4RecursiveJsonSplitter.split_text.<locals>.<listcomp>)rC   )r   r?   r@   rD   r6   r   rE   r   
split_textw   s   z RecursiveJsonSplitter.split_texttexts	metadatasOptional[list[dict[Any, Any]]]list[Document]c                 C  sf   |pi gt | }g }t|D ]!\}}| j|||dD ]}	t|| }
t|	|
d}|| qq|S )z4Create documents from a list of json objects (Dict).)r?   r@   rD   )Zpage_contentmetadata)r   r3   rH   copydeepcopyr   r9   )r   rI   r@   rD   rJ   Z
_metadatasZ	documentsr-   textrF   rM   Znew_docr   r   r   create_documents   s   
z&RecursiveJsonSplitter.create_documents)r   N)r	   r   r
   r   r   r   )r   r   r   r   )r   r   r   r    r!   r   r   r   )r   r   r   r   )NN)r   r   r4   r5   r6   r7   r   r8   )F)r?   r   r@   rA   r   r8   )FT)r?   r   r@   rA   rD   rA   r   r    )FTN)
rI   r8   r@   rA   rD   rA   rJ   rK   r   rL   )__name__
__module____qualname____doc__r	   __annotations__r
   r   staticmethodr   r%   r&   r:   rC   rH   rQ   __classcell__r   r   r   r   r   
   s2   
 
"r   )

__future__r   rN   r   typingr   r   Zlangchain_core.documentsr   r   r   r   r   r   <module>   s    