Ë
    ÜÓ^j3  ã                   óŒ   — d Z ddlZddlZddlmZ ddlmZ ddlmZ  ej                  e
«      Zddd	œZd
„ Z G d„ de«      ZdgZy)z Tokenization classes for PhoBERTé    N)Úcopyfileé   )ÚPreTrainedTokenizer)Úloggingz	vocab.txtz	bpe.codes)Ú
vocab_fileÚmerges_filec                 óx   — t        «       }| d   }| dd D ]  }|j                  ||f«       |}Œ t        |«      }|S )z…
    Return set of symbol pairs in a word.

    Word is represented as tuple of symbols (symbols being variable-length strings).
    r   é   N)ÚsetÚadd)ÚwordÚpairsÚ	prev_charÚchars       ú{/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/phobert/tokenization_phobert.pyÚ	get_pairsr   !   sO   € ô ‹E€EØ�Q‘€IØ�Q�R�ò ˆØ�	‰	�9˜dÐ#Ô$Ø‰	ðô �‹J€EØ€Ló    c            
       ó*  ‡ — e Zd ZdZeZ	 	 	 	 	 	 	 dˆ fd„	Z	 ddee   dee   dz  dee   fd„Z		 ddee   dee   dz  de
dee   fˆ fd	„Z	 ddee   dee   dz  dee   fd
„Zed„ «       Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zddededz  dee   fd„Zd„ Zˆ xZS )ÚPhobertTokenizeraO	  
    Construct a PhoBERT tokenizer. Based on Byte-Pair-Encoding.

    This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
    this superclass for more information regarding those methods.

    Args:
        vocab_file (`str`):
            Path to the vocabulary file.
        merges_file (`str`):
            Path to the merges file.
        bos_token (`st`, *optional*, defaults to `"<s>"`):
            The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the beginning of
            sequence. The token used is the `cls_token`.

            </Tip>

        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the end of sequence.
            The token used is the `sep_token`.

            </Tip>

        sep_token (`str`, *optional*, defaults to `"</s>"`):
            The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
            sequence classification or for a text and a question for question answering. It is also used as the last
            token of a sequence built with special tokens.
        cls_token (`str`, *optional*, defaults to `"<s>"`):
            The classifier token which is used when doing sequence classification (classification of the whole sequence
            instead of per-token classification). It is the first token of the sequence when built with special tokens.
        unk_token (`str`, *optional*, defaults to `"<unk>"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
        mask_token (`str`, *optional*, defaults to `"<mask>"`):
            The token used for masking values. This is the token used when training this model with masked language
            modeling. This is the token which the model will try to predict.
    c
                 óü  •— || _         || _        i | _        d| j                  t        |«      <   d| j                  t        |«      <   d| j                  t        |«      <   d| j                  t        |«      <   | j	                  |«       | j                  j                  «       D ��ci c]  \  }}||“Œ
 c}}| _        t        |d¬«      5 }|j                  «       j                  d«      d d }d d d «       D �cg c]  }t        |j                  «       d d «      ‘Œ  }}t        t        |t        t        |«      «      «      «      | _        i | _        t#        ‰| �H  d
|||||||	d	œ|
¤Ž y c c}}w # 1 sw Y   ŒƒxY wc c}w )Nr   r
   é   r   úutf-8©Úencodingú
éÿÿÿÿ)Ú	bos_tokenÚ	eos_tokenÚ	unk_tokenÚ	sep_tokenÚ	cls_tokenÚ	pad_tokenÚ
mask_token© )r   r   ÚencoderÚstrÚadd_from_fileÚitemsÚdecoderÚopenÚreadÚsplitÚtupleÚdictÚzipÚrangeÚlenÚ	bpe_ranksÚcacheÚsuperÚ__init__)Úselfr   r   r   r   r    r!   r   r"   r#   ÚkwargsÚkÚvÚmerges_handleÚmergesÚmergeÚ	__class__s                   €r   r5   zPhobertTokenizer.__init__d   sY  ø€ ð %ˆŒØ&ˆÔàˆŒØ'(ˆ�‰”S˜“^Ñ$Ø'(ˆ�‰”S˜“^Ñ$Ø'(ˆ�‰”S˜“^Ñ$Ø'(ˆ�‰”S˜“^Ñ$à×Ñ˜:Ô&à)-¯©×);Ñ);Ó)=×>¡  A˜˜1™Ó>ˆŒä�+¨Ô0ð 	;°MØ"×'Ñ'Ó)×/Ñ/°Ó5°c°rÐ:ˆF÷	;à9?Ö@°”%˜Ÿ™› c rÐ*Õ+Ð@ˆÐ@äœc &¬%´°F³Ó*<Ó=Ó>ˆŒØˆŒ
ä‰Ñð 		
ØØØØØØØ!ñ		
ð ó		
ùó ?÷	;ð 	;üâ@s   Â%E'Ã#E-Ã7#E9Å-E6NÚtoken_ids_0Útoken_ids_1Úreturnc                 ó˜   — |€| j                   g|z   | j                  gz   S | j                   g}| j                  g}||z   |z   |z   |z   |z   S )a–  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A PhoBERT sequence has the following format:

        - single sequence: `<s> X </s>`
        - pair of sequences: `<s> A </s></s> B </s>`

        Args:
            token_ids_0 (`list[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`list[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        )Úcls_token_idÚsep_token_id)r6   r>   r?   ÚclsÚseps        r   Ú build_inputs_with_special_tokensz1PhobertTokenizer.build_inputs_with_special_tokens�   sg   € ð( ÐØ×%Ñ%Ð&¨Ñ4¸×8IÑ8IÐ7JÑJÐJØ× Ñ Ð!ˆØ× Ñ Ð!ˆØ�[Ñ  3Ñ&¨Ñ,¨{Ñ:¸SÑ@Ð@r   Úalready_has_special_tokensc                 ó¶   •— |rt         ‰| �  ||d¬«      S |€dgdgt        |«      z  z   dgz   S dgdgt        |«      z  z   ddgz   dgt        |«      z  z   dgz   S )aÄ  
        Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
        special tokens using the tokenizer `prepare_for_model` method.

        Args:
            token_ids_0 (`list[int]`):
                List of IDs.
            token_ids_1 (`list[int]`, *optional*):
                Optional second list of IDs for sequence pairs.
            already_has_special_tokens (`bool`, *optional*, defaults to `False`):
                Whether or not the token list is already formatted with special tokens for the model.

        Returns:
            `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
        T)r>   r?   rG   r
   r   )r4   Úget_special_tokens_maskr1   )r6   r>   r?   rG   r=   s       €r   rI   z(PhobertTokenizer.get_special_tokens_maskª   s‹   ø€ ñ& &Ü‘7Ñ2Ø'°[Ð]að 3ó ð ð ÐØ�3˜1˜#¤ KÓ 0Ñ0Ñ1°Q°CÑ7Ð7Øˆs�q�cœC Ó,Ñ,Ñ-°°A°Ñ6¸1¸#ÄÀKÓ@PÑ:PÑQÐUVÐTWÑWÐWr   c                 ó    — | j                   g}| j                  g}|€t        ||z   |z   «      dgz  S t        ||z   |z   |z   |z   |z   «      dgz  S )aÌ  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. PhoBERT does not
        make use of token type ids, therefore a list of zeros is returned.

        Args:
            token_ids_0 (`list[int]`):
                List of IDs.
            token_ids_1 (`list[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `list[int]`: List of zeros.
        r   )rC   rB   r1   )r6   r>   r?   rE   rD   s        r   Ú$create_token_type_ids_from_sequencesz5PhobertTokenizer.create_token_type_ids_from_sequencesÆ   sm   € ð" × Ñ Ð!ˆØ× Ñ Ð!ˆàÐÜ�s˜[Ñ(¨3Ñ.Ó/°1°#Ñ5Ð5Ü�3˜Ñ$ sÑ*¨SÑ0°;Ñ>ÀÑDÓEÈÈÑKÐKr   c                 ó,   — t        | j                  «      S ©N)r1   r%   ©r6   s    r   Ú
vocab_sizezPhobertTokenizer.vocab_sizeÞ   s   € ä�4—<‘<Ó Ð r   c                 óB   — t        | j                  fi | j                  ¤ŽS rM   )r.   r%   Úadded_tokens_encoderrN   s    r   Ú	get_vocabzPhobertTokenizer.get_vocabâ   s   € Ü�D—L‘LÑ> D×$=Ñ$=Ñ>Ð>r   c                 ó$  ‡ — |‰ j                   v r‰ j                   |   S t        |«      }t        t        |d d «      |d   dz   gz   «      }t        |«      }|s|S 	 t	        |ˆ fd„¬«      }|‰ j
                  vrnÎ|\  }}g }d}|t        |«      k  r�	 |j                  ||«      }	|j                  |||	 «       |	}||   |k(  r6|t        |«      dz
  k  r%||dz      |k(  r|j                  ||z   «       |dz  }n|j                  ||   «       |dz  }|t        |«      k  rŒ�t        |«      }|}t        |«      dk(  rnt        |«      }Œídj                  |«      }|d d	 }|‰ j                   |<   |S # t        $ r |j                  ||d  «       Y Œnw xY w)
Nr   z</w>c                 óN   •— ‰j                   j                  | t        d«      «      S )NÚinf)r2   ÚgetÚfloat)Úpairr6   s    €r   ú<lambda>z&PhobertTokenizer.bpe.<locals>.<lambda>ð   s   ø€ °·±×1CÑ1CÀDÌ%ÐPUË,Ó1W€ r   )Úkeyr   r
   r   ú@@ éüÿÿÿ)r3   r-   Úlistr   Úminr2   r1   ÚindexÚextendÚ
ValueErrorÚappendÚjoin)
r6   Útokenr   r   ÚbigramÚfirstÚsecondÚnew_wordÚiÚjs
   `         r   ÚbpezPhobertTokenizer.bpeå   s±  ø€ Ø�D—J‘JÑØ—:‘:˜eÑ$Ð$Ü�U‹|ˆÜ”T˜$˜s ˜)“_¨¨R©°6Ñ(9Ð':Ñ:Ó;ˆÜ˜$“ˆáØˆLàÜ˜Ó$WÔXˆFØ˜TŸ^™^Ñ+ØØ"‰MˆE�6ØˆHØˆAØ”c˜$“i’-ðØŸ
™
 5¨!Ó,�Að
 —O‘O D¨¨1 IÔ.Ø�Aà˜‘7˜eÒ#¨¬C°«I¸©MÒ(9¸dÀ1ÀqÁ5¹kÈVÒ>SØ—O‘O E¨F¡NÔ3Ø˜‘F‘Aà—O‘O D¨¡GÔ,Ø˜‘F�Að ”c˜$“i“-ô  ˜X“ˆHØˆDÜ�4‹y˜AŠ~Øä! $›�ð9 ð: �z‰z˜$ÓˆØ�C�RˆyˆØ ˆ�
‰
�5ÑØˆøô- "ò Ø—O‘O D¨¨ HÔ-Ùðús   ÂE/ Å/FÆFc                 ó´   — g }t        j                  d|«      }|D ]:  }|j                  t        | j	                  |«      j                  d«      «      «       Œ< |S )zTokenize a string.z\S+\n?ú )ÚreÚfindallr`   r]   rk   r,   )r6   ÚtextÚsplit_tokensÚwordsrd   s        r   Ú	_tokenizezPhobertTokenizer._tokenize  sT   € àˆä—
‘
˜9 dÓ+ˆàò 	BˆEØ×Ñ¤ T§X¡X¨e£_×%:Ñ%:¸3Ó%?Ó @ÕAð	BàÐr   c                 ó€   — | j                   j                  || j                   j                  | j                  «      «      S )z0Converts a token (str) in an id using the vocab.)r%   rV   r   )r6   rd   s     r   Ú_convert_token_to_idz%PhobertTokenizer._convert_token_to_id  s,   € à�|‰|×Ñ  t§|¡|×'7Ñ'7¸¿¹Ó'GÓHÐHr   c                 óN   — | j                   j                  || j                  «      S )z=Converts an index (integer) in a token (str) using the vocab.)r)   rV   r   )r6   r_   s     r   Ú_convert_id_to_tokenz%PhobertTokenizer._convert_id_to_token  s   € à�|‰|×Ñ  t§~¡~Ó6Ð6r   c                 ód   — dj                  |«      j                  dd«      j                  «       }|S )z:Converts a sequence of tokens (string) in a single string.rm   r[   Ú )rc   ÚreplaceÚstrip)r6   ÚtokensÚ
out_strings      r   Úconvert_tokens_to_stringz)PhobertTokenizer.convert_tokens_to_string#  s,   € à—X‘X˜fÓ%×-Ñ-¨e°RÓ8×>Ñ>Ó@ˆ
ØÐr   Úsave_directoryÚfilename_prefixc                 óú  — t         j                  j                  |«      st        j	                  d|› d�«       y t         j                  j                  ||r|dz   ndt        d   z   «      }t         j                  j                  ||r|dz   ndt        d   z   «      }t         j                  j                  | j                  «      t         j                  j                  |«      k7  r@t         j                  j                  | j                  «      rt        | j                  |«       nit         j                  j                  | j                  «      s@t        |d«      5 }| j                  j                  «       }|j                  |«       d d d «       t         j                  j                  | j                  «      t         j                  j                  |«      k7  rt        | j                  |«       ||fS # 1 sw Y   ŒlxY w)NzVocabulary path (z) should be a directoryú-ry   r   r   Úwb)ÚosÚpathÚisdirÚloggerÚerrorrc   ÚVOCAB_FILES_NAMESÚabspathr   Úisfiler   r*   Úsp_modelÚserialized_model_protoÚwriter   )r6   r   r€   Úout_vocab_fileÚout_merge_fileÚfiÚcontent_spiece_models          r   Úsave_vocabularyz PhobertTokenizer.save_vocabulary(  sr  € Ü�w‰w�}‰}˜^Ô,Ü�L‰LÐ,¨^Ð,<Ð<SÐTÔUØÜŸ™Ÿ™Ø±o˜_¨sÒ2È2ÔQbÐcoÑQpÑpó
ˆô Ÿ™Ÿ™Ø±o˜_¨sÒ2È2ÔQbÐcpÑQqÑqó
ˆô �7‰7�?‰?˜4Ÿ?™?Ó+¬r¯w©w¯©¸~Ó/NÒNÔSU×SZÑSZ×SaÑSaÐbf×bqÑbqÔSrÜ�T—_‘_ nÕ5Ü—‘—‘ §¡Ô0Ü�n dÓ+ð /¨rØ'+§}¡}×'KÑ'KÓ'MÐ$Ø—‘Ð-Ô.÷/ô �7‰7�?‰?˜4×+Ñ+Ó,´·±·±ÀÓ0OÒOÜ�T×%Ñ% ~Ô6à˜~Ð-Ð-÷/ð /ús   Å,G1Ç1G:c                 óÂ  — t        |t        «      r*	 t        |dd¬«      5 }| j                  |«       ddd«       y|j                  «       }|D ]Z  }|j                  «       }|j                  d«      }|dk(  rt        d	«      ‚|d| }t        | j                  «      | j                  |<   Œ\ y# 1 sw Y   yxY w# t        $ r}|‚d}~wt
        $ r t        d|› d�«      ‚w xY w)
zi
        Loads a pre-existing dictionary from a text file and adds its symbols to this instance.
        Úrr   r   NzIncorrect encoding detected in z, please rebuild the datasetrm   r   z5Incorrect dictionary format, expected '<token> <cnt>')Ú
isinstancer&   r*   r'   ÚFileNotFoundErrorÚUnicodeErrorÚ	ExceptionÚ	readlinesr{   Úrfindra   r1   r%   )	r6   ÚfÚfdÚfnfeÚlinesÚlineTmpÚlineÚidxr   s	            r   r'   zPhobertTokenizer.add_from_fileE  sç   € ô �aœÔðcÜ˜!˜S¨7Ô3ð +°rØ×&Ñ& rÔ*÷+ð à—‘“ˆØò 	3ˆGØ—=‘=“?ˆDØ—*‘*˜S“/ˆCØ�bŠyÜ Ð!XÓYÐYØ˜˜�:ˆDÜ!$ T§\¡\Ó!2ˆD�L‰L˜Òñ	3÷+ð ûô	 %ò Ø�
ûÜò cÜÐ"AÀ!ÀÐD`Ð aÓbÐbðcús3   ’B7  B+²B7 Â+B4Â0B7 Â4B7 Â7	CÃ CÃC)ú<s>ú</s>r¤   r£   z<unk>z<pad>z<mask>rM   )NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r‰   Úvocab_files_namesr5   r]   ÚintrF   ÚboolrI   rK   ÚpropertyrO   rR   rk   rs   ru   rw   r~   r&   r-   r“   r'   Ú__classcell__)r=   s   @r   r   r   1   s@  ø„ ñ.ð` *Ðð ØØØØØØõ*
ðZ GKñAØ ™9ðAØ37¸±9¸tÑ3CðAà	ˆc‰óAð6 puñXØ ™9ðXØ37¸±9¸tÑ3CðXØhlðXà	ˆc‰õXð: GKñLØ ™9ðLØ37¸±9¸tÑ3CðLà	ˆc‰óLð0 ñ!ó ð!ò?ò*òXòIò7òñ
.¨cð .ÀCÈ$ÁJð .ÐZ_Ð`cÑZdó .ö:3r   r   )r¨   r„   rn   Úshutilr   Útokenization_pythonr   Úutilsr   Ú
get_loggerr¥   r‡   r‰   r   r   Ú__all__r$   r   r   ú<module>r³      s\   ðñ 'ã 	Û 	Ý å 6Ý ð 
ˆ×	Ñ	˜HÓ	%€ð ØñÐ òô i3Ð*ô i3ðX	 Ð
�r   