Ë
    kQêj!  ã                  ó\   — d dl mZ d dlZddlmZ ddlmZmZ ddlm	Z
 d
d„Z G d	„ d«      Zy)é    )ÚannotationsNé   )Ú__version__)ÚMissingColumnErrorÚMissingValueError)ÚpandasÚContextualizerc                ó   — t        | «      S )a"  Create a Contextualizer object for the given DataFrame.

    Used to create context windows. Context windows are rolling subsets of text
    data.

    The input text column should already be separated into rows that will be the
    unit of the window. So to create a context window over tokens, start with
    a DataFrame with one token per row. To create a context window over sentences,
    start with a DataFrame with one sentence per row.

    Examples
    --------
    >>> from lancedb.context import contextualize
    >>> import pandas as pd
    >>> data = pd.DataFrame({
    ...    'token': ['The', 'quick', 'brown', 'fox', 'jumped', 'over',
    ...              'the', 'lazy', 'dog', 'I', 'love', 'sandwiches'],
    ...    'document_id': [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2]
    ... })

    ``window`` determines how many rows to include in each window. In our case
    this how many tokens, but depending on the input data, it could be sentences,
    paragraphs, messages, etc.

    >>> contextualize(data).window(3).stride(1).text_col('token').to_pandas()
                    token  document_id
    0     The quick brown            1
    1     quick brown fox            1
    2    brown fox jumped            1
    3     fox jumped over            1
    4     jumped over the            1
    5       over the lazy            1
    6        the lazy dog            1
    7          lazy dog I            1
    8          dog I love            1
    9   I love sandwiches            2
    10    love sandwiches            2
    >>> (contextualize(data).window(7).stride(1).min_window_size(7)
    ...   .text_col('token').to_pandas())
                                      token  document_id
    0   The quick brown fox jumped over the            1
    1  quick brown fox jumped over the lazy            1
    2    brown fox jumped over the lazy dog            1
    3        fox jumped over the lazy dog I            1
    4       jumped over the lazy dog I love            1
    5   over the lazy dog I love sandwiches            1

    ``stride`` determines how many rows to skip between each window start. This can
    be used to reduce the total number of windows generated.

    >>> contextualize(data).window(4).stride(2).text_col('token').to_pandas()
                        token  document_id
    0     The quick brown fox            1
    2   brown fox jumped over            1
    4    jumped over the lazy            1
    6          the lazy dog I            1
    8   dog I love sandwiches            1
    10        love sandwiches            2

    ``groupby`` determines how to group the rows. For example, we would like to have
    context windows that don't cross document boundaries. In this case, we can
    pass ``document_id`` as the group by.

    >>> (contextualize(data)
    ...     .window(4).stride(2).text_col('token').groupby('document_id')
    ...     .to_pandas())
                       token  document_id
    0    The quick brown fox            1
    2  brown fox jumped over            1
    4   jumped over the lazy            1
    6           the lazy dog            1
    9      I love sandwiches            2

    ``min_window_size`` determines the minimum size of the context windows
    that are generated.This can be used to trim the last few context windows
    which have size less than ``min_window_size``.
    By default context windows of size 1 are skipped.

    >>> (contextualize(data)
    ...     .window(6).stride(3).text_col('token').groupby('document_id')
    ...     .to_pandas())
                                 token  document_id
    0  The quick brown fox jumped over            1
    3     fox jumped over the lazy dog            1
    6                     the lazy dog            1
    9                I love sandwiches            2

    >>> (contextualize(data)
    ...     .window(6).stride(3).min_window_size(4).text_col('token')
    ...     .groupby('document_id')
    ...     .to_pandas())
                                 token  document_id
    0  The quick brown fox jumped over            1
    3     fox jumped over the lazy dog            1

    )r	   )Úraw_dfs    úT/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/context.pyÚcontextualizer      s   € ôB ˜&Ó!Ð!ó    c                  ó€   — e Zd ZdZd„ Zdd„Zdd„Zdd„Zdd„Zdd„Z	 e
j                  dd	ed
¬«      dd„«       Zdd„Zy)r	   zXCreate context windows from a DataFrame.
    See [lancedb.context.contextualize][].
    c                óX   — d | _         d | _        d | _        d | _        d| _        || _        y )Né   )Ú	_text_colÚ_groupbyÚ_strideÚ_windowÚ_min_window_sizeÚ_raw_df)Úselfr   s     r   Ú__init__zContextualizer.__init__w   s-   € ØˆŒØˆŒØˆŒØˆŒØ !ˆÔØˆ�r   c                ó   — || _         | S )z¤Set the window size. i.e., how many rows to include in each window.

        Parameters
        ----------
        window: int
            The window size.
        )r   )r   Úwindows     r   r   zContextualizer.window   ó   € ð ˆŒØˆr   c                ó   — || _         | S )zœSet the stride. i.e., how many rows to skip between each window.

        Parameters
        ----------
        stride: int
            The stride.
        )r   )r   Ústrides     r   r   zContextualizer.strideŠ   r   r   c                ó   — || _         | S )z¼Set the groupby column. i.e., how to group the rows.
        Windows don't cross groups

        Parameters
        ----------
        groupby: str
            The groupby column.
        )r   )r   Úgroupbys     r   r    zContextualizer.groupby•   s   € ð  ˆŒØˆr   c                ó   — || _         | S )z—Set the text column used to make the context window.

        Parameters
        ----------
        text_col: str
            The text column.
        )r   )r   Útext_cols     r   r"   zContextualizer.text_col¡   s   € ð "ˆŒØˆr   c                ó   — || _         | S )z­Set the (optional) min_window_size size for the context window.

        Parameters
        ----------
        min_window_size: int
            The min_window_size.
        )r   )r   Úmin_window_sizes     r   r$   zContextualizer.min_window_size¬   s   € ð !0ˆÔØˆr   z0.3.1z0.4.0zUse to_pandas() instead)Údeprecated_inÚ
removed_inÚcurrent_versionÚdetailsc                ó"   — | j                  «       S )N)Ú	to_pandas)r   s    r   Úto_dfzContextualizer.to_df·   s   € ð �~‰~ÓÐr   c           	     ó<  ‡ — t         €t        d«      ‚‰ j                  ‰ j                  j                  j                  «       vrt        ‰ j                  «      ‚‰ j                  �‰ j                  dk  rt        d«      ‚‰ j                  �‰ j                  dk  rt        d«      ‚ˆ fd„}‰ j                  € |‰ j                  «      S t        j                  ‰ j                  j                  ‰ j                  «      D ��cg c]  \  }} ||«      ‘Œ c}}«      S c c}}w )z2Create the context windows and return a DataFrame.z:pandas is required to create context windows using lancedbr   znThe value of window is None or less than 1. Specify the window size (number of rows to include in each window)zkThe value of stride is None or less than 1. Specify the stride (number of rows to skip between each window)c                óF  •— | ‰j                      j                  }| j                  d d ‰j                  …d d …f   j	                  «       }t        dt        | «      ‰j                  «      D �cg c]l  }|‰j                  z   t        | «      k  st        | «      |z
  ‰j                  k\  r4dj                  ||t        |‰j                  z   t        | «      «       «      ‘Œn }}t        |«      t        |«      k  r|j                  d t        |«       }||‰j                   <   |S c c}w )Nr   ú )r   ÚvaluesÚilocr   ÚcopyÚrangeÚlenr   r   ÚjoinÚmin)ÚgrpÚtextÚcontextsÚstart_iÚwindowsr   s        €r   Úprocess_groupz/Contextualizer.to_pandas.<locals>.process_groupÖ   sû   ø€ ð �t—~‘~Ñ&×-Ñ-ˆDØ—x‘x¡ 4§<¡< ²Ð 2Ñ3×8Ñ8Ó:ˆHô  % Q¬¨C«°$·,±,Ó?öàØ˜TŸ\™\Ñ)¬S°«XÒ5Ü�s“8˜gÑ%¨×)>Ñ)>Ò>ð —‘˜˜g¬¨G°d·l±lÑ,BÄCÈÃHÓ(MÐNÕOðˆGð ô �7‹|œc (›mÒ+Ø#Ÿ=™=¨¬3¨w«<Ð8�Ø'.ˆH�T—^‘^Ñ$ØˆOùòs   Á+A1D)ÚpdÚImportErrorr   r   ÚcolumnsÚtolistr   r   r   r   r   Úconcatr    )r   r;   Ú_r6   s   `   r   r*   zContextualizer.to_pandasÀ   sø   ø€ äˆ:ÜØLóð ð �>‰> §¡×!5Ñ!5×!<Ñ!<Ó!>Ñ>Ü$ T§^¡^Ó4Ð4à�<‰<Ð 4§<¡<°!Ò#3Ü#ðIóð ð
 �<‰<Ð 4§<¡<°!Ò#3Ü#ðFóð ô
	ð" �=‰=Ð Ù  §¡Ó.Ð.ä�y‰yØ.2¯l©l×.BÑ.BÀ4Ç=Á=Ó.Q×R¡F A s‰]˜3ÕÓRó
ð 	
ùÛRs   Ã>D
N)r   ÚintÚreturnr	   )r   rB   rC   r	   )r    ÚstrrC   r	   )r"   rD   rC   r	   )r$   rB   rC   r	   )rC   ú'pd.DataFrame')Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r"   r$   ÚdeprecationÚ
deprecatedr   r+   r*   © r   r   r	   r	   r   sW   „ ñòó	ó	ó
ó	ó	ð €[×ÑØØØ#Ø)ô	ò óð ô,
r   )r   rE   rC   r	   )Ú
__future__r   rJ   Ú r   Ú
exceptionsr   r   Údependenciesr   r<   r   r	   rL   r   r   ú<module>rQ      s)   ðõ
 #ã å ß =Ý &óa"÷Hz
ò z
r   