leafspark commited on
Commit
b997ebb
·
verified ·
1 Parent(s): a52e760

remove old tokenizer

Browse files
Files changed (1) hide show
  1. claude_tokenizer.py +0 -112
claude_tokenizer.py DELETED
@@ -1,112 +0,0 @@
1
- import re
2
- import json
3
- from typing import List, Dict
4
-
5
- class ClaudeTokenizer:
6
- def __init__(self, config_file: str, algorithm: str = "trie"):
7
- with open(config_file, "r") as f:
8
- config = json.load(f)
9
-
10
- self.vocab = config["vocab"]
11
- self.vocab_size = config["n_vocab_size"]
12
- self.pat_str = config["pat_str"]
13
- self.special_tokens = config["special_tokens"]
14
-
15
- self.token_to_id = {token: i for i, token in enumerate(self.vocab)}
16
- self.id_to_token = {i: token for token, i in self.token_to_id.items()}
17
-
18
- for token, id in self.special_tokens.items():
19
- self.token_to_id[token] = id
20
- self.id_to_token[id] = token
21
-
22
- self.pat = re.compile(self.pat_str)
23
- self.vocab_trie = self._build_trie(self.vocab)
24
-
25
- self.algorithm = algorithm
26
- if algorithm not in ["trie", "linear"]:
27
- raise ValueError("Invalid algorithm. Choose 'trie' or 'linear'.")
28
-
29
- def _build_trie(self, vocab: List[str]) -> Dict:
30
- trie = {}
31
- for token in vocab:
32
- current = trie
33
- for char in token:
34
- if isinstance(current, str):
35
- break
36
- if char not in current:
37
- current[char] = {}
38
- current = current[char]
39
- if isinstance(current, dict):
40
- current["*"] = token
41
- return trie
42
-
43
- def tokenize(self, text: str) -> List[str]:
44
- if self.algorithm == "trie":
45
- tokens = []
46
- for part in self.pat.findall(text):
47
- tokens.extend(self._tokenize_part_trie(part))
48
- return tokens
49
- else:
50
- return self._tokenize_part_linear(text)
51
-
52
- def encode(self, text: str) -> List[int]:
53
- tokens = self.tokenize(text)
54
- return [
55
- self.token_to_id.get(token, self.special_tokens["<META>"])
56
- for token in tokens
57
- ]
58
-
59
- def decode(self, ids: List[int]) -> str:
60
- return "".join(self.id_to_token.get(id, "") for id in ids)
61
-
62
- def _tokenize_part_trie(self, text: str) -> List[str]:
63
- tokens = []
64
- while text:
65
- current = self.vocab_trie
66
- longest_match = ""
67
- for i, char in enumerate(text):
68
- if char not in current:
69
- break
70
- current = current[char]
71
- if "*" in current:
72
- longest_match = current["*"]
73
- if longest_match:
74
- tokens.append(longest_match)
75
- text = text[len(longest_match):]
76
- else:
77
- tokens.append(text[0])
78
- text = text[1:]
79
- return tokens
80
-
81
- def _tokenize_part_linear(self, text: str) -> List[str]:
82
- tokens = []
83
- while text:
84
- longest_match = ""
85
- for token in self.vocab:
86
- if text.startswith(token) and len(token) > len(longest_match):
87
- longest_match = token
88
- if longest_match:
89
- tokens.append(longest_match)
90
- text = text[len(longest_match):]
91
- else:
92
- tokens.append(text[0])
93
- text = text[1:]
94
- return tokens
95
-
96
-
97
- # Usage example
98
- if __name__ == "__main__":
99
- # Choose the algorithm: "trie" or "linear"
100
- algorithm = "linear" # or "trie"
101
-
102
- tokenizer = ClaudeTokenizer("tokenizer_config.json", algorithm=algorithm)
103
-
104
- test_text = """Hello! It's nice to meet you. How can I assist you today? I'm here to help with any questions you might have or tasks you need help with."""
105
- tokens = tokenizer.tokenize(test_text)
106
- print(f"Tokens ({algorithm}):", tokens)
107
-
108
- encoded = tokenizer.encode(test_text)
109
- print(f"Encoded ({algorithm}):", encoded)
110
-
111
- decoded = tokenizer.decode(encoded)
112
- print(f"Decoded ({algorithm}):", decoded)