Ë
    Q^(h 9  ã                   ó¾  — d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZmZ ej                   j#                  e«      Zej                   j'                  ed«      Z ee«      5 Zej/                  «       Zddd«        G d„ d«      Ze	e   Ze	ee      Z G d„ d	e
«      Z G d
„ d«      Z G d„ d«      Z G d„ d«      Z efde	e!   de!fd„Z"y# 1 sw Y   ŒWxY w)é    N)ÚTemplate)ÚAnyÚCallableÚDictÚListÚ
NamedTupleÚOptionalÚTuple)ÚEncodingÚ	Tokenizerzvisualizer-styles.cssc                   ó@   — e Zd ZU eed<   eed<   eed<   dededefd„Zy)Ú
AnnotationÚstartÚendÚlabelc                 ó.   — || _         || _        || _        y ©N)r   r   r   )Úselfr   r   r   s       úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/tokenizers/tools/visualizer.pyÚ__init__zAnnotation.__init__   s   € ØˆŒ
ØˆŒØˆ�
ó    N)Ú__name__Ú
__module__Ú__qualname__ÚintÚ__annotations__Ústrr   © r   r   r   r      s+   … ØƒJØ	ƒHØƒJð˜cð ¨ð °Cô r   r   c                   ó.   — e Zd ZU ee   ed<   ee   ed<   y)ÚCharStateKeyÚtoken_ixÚanno_ixN)r   r   r   r	   r   r   r   r   r   r    r       s   … Ø�s‰mÓØ�c‰]Ôr   r    c                   óP   — e Zd ZU ee   ed<   d„ Zed„ «       Zed„ «       Z	de
fd„Zy)Ú	CharStateÚchar_ixc                 ó.   — || _         d | _        g | _        y r   )r%   r"   Útokens)r   r%   s     r   r   zCharState.__init__'   s   € ØˆŒà&*ˆŒØ!#ˆ�r   c                 óT   — t        | j                  «      dkD  r| j                  d   S d S )Nr   ©Úlenr'   ©r   s    r   r!   zCharState.token_ix-   s%   € ä!$ T§[¡[Ó!1°AÒ!5ˆt�{‰{˜1‰~Ð?¸4Ð?r   c                 ó2   — t        | j                  «      dkD  S )zJ
        BPE tokenizers can output more than one token for a char
        é   r)   r+   s    r   Úis_multitokenzCharState.is_multitoken1   s   € ô
 �4—;‘;Ó !Ñ#Ð#r   Úreturnc                 óD   — t        | j                  | j                  ¬«      S )N)r!   r"   )r    r!   r"   r+   s    r   Úpartition_keyzCharState.partition_key8   s   € ÜØ—]‘]Ø—L‘Lô
ð 	
r   N)r   r   r   r	   r   r   r   Úpropertyr!   r.   r    r1   r   r   r   r$   r$   $   sG   … Ø�c‰]Óò$ð ñ@ó ð@ð ñ$ó ð$ð
˜|ô 
r   r$   c                   ó   — e Zd Zy)ÚAlignedN)r   r   r   r   r   r   r4   r4   ?   s   „ Ør   r4   c            
       óR  — e Zd ZdZ ej
                  dej                  ¬«      Z	 	 ddede	de
eegef      fd„Zg dfd	ed
ede
e	   de
e   fd„Zed
edeeef   fd„«       Zedee   d	edefd„«       Zed	eded
edefd„«       Zed	ed
edefd„«       Zed	eded
edee   fd„«       Zy)ÚEncodingVisualizera³  
    Build an EncodingVisualizer

    Args:

         tokenizer (:class:`~tokenizers.Tokenizer`):
            A tokenizer instance

         default_to_notebook (:obj:`bool`):
            Whether to render html output in a notebook by default

         annotation_converter (:obj:`Callable`, `optional`):
            An optional (lambda) function that takes an annotation in any format and returns
            an Annotation object
    z(.{1})?(unk|oov)(.{1})?)ÚflagsNÚ	tokenizerÚdefault_to_notebookÚannotation_converterc                 ót   — |r		 ddl m}m} || _        || _        || _        y # t        $ r t	        d«      ‚w xY w)Nr   ©ÚHTMLÚdisplayzÙWe couldn't import IPython utils for html display.
                        Are you running in a notebook?
                        You can also pass `default_to_notebook=False` to get back raw HTML
                    )ÚIPython.core.displayr=   r>   ÚImportErrorÚ	Exceptionr8   r9   Úannotation_coverter)r   r8   r9   r:   r=   r>   s         r   r   zEncodingVisualizer.__init__V   sN   € ñ ðß>ð #ˆŒØ#6ˆÔ Ø#7ˆÔ Øøô ò Üðóð ðús   „" ¢7ÚtextÚannotationsr/   c                 óH  — | j                   }|�|}|r		 ddlm}m} | j                  �t        t        | j                  |«      «      }| j                  j                  |«      }t        j                  |||«      }|r  |«      «       y|S # t        $ r t        d«      ‚w xY w)aß  
        Build a visualization of the given text

        Args:
            text (:obj:`str`):
                The text to tokenize

            annotations (:obj:`List[Annotation]`, `optional`):
                An optional list of annotations of the text. The can either be an annotation class
                or anything else if you instantiated the visualizer with a converter function

            default_to_notebook (:obj:`bool`, `optional`, defaults to `False`):
                If True, will render the html in a notebook. Otherwise returns an html string.

        Returns:
            The HTML string if default_to_notebook is False, otherwise (default) returns None and
            renders the HTML in the notebook

        Nr   r<   zeWe couldn't import IPython utils for html display.
                    Are you running in a notebook?)r9   r?   r=   r>   r@   rA   rB   ÚlistÚmapr8   Úencoder6   Ú_EncodingVisualizer__make_html)	r   rC   rD   r9   Úfinal_default_to_notebookr=   r>   ÚencodingÚhtmls	            r   Ú__call__zEncodingVisualizer.__call__l   s«   € ð2 %)×$<Ñ$<Ð!ØÐ*Ø(;Ð%Ù$ðß>ð ×#Ñ#Ð/Üœs 4×#;Ñ#;¸[ÓIÓJˆKØ—>‘>×(Ñ(¨Ó.ˆÜ!×-Ñ-¨d°H¸kÓJˆÙ$Ù‘D˜“JÕàˆKøô ò Üð6óð ðús   ”B ÂB!c                 óê   — t        | «      dk(  ri S t        t        d„ | «      «      }t        |«      }t        d|z  «      }|dk  rd}d}d}d}i }t	        |«      D ]  }d|› d	|› d
|› d�||<   ||z  }Œ |S )a  
        Generates a color palette for all the labels in a given set of annotations

        Args:
          annotations (:obj:`Annotation`):
            A list of annotations

        Returns:
            :obj:`dict`: A dictionary mapping labels to colors in HSL format
        r   c                 ó   — | j                   S r   )r   )Úxs    r   ú<lambda>z;EncodingVisualizer.calculate_label_colors.<locals>.<lambda>§   s
   €  1§7¡7€ r   éÿ   é   é    é@   é
   zhsl(ú,z%,ú%)r*   ÚsetrG   r   Úsorted)	rD   ÚlabelsÚ
num_labelsÚh_stepÚsÚlÚhÚcolorsr   s	            r   Úcalculate_label_colorsz)EncodingVisualizer.calculate_label_colors™   s¡   € ô ˆ{Ó˜qÒ ØˆIÜ”SÑ*¨KÓ8Ó9ˆÜ˜“[ˆ
Ü�S˜:Ñ%Ó&ˆØ�BŠ;ØˆFØˆØˆØˆØˆä˜F“^ò 	ˆEØ" 1 # Q q c¨¨A¨3¨aÐ0ˆF�5‰MØ�‰K‰Að	ð ˆr   Úconsecutive_chars_listrK   c                 ó  — | d   }|j                   €|j                  |j                     }d|› d�S | d   }|j                   }|j                   dz   }||| }g }	i }
|j                  �Ä|	j                  d«       |j                  r|	j                  d«       |j                  dz  r|	j                  d	«       n|	j                  d
«       t
        j                  j                  |j                  |j                     «      �?|	j                  d«       |j                  |j                     |
d<   n|	j                  d«       ddj                  |	«      › d�}d}|
j                  «       D ]  \  }}|d|› d|› d�z  }Œ d|› d|› d|› d�S )aã  
        Converts a list of "consecutive chars" into a single HTML element.
        Chars are consecutive if they fall under the same word, token and annotation.
        The CharState class is a named tuple with a "partition_key" method that makes it easy to
        compare if two chars are consecutive.

        Args:
            consecutive_chars_list (:obj:`List[CharState]`):
                A list of CharStates that have been grouped together

            text (:obj:`str`):
                The original text being processed

            encoding (:class:`~tokenizers.Encoding`):
                The encoding returned from the tokenizer

        Returns:
            :obj:`str`: The HTML span for a set of consecutive chars
        r   z(<span class="special-token" data-stoken=z></span>éÿÿÿÿr-   Útokenzmulti-tokené   z	odd-tokenz
even-tokenzspecial-tokenÚstokz	non-tokenzclass="ú ú"Ú z data-z="z<span z >ú</span>)
r%   r'   r!   Úappendr.   r6   Úunk_token_regexÚsearchÚjoinÚitems)rc   rC   rK   ÚfirstÚstokenÚlastr   r   Ú	span_textÚcss_classesÚ
data_itemsÚcssÚdataÚkeyÚvals                  r   Úconsecutive_chars_to_htmlz,EncodingVisualizer.consecutive_chars_to_html¶   s’  € ð2 ' qÑ)ˆØ�=‰=Ð à—_‘_ U§^¡^Ñ4ˆFð >¸f¸XÀXÐNÐNà% bÑ)ˆØ—‘ˆØ�l‰l˜QÑˆØ˜˜s�Oˆ	ØˆØˆ
Ø�>‰>Ð%à×Ñ˜wÔ'Ø×"Ò"Ø×"Ñ" =Ô1Ø�~‰~ Ò!ð
 ×"Ñ" ;Õ/ð ×"Ñ" <Ô0Ü!×1Ñ1×8Ñ8¸¿¹ÈÏÉÑ9XÓYÐeà×"Ñ" ?Ô3à%-§_¡_°U·^±^Ñ%D�
˜6Ò"ð ×Ñ˜{Ô+Ø˜#Ÿ(™( ;Ó/Ð0°Ð4ˆØˆØ"×(Ñ(Ó*ò 	+‰HˆC�Ø�f˜S˜E  C 5¨Ð*Ñ*‰Dð	+à˜�u˜A˜d˜V 2 i [°Ð8Ð8r   c                 óB  — t         j                  | ||«      }|d   g}|d   j                  }g }t         j                  |«      }|d   j                  }|�.||   }	|	j                  }
||
   }|j                  d|› d|
› d�«       |dd  D ]â  }|j                  }||k7  rm|j                  t         j                  || |¬«      «       |g}|�|j                  d«       |�.||   }	|	j                  }
||
   }|j                  d|› d|
› d�«       |}|j                  «       |d   j                  «       k(  r|j                  |«       Œ¹|j                  t         j                  || |¬«      «       |g}Œä |j                  t         j                  || |¬«      «       t        |«      }|S )Nr   z&<span class="annotation" style="color:z" data-label="z">r-   )rC   rK   rl   )	r6   Ú%_EncodingVisualizer__make_char_statesr"   rb   r   rm   r|   r1   ÚHTMLBody)rC   rK   rD   Úchar_statesÚcurrent_consecutive_charsÚprev_anno_ixÚspansÚlabel_colors_dictÚcur_anno_ixÚannor   ÚcolorÚcsÚress                 r   Ú__make_htmlzEncodingVisualizer.__make_htmlú   së  € ä(×;Ñ;¸DÀ(ÈKÓXˆØ%0°¡^Ð$4Ð!Ø" 1‘~×-Ñ-ˆØˆÜ.×EÑEÀkÓRÐØ! !‘n×,Ñ,ˆØÐ"à˜{Ñ+ˆDØ—J‘JˆEØ% eÑ,ˆEØ�L‰LÐAÀ%ÀÈÐW\ÐV]Ð]_Ð`Ôaà˜a˜b�/ò &	1ˆBØŸ*™*ˆKØ˜lÒ*à—‘ä&×@Ñ@Ø1Ø!Ø!)ð Aó ôð .0¨DÐ)àÐ+à—L‘L Ô+ØÐ*à& {Ñ3�DØ ŸJ™J�EØ-¨eÑ4�EØ—L‘LÐ#IÈ%ÈÐP^Ð_dÐ^eÐegÐ!hÔiØ&ˆLà×ÑÓ!Ð%>¸qÑ%A×%OÑ%OÓ%QÒQà)×0Ñ0°Õ4ð —‘Ü&×@Ñ@Ø1Ø!Ø!)ð Aó ôð .0¨DÑ)ðM&	1ðR 	�‰Ü×8Ñ8Ø)ØØ!ð 9ó ô	
ô �u‹oˆØˆ
r   c                 óž   — dgt        | «      z  }t        |«      D ]/  \  }}t        |j                  |j                  «      D ]  }|||<   Œ	 Œ1 |S )aÓ  
        Args:
            text (:obj:`str`):
                The raw text we want to align to

            annotations (:obj:`AnnotationList`):
                A (possibly empty) list of annotations

        Returns:
            A list of  length len(text) whose entry at index i is None if there is no annotation on
            character i or k, the index of the annotation that covers index i where k is with
            respect to the list of annotations
        N)r*   Ú	enumerateÚranger   r   )rC   rD   Úannotation_mapr"   ÚaÚis         r   Ú__make_anno_mapz"EncodingVisualizer.__make_anno_map<  s\   € ð ˜¤# d£)Ñ+ˆÜ# KÓ0ò 	,‰JˆG�QÜ˜1Ÿ7™7 A§E¡EÓ*ò ,�Ø$+�˜qÒ!ñ,ð	,ð Ðr   c                 ó”  — t         j                  | |«      }t        t        | «      «      D �cg c]  }t	        |«      ‘Œ }}t        |j                  «      D ]M  \  }}|j                  |«      }|€Œ|\  }	}
t        |	|
«      D ]   }||   j                  j                  |«       Œ" ŒO t        |«      D ]  \  }}|||   _	        Œ |S c c}w )aÂ  
        For each character in the original text, we emit a tuple representing it's "state":

            * which token_ix it corresponds to
            * which word_ix it corresponds to
            * which annotation_ix it corresponds to

        Args:
            text (:obj:`str`):
                The raw text we want to align to

            annotations (:obj:`List[Annotation]`):
                A (possibly empty) list of annotations

            encoding: (:class:`~tokenizers.Encoding`):
                The encoding returned from the tokenizer

        Returns:
            :obj:`List[CharState]`: A list of CharStates, indicating for each char in the text what
            it's state is
        )
r6   Ú"_EncodingVisualizer__make_anno_mapr�   r*   r$   rŒ   r'   Útoken_to_charsrm   r"   )rC   rK   rD   rŽ   r%   r€   r!   rf   Úoffsetsr   r   r�   r"   s                r   Ú__make_char_statesz%EncodingVisualizer.__make_char_statesQ  sÙ   € ô. ,×;Ñ;¸DÀ+ÓNˆäJOÔPSÐTXÓPYÓJZÖ'[¸w¬	°'Õ(:Ð'[ˆÐ'[Ü(¨¯©Ó9ò 	;‰OˆH�eØ×-Ñ-¨hÓ7ˆGØÑ"Ø$‘
��sÜ˜u cÓ*ò ;�AØ ‘N×)Ñ)×0Ñ0°Õ:ñ;ð		;ô !*¨.Ó 9ò 	3ÑˆG�WØ+2ˆK˜Ñ Õ(ð	3ð Ðùò (\s   ­C)TN)r   r   r   Ú__doc__ÚreÚcompileÚ
IGNORECASErn   r   Úboolr	   r   r   r   r   r   ÚAnnotationListrM   Ústaticmethodr   rb   r   r$   r   r|   rI   ÚPartialIntListr“   r~   r   r   r   r6   r6   C   sŠ  „ ñð  !�b—j‘jÐ!>ÀbÇmÁmÔT€Oð
 %)ØFJñ	àðð "ðð ' x°°°zÐ0AÑ'BÑCó	ð2 ')Ø.2ñ	+àð+ð $ð+ð & d™^ð	+ð
 
�#‰ó+ðZ ð¨Nð ¸tÀCÈÀH¹~ò ó ðð8 ðA9Ø $ Y¡ðA9àðA9ð òA9ó ðA9ðF ð?˜#ð ?¨ð ?Àð ?ÐSVò ?ó ð?ðB ð˜cð °ð À>ò ó ðð( ð" ð "°ð "À~ð "ÐZ^Ð_hÑZiò "ó ñ"r   r6   Úchildrenr/   c                 ó6   — dj                  | «      }d|› d|› d�S )a[  
    Generates the full html with css from a list of html spans

    Args:
        children (:obj:`List[str]`):
            A list of strings, assumed to be html elements

        css_styles (:obj:`str`, `optional`):
            Optional alternative implementation of the css

    Returns:
        :obj:`str`: An HTML string with style markup
    rk   z?
    <html>
        <head>
            <style>
                zs
            </style>
        </head>
        <body>
            <div class="tokenized-text" dir=auto>
            z4
            </div>
        </body>
    </html>
    )rp   )rŸ   Ú
css_stylesÚchildren_texts      r   r   r   w  s9   € ð —G‘G˜HÓ%€Mðð �ð ð
 ˆOð ðð r   )#Ú	itertoolsÚosr˜   Ústringr   Útypingr   r   r   r   r   r	   r
   Ú
tokenizersr   r   ÚpathÚdirnameÚ__file__rp   Úcss_filenameÚopenÚfÚreadrx   r   rœ   r   rž   r    r$   r4   r6   r   r   r   r   r   ú<module>r¯      sÞ   ðÛ Û 	Û 	Ý ß I× IÑ Iç *ð �'‰'�/‰/˜(Ó
#€Ø�w‰w�|‰|˜GÐ%<Ó=€Ù	ˆ,Óð ˜1Ø
�&‰&‹(€C÷÷ñ ð �jÑ!€Ø�h˜s‘mÑ$€ô�:ô ÷

ñ 
÷6	ñ 	÷qñ qðh	 .1ñ �t˜C‘yð °Sô ÷Wð ús   Á,CÃC