cognitivess
commited on
Commit
•
00808cf
1
Parent(s):
ad38e72
Update cognitivess_model/tokenization_cognitivess.py
Browse files
cognitivess_model/tokenization_cognitivess.py
CHANGED
@@ -1,4 +1,48 @@
|
|
1 |
-
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# cognitivess_model/tokenization_cognitivess.py
|
2 |
|
3 |
+
from transformers import PreTrainedTokenizer
|
4 |
+
import json
|
5 |
+
|
6 |
+
class CognitivessTokenizer(PreTrainedTokenizer):
|
7 |
+
def __init__(self, vocab_file, merges_file=None, **kwargs):
|
8 |
+
super().__init__(**kwargs)
|
9 |
+
self.vocab_file = vocab_file
|
10 |
+
self.merges_file = merges_file
|
11 |
+
self.load_vocab()
|
12 |
+
|
13 |
+
def load_vocab(self):
|
14 |
+
# Load vocabulary
|
15 |
+
with open(self.vocab_file, 'r') as f:
|
16 |
+
self.vocab = {line.strip(): idx for idx, line in enumerate(f)}
|
17 |
+
|
18 |
+
# Load merges file if exists
|
19 |
+
self.merges = []
|
20 |
+
if self.merges_file:
|
21 |
+
with open(self.merges_file, 'r') as f:
|
22 |
+
self.merges = [line.strip() for line in f]
|
23 |
+
|
24 |
+
def _tokenize(self, text):
|
25 |
+
# Tokenization logic (basic example)
|
26 |
+
tokens = text.split() # Simple whitespace-based tokenization
|
27 |
+
return tokens
|
28 |
+
|
29 |
+
def convert_tokens_to_ids(self, tokens):
|
30 |
+
return [self.vocab.get(token, self.vocab.get('[UNK]')) for token in tokens]
|
31 |
+
|
32 |
+
def convert_ids_to_tokens(self, ids):
|
33 |
+
reverse_vocab = {idx: token for token, idx in self.vocab.items()}
|
34 |
+
return [reverse_vocab.get(idx, '[UNK]') for idx in ids]
|
35 |
+
|
36 |
+
def save_vocabulary(self, save_directory):
|
37 |
+
vocab_path = f"{save_directory}/vocab.txt"
|
38 |
+
with open(vocab_path, 'w') as f:
|
39 |
+
for token in self.vocab:
|
40 |
+
f.write(f"{token}\n")
|
41 |
+
|
42 |
+
if self.merges_file:
|
43 |
+
merges_path = f"{save_directory}/merges.txt"
|
44 |
+
with open(merges_path, 'w') as f:
|
45 |
+
for merge in self.merges:
|
46 |
+
f.write(f"{merge}\n")
|
47 |
+
return vocab_path, merges_path
|
48 |
+
return vocab_path,
|