
    fTh";                         S 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rSSKJrJr  SSKJrJr  SSKJr  \R*                  " \5      rS	S
0r\" SS9 " S S\5      5       rS/rg)zTokenization classes.    N)copyfile)AnyDictListOptionalTuple   )
AddedTokenPreTrainedTokenizer)SPIECE_UNDERLINElogging)requires
vocab_filezspiece.model)sentencepiece)backendsc                     ^  \ rS rSrSr\rSSSSSSSS	S
SSS/S4S\\\	\
4      SS4U 4S jjjr\S 5       rS rS rS rS rS\	S\\	   4S jrS rS rS r S'S\\   S\\\      S\\   4S jjr S(S\\   S\\\      S\S\\   4U 4S  jjjr S'S\\   S\\\      S\\   4S! jjrS'S"\	S#\\	   S\\	   4S$ jjrU 4S% jrS&rU =r $ ))CpmTokenizer"   zMRuns pre-tokenization with Jieba segmentation tool. It is used in CPM models.FTz<s>z</s>z<unk>z<sep>z<pad>z<cls>z<mask>z<eop>z<eod>Nsp_model_kwargsreturnc                    > [        U[        5      (       a  [        USSS9OUnUc  0 OUU l        X l        X0l        X@l        Xl        [        R                  " S0 U R                  D6U l
        U R                  R                  U5         SSKnXl        [        R                  SS5      U l        ["        TU ]H  " SUUUUUUUU	U
UUU R                  S	.UD6  S
U l        g! [         a  nUR                  S5      eSnAff = f)a%  
Construct a CPM tokenizer. Based on [Jieba](https://pypi.org/project/jieba/) and
[SentencePiece](https://github.com/google/sentencepiece).

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`):
        [SentencePiece](https://github.com/google/sentencepiece) file (generally has a .spm extension) that
        contains the vocabulary necessary to instantiate a tokenizer.
    do_lower_case (`bool`, *optional*, defaults to `True`):
        Whether to lowercase the input when tokenizing.
    remove_space (`bool`, *optional*, defaults to `True`):
        Whether to strip the text when tokenizing (removing excess spaces before and after the string).
    keep_accents (`bool`, *optional*, defaults to `False`):
        Whether to keep accents when tokenizing.
    bos_token (`str`, *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>

    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.
    sep_token (`str`, *optional*, defaults to `"<sep>"`):
        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.
    pad_token (`str`, *optional*, defaults to `"<pad>"`):
        The token used for padding, for example when batching sequences of different lengths.
    cls_token (`str`, *optional*, defaults to `"<cls>"`):
        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.
    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.
    additional_special_tokens (`List[str]`, *optional*, defaults to `["<eop>", "<eod>"]`):
        Additional special tokens used by the tokenizer.

Attributes:
    sp_model (`SentencePieceProcessor`):
        The *SentencePiece* processor that is used for every conversion (string, tokens and IDs).
TF)lstriprstripNr   zxYou need to install jieba to use CpmTokenizer or CpmTokenizerFast. See https://pypi.org/project/jieba/ for installation.z 
u   ▂▃)do_lower_caseremove_spacekeep_accents	bos_token	eos_token	unk_token	sep_token	pad_token	cls_token
mask_tokenadditional_special_tokensr   r	    )
isinstancestrr
   r   r   r   r   r   spmSentencePieceProcessorsp_modelLoadjiebaModuleNotFoundError	__class__	maketrans
translatorsuper__init___pad_token_type_id)selfr   r   r   r   r   r   r   r    r!   r"   r#   r$   r   kwargsr,   errorr.   s                    `/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/cpm/tokenization_cpm.pyr2   CpmTokenizer.__init__(   s   ^ KUU_adJeJeZ
4Fku
%4%<r/*(($22JT5I5IJ:&	 
--~> 	
'%%!&? 00	
 	
  #$1 # 	//H 	s   	C 
C='C88C=c                 ,    [        U R                  5      $ N)lenr*   )r4   s    r7   
vocab_sizeCpmTokenizer.vocab_size   s     4==!!    c                     [        U R                  5       Vs0 s H  oR                  U5      U_M     nnUR                  U R                  5        U$ s  snf r:   )ranger<   convert_ids_to_tokensupdateadded_tokens_encoder)r4   ivocabs      r7   	get_vocabCpmTokenizer.get_vocab   sL    ;@;QR;Qa++A.1;QRT../ Ss   Ac                 D    U R                   R                  5       nS US'   U$ )Nr*   )__dict__copy)r4   states     r7   __getstate__CpmTokenizer.__getstate__   s#    ""$ jr>   c                     Xl         [        U S5      (       d  0 U l        [        R                  " S0 U R                  D6U l        U R
                  R                  U R                  5        g )Nr   r%   )rI   hasattrr   r(   r)   r*   r+   r   )r4   ds     r7   __setstate__CpmTokenizer.__setstate__   sP     t.//#%D 22JT5I5IJ4??+r>   c                    U R                   (       a.  SR                  UR                  5       R                  5       5      nOUnUR	                  SS5      R	                  SS5      nU R
                  (       dV  [        R                  " SU5      nSR                  U Vs/ s H"  n[        R                  " U5      (       a  M   UPM$     sn5      nU R                  (       a  UR                  5       nU$ s  snf )N z``"z''NFKD )r   joinstripsplitreplacer   unicodedata	normalize	combiningr   lower)r4   inputsoutputscs       r7   preprocess_textCpmTokenizer.preprocess_text   s    hhv||~3356GG//$,44T3?  !++FG<Ggg'R'Q9N9Nq9Qq'RSGmmoG	 Ss   C.>C.textc                 @   U R                  U5      nU R                  R                  U[        S9n/ nU H  n[	        U5      S:  a  US   [        S5      :X  a  US   R                  5       (       a  U R                  R                  USS R                  [        S5      5      nUS   [        :w  a3  US   S   [        :X  a#  [	        US   5      S:X  a  USS nOUS   SS US'   UR                  US   5        UR                  U5        M  UR                  U5        M     U$ )	zTokenize a string.)out_type   ,NrW   r   )rc   r*   encoder'   r;   isdigitEncodeAsPiecesr[   r   appendextend)r4   re   pieces
new_piecespiece
cur_piecess         r7   	_tokenizeCpmTokenizer._tokenize   s   ##D)%%dS%9
E5zA~%)s3x"7E"I<M<M<O<O!]]99%*:L:LM]_a:bc
8//JqM!4DHX4X:a=)Q.%/^
(21ab(9
1!!%),!!*-!!%(  r>   c                 8    U R                   R                  U5      $ )z0Converts a token (str) in an id using the vocab.)r*   	PieceToId)r4   tokens     r7   _convert_token_to_id!CpmTokenizer._convert_token_to_id       }}&&u--r>   c                 8    U R                   R                  U5      $ )z=Converts an index (integer) in a token (str) using the vocab.)r*   	IdToPiece)r4   indexs     r7   _convert_id_to_token!CpmTokenizer._convert_id_to_token   r|   r>   c                 l    SR                  U5      R                  [        S5      R                  5       nU$ )zIConverts a sequence of tokens (strings for sub-words) in a single string.rW   rT   )rX   r[   r   rY   )r4   tokens
out_strings      r7   convert_tokens_to_string%CpmTokenizer.convert_tokens_to_string   s,    WWV_,,-=sCIIK
r>   token_ids_0token_ids_1c                 d    U R                   /nU R                  /nUc  X-   U-   $ X-   U-   U-   U-   $ )a8  
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. An XLNet sequence has the following format:

- single sequence: `X <sep> <cls>`
- pair of sequences: `A <sep> B <sep> <cls>`

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.
)sep_token_idcls_token_id)r4   r   r   sepclss        r7    build_inputs_with_special_tokens-CpmTokenizer.build_inputs_with_special_tokens   sL    &   !  !$s** ;.4s::r>   already_has_special_tokensc                    > U(       a  [         TU ]  XSS9$ Ub(  S/[        U5      -  S/-   S/[        U5      -  -   SS/-   $ S/[        U5      -  SS/-   $ )ad  
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   r   r   rh   )r1   get_special_tokens_maskr;   )r4   r   r   r   r.   s       r7   r   $CpmTokenizer.get_special_tokens_mask  s}    & &72']a 3   "C#k**qc1aS3{;K5KLPQSTvUUc+&&1a&00r>   c                     U R                   /nS/nUc  [        X-   5      S/-  U-   $ [        X-   5      S/-  [        X#-   5      S/-  -   U-   $ )a[  
Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet
sequence pair mask has the following format:

```
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence    | second sequence |
```

If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

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 [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
   r   rh   )r   r;   )r4   r   r   r   cls_segment_ids        r7   $create_token_type_ids_from_sequences1CpmTokenizer.create_token_type_ids_from_sequences(  sg    .   !{()QC/.@@;$%+c+2C.Ds.JJ^[[r>   save_directoryfilename_prefixc                    [         R                  R                  U5      (       d  [        R	                  SU S35        g [         R                  R                  X(       a  US-   OS[        S   -   5      n[         R                  R                  U R                  5      [         R                  R                  U5      :w  aG  [         R                  R                  U R                  5      (       a  [        U R                  U5        U4$ [         R                  R                  U R                  5      (       dC  [        US5       nU R                  R                  5       nUR                  U5        S S S 5        U4$ U4$ ! , (       d  f       U4$ = f)NzVocabulary path (z) should be a directory-rW   r   wb)ospathisdirloggerr6   rX   VOCAB_FILES_NAMESabspathr   isfiler   openr*   serialized_model_protowrite)r4   r   r   out_vocab_fileficontent_spiece_models         r7   save_vocabularyCpmTokenizer.save_vocabularyG  s,   ww}}^,,LL,^,<<STUo_s22QbcoQpp
 77??4??+rww~/NNSUSZSZSaSabfbqbqSrSrT__n5    00nd+r'+}}'K'K'M$-. ,     	 ,+   s   ?,E99
F	c                    > [         TU ]  " U0 UD6nUR                  SS5      R                  SS5      R                  SS5      nU$ )NrT   rW   u   ▂u   ▃
)r1   _decoder[   )r4   argsr5   re   r.   s       r7   r   CpmTokenizer._decodeX  sE    w//||C$,,Xs;CCHdSr>   )
rI   r3   r   r,   r   r   r*   r   r0   r   r:   )NF)!__name__
__module____qualname____firstlineno____doc__r   vocab_files_namesr   r   r'   r   r2   propertyr<   rF   rL   rQ   rc   r   ru   rz   r   r   intr   boolr   r   r   r   r   __static_attributes____classcell__)r.   s   @r7   r   r   "   s   W)
 #*G"448u$ "$sCx.1u$  
!u$ u$n " ", c d3i *.
.
 JN;9;3;DI3F;	c;6 sx1913;DI3F1ko1	c1 1< JN\9\3;DI3F\	c\>!c !HSM !]bcf]g !" r>   r   )r   r   r\   shutilr   typingr   r   r   r   r   r   r(   tokenization_utilsr
   r   utilsr   r   utils.import_utilsr   
get_loggerr   r   r   r   __all__r%   r>   r7   <module>r      sr     	   3 3  A . * 
		H	%!>2  
%&x& x 'xv	 
r>   