Views
No views yet
[UNK] (Unknown Token): ID 0[CLS] (Classification Token / Start of Sequence): ID 1[SEP] (Separator Token / End of Sequence): ID 2[MASK] (Mask Token): ID 3[PAD] (Padding Token): ID 50000 (Note: This ID might be different if your tokenizer assigned it differently; please verify from your training output.)PAD token: '[PAD]' (ID: XXXXX)) and update it here if necessary.transformers library:1from transformers import AutoTokenizer
2
3tokenizer_id = "LocalDoc/az-en-unigram-tokenizer-50k"
4
5try:
6 tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
7 print(f"Tokenizer loaded successfully from {tokenizer_id}!")
8except Exception as e:
9 print(f"Failed to load tokenizer. Make sure 'sentencepiece_model_pb2.py' is available or you have 'protobuf' installed if needed by the tokenizer loading mechanism.")
10 print(f"Error: {e}")
11 # As a fallback or for certain environments, you might need to ensure protobuf and sentencepiece_model_pb2.py are handled
12 # For example, if the user's environment is minimal:
13 # !pip install protobuf
14 # !wget https://raw.githubusercontent.com/google/sentencepiece/master/python/src/sentencepiece/sentencepiece_model_pb2.py
15 # tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
16
17
18# Example Azerbaijani text
19az_text = "Bu, Azərbaycan dilində bir test cümləsidir."
20encoded_az = tokenizer.encode(az_text)
21tokens_az = tokenizer.convert_ids_to_tokens(encoded_az)
22print(f"Azerbaijani Original: {az_text}")
23print(f"Azerbaijani Encoded IDs: {encoded_az}")
24print(f"Azerbaijani Tokens: {tokens_az}")
25
26# Example English text
27en_text = "This is a test sentence in English."
28encoded_en = tokenizer.encode(en_text)
29tokens_en = tokenizer.convert_ids_to_tokens(encoded_en)
30print(f"\nEnglish Original: {en_text}")
31print(f"English Encoded IDs: {encoded_en}")
32print(f"English Tokens: {tokens_en}")
33
34# Example with special tokens
35special_text = "[CLS] Bu bir cümlədir. [SEP] This is a sentence. [MASK]"
36encoded_special = tokenizer.encode(special_text)
37tokens_special = tokenizer.convert_ids_to_tokens(encoded_special)
38print(f"\nSpecial Text Original: {special_text}")
39print(f"Special Text Encoded IDs: {encoded_special}")
40print(f"Special Text Tokens: {tokens_special}")1Tokenizer loaded successfully from LocalDoc/az-en-unigram-tokenizer-50k!
2Azerbaijani Original: Bu, Azərbaycan dilində bir test cümləsidir.
3Azerbaijani Encoded IDs: [90, 4, 66, 2940, 30, 2248, 34485, 116, 5]
4Azerbaijani Tokens: ['▁Bu', ',', '▁Azərbaycan', '▁dilində', '▁bir', '▁test', '▁cümləsi', 'dir', '.']
5
6English Original: This is a test sentence in English.
7English Encoded IDs: [283, 18, 14, 2248, 3841, 10, 2784, 5]
8English Tokens: ['▁This', '▁is', '▁a', '▁test', '▁sentence', '▁in', '▁English', '.']
9
10Special Text Original: [CLS] Bu bir cümlədir. [SEP] This is a sentence. [MASK]
11Special Text Encoded IDs: [1, 90, 30, 10798, 116, 5, 15, 2, 283, 18, 14, 3841, 5, 15, 3]
12Special Text Tokens: ['[CLS]', '▁Bu', '▁bir', '▁cümlə', 'dir', '.', '▁', '[SEP]', '▁This', '▁is', '▁a', '▁sentence', '.', '▁', '[MASK]']unigram500000.9995| Token | Description |
|---|---|
[UNK] | Unknown token |
[PAD] | Padding token |
[CLS] | Beginning of sentence |
[SEP] | End of sentence |
[MASK] | Mask token (user-defined) |
1unk_piece: [UNK]
2pad_piece: [PAD]
3bos_piece: [CLS]
4eos_piece: [SEP]
5user_defined_symbols: [MASK]