
    Nj                    x    d dl mZ d dlZd dlZd dlmZ  ej                  e      Z	 	 	 	 	 	 	 	 ddZ	ddZ
d	dZy)
    )annotationsN)TokenizerModelc                <   t               }d}d}d}| j                  }|r=|D cg c]  }|j                  |      s| }	}| j                  |	      } t	        |	      }| j
                  }
t        |      }g }g }|D ]=  }|
j                  |      }t	        |      dk  rt        j                  d| d       |dz  }Bt	        |      dkD  rq|D cg c]  }d| d
 }}dj                  |      }t        j                  d| d| d       || j                  v r| j                  |      } |j                  |       |d   }||v rt        j                  d| d	       |dz  }|r1|j                  |      r t        j                  d| d
       |dz  }|j                  |       |j                  |       @ d| _        | j                         } | j!                  |d      } | j#                  |ddd      } t	        |      }t%        ||||       | S c c}w c c}w )au  Clean a vocabulary by removing duplicates and tokens that were already in the vocabulary.

    This function removes duplicate tokens and tokens that are already in the model's vocabulary.
    It also removes the tokenizer's post-processor, which we do not use anyway.

    :param model: The tokenizer model to clean.
    :param vocabulary_to_add: The vocabulary to add to the model. Any tokens in this vocabulary that
        are split according to the pretokenizer are added as multiword tokens.
    :param token_remove_regex: A regex pattern to remove tokens from the vocabulary.
    :return: The cleaned tokenizer model.
    r      zToken 'z ' was empty after preprocessing.',z7' was split into multiple tokens after preprocessing: [z#], adding it as a multi-word token.z ' was already in the vocabulary.z!' was removed due to regex match.NT)preprocess_tokensF)
is_specialsingle_word
normalized)setsorted_vocabularymatchremove_tokens_from_vocabularylenpreprocessor
preprocessloggerwarningjoin
vocabularyremove_token_from_vocabularyappendaddpost_processorprune_added_tokensadd_tokens_to_vocabularyadd_addedtokens_report_statistics)modelvocabulary_to_addtoken_remove_regexseen_tokensn_duplicaten_emptyn_regex_removedinternal_tokenstokentokens_to_remover   tokens_to_addadded_tokens_to_addpreprocessedsubwordtokens_as_str
split_inton_multiwords                     n/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/model2vec/tokenizer/tokenizer.pyclean_and_create_vocabularyr2      sX     %KKGO!&!8!8O/>beBTBZBZ[`BaEb334DE./%%Lo&K!M%'"#..u5|q NNWUG+KLMqLG|q ;GH<q	^<MH-0JNN% WXbWc  dG  H (((::5A&&u-QKNNWUG+KLM1K"4":":5"ANNWUG+LMNq OU#7 #:  E$$&E**=D*QE!!"5%UZgk!lE)*K{K'JL[ c Is   HHHc                    | rt         j                  d|  d       |rt         j                  d| d       |rt         j                  d| d       |rt         j                  d| d       yy)z:Report statistics on the various types of issues we found.zAdded z% multi-word tokens to the vocabulary.zRemoved z duplicate tokens.z tokens due to regex match.z empty tokens.N)r   info)r0   r$   r&   r%   s       r1   r   r   S   sk    f[M)NOPh{m+=>?h//JKLhwi~67     c                :   |j                   xs g |j                  xs g }}|j                  }|j                         }g }| D ]W  }|j	                  |      }||j                  g |||       .|j                  |j                  |      j                         Y |S )zConvert a list of Token objects to their corresponding token ID sequences.

    :param tokens: List of Token objects to convert
    :param model: The tokenizermodel of the tokenizer.
    :return: List of token IDs corresponding to the input tokens
    )bos_idseos_idsr   to_tokenizergetr   encodeids)	tokensr    prefixsuffixr   	tokenizer	token_idsr(   token_ids	            r1   turn_tokens_into_idsrC   _   s     ]](b%--*=2FF!!J""$I!#I>>%(9v9x9&9:Y--e4889  r5   )r    r   r!   	list[str]r"   zre.Pattern[str] | Nonereturnr   )
r0   intr$   rF   r&   rF   r%   rF   rE   None)r=   rD   r    r   rE   zlist[list[int]])
__future__r   loggingre
skeletokenr   	getLogger__name__r   r2   r   rC    r5   r1   <module>rO      s\    "  	 %			8	$EE E /E 	EP	8r5   