File size: 7,283 Bytes
e5b79b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
import torch.nn as nn
from typing import List, Dict, Optional
import nltk
from nltk.stem import WordNetLemmatizer, PorterStemmer
import os
import json
import re
from collections import Counter

# Quietly ensure dependencies are present
def ensure_nltk_data():
    for pkg in ['punkt', 'wordnet', 'omw-1.4', 'punkt_tab']:
        try:
            nltk.data.find(f'tokenizers/{pkg}' if 'punkt' in pkg else f'corpora/{pkg}')
        except LookupError:
            nltk.download(pkg, quiet=True)

ensure_nltk_data()

# Optional SpaCy import
try:
    import spacy
    SPACY_AVAILABLE = True
except ImportError:
    SPACY_AVAILABLE = False
    print("SpaCy not available. Install with: pip install spacy")

class AdvancedTokenizer:
    def __init__(self, vocab_size=5007, use_spacy_pos=False, use_stemming=False):
        self.vocab_size = vocab_size
        self.lemmatizer = WordNetLemmatizer()
        self.stemmer = PorterStemmer()
        self.use_spacy_pos = use_spacy_pos and SPACY_AVAILABLE
        self.use_stemming = use_stemming
        
        # Initialize SpaCy if requested
        if self.use_spacy_pos:
            try:
                self.nlp = spacy.load("en_core_web_sm")
                print("SpaCy POS Tagging: ENABLED")
            except OSError:
                print("SpaCy model not found. Disabling POS tagging.")
                self.use_spacy_pos = False
        
        self.word_to_id = {}
        self.id_to_word = {}
        # Special tokens
        self.unk_token = "<UNK>"
        self.pad_token = "<PAD>"
        self.sos_token = "<SOS>"
        self.eos_token = "<EOS>"
        self.instruction_token = "[INSTRUCTION]"
        self.response_token = "[RESPONSE]"
        self.system_token = "[SYSTEM]"
        self.user_token = "[USER]"
        self.thought_token = "[THOUGHT]"
        
        # Initialize with specials + Agentic CoT and Tool Calling Tokens
        self.specials = [
            self.pad_token, self.sos_token, self.eos_token, self.unk_token, 
            self.instruction_token, self.response_token,
            self.system_token, self.user_token, self.thought_token,
            "[TOOL_CALL]", "[TOOL_ARG]", "[TOOL_RESULT]"
        ]
        
        for i, token in enumerate(self.specials):
            self.word_to_id[token] = i
            self.id_to_word[i] = token
            
        self.is_trained = False


    def preprocess_text(self, text: str) -> str:
        """Applies basic grammar rules."""
        # 1. Fix multiple spaces
        text = re.sub(r'\s+', ' ', text)
        
        # 2. Ensure space after punctuation
        text = re.sub(r'([.,;?!])(?=[a-zA-Z])', r'\1 ', text)
        
        # 3. Capitalize first letter of sentences
        sentences = re.split(r'([.?!]\s*)', text)
        text = ''.join([s.capitalize() if i % 2 == 0 else s for i, s in enumerate(sentences)])
        
        return text.strip()

    def process_token(self, token: str, pos: Optional[str] = None) -> str:
        """Applies Lemmatization AND Stemming, optionally using POS tag."""
        # 1. Lemmatize (with POS if available)
        if pos and pos in ['NOUN', 'VERB', 'ADJ', 'ADV']:
            # Map SpaCy POS to WordNet POS
            pos_map = {'NOUN': 'n', 'VERB': 'v', 'ADJ': 'a', 'ADV': 'r'}
            lemma = self.lemmatizer.lemmatize(token, pos=pos_map.get(pos, 'n'))
        else:
            lemma = self.lemmatizer.lemmatize(token)
        
        # 2. Stem (Root form) - OPTIONAL
        if self.use_stemming:
            lemma = self.stemmer.stem(lemma)
            
        return lemma

    def train(self, text: str):
        """Builds vocabulary from text."""
        mode_str = "Grammar + Lemma + Stem"
        if self.use_spacy_pos:
            mode_str += " + POS"
        print(f"Training Tokenizer ({mode_str})...")
        
        pattern = '(' + '|'.join(map(re.escape, self.specials)) + ')'
        parts = re.split(pattern, text)
        
        processed_tokens = []
        for part in parts:
            if part in self.specials:
                continue # Skip specials for vocab building
            elif part.strip():
                part = self.preprocess_text(part)
                if self.use_spacy_pos:
                    doc = self.nlp(part.lower())
                    processed_tokens.extend([self.process_token(token.text, token.pos_) for token in doc])
                else:
                    tokens = nltk.word_tokenize(part.lower())
                    processed_tokens.extend([self.process_token(t) for t in tokens])
        
        # 3. Count frequencies
        counts = Counter(processed_tokens)
        
        # 4. Select top words
        num_specials = len(self.specials)
        most_common = counts.most_common(self.vocab_size - num_specials)
        
        for i, (word, _) in enumerate(most_common):
            idx = i + num_specials
            self.word_to_id[word] = idx
            self.id_to_word[idx] = word
            
        print(f"Tokenizer trained. Vocab size: {len(self.word_to_id)}")
        self.is_trained = True
        self.save("vocab.json")

    def encode(self, text: str) -> List[int]:
        if not self.is_trained:
            if os.path.exists("vocab.json"):
                self.load("vocab.json")
            else:
                self.train(text)

        ids = []
        pattern = '(' + '|'.join(map(re.escape, self.specials)) + ')'
        parts = re.split(pattern, text)
        
        for part in parts:
            if part in self.specials:
                ids.append(self.word_to_id[part])
            elif part.strip():
                part = self.preprocess_text(part)
                if self.use_spacy_pos:
                    doc = self.nlp(part.lower())
                    ids.extend([self.word_to_id.get(self.process_token(token.text, token.pos_), 
                                               self.word_to_id[self.unk_token]) for token in doc])
                else:
                    tokens = nltk.word_tokenize(part.lower())
                    ids.extend([self.word_to_id.get(self.process_token(t), 
                                               self.word_to_id[self.unk_token]) for t in tokens])
        return ids

    def decode(self, ids: List[int]) -> str:
        tokens = [self.id_to_word.get(i, self.unk_token) for i in ids]
        # Simple detokenization (join with spaces)
        # NLTK doesn't have a perfect detokenizer built-in, simple join is okay for now
        return " ".join(tokens).replace(" .", ".").replace(" ,", ",")

    def save(self, path):
        with open(path, 'w') as f:
            json.dump(self.word_to_id, f)
            
    def load(self, path):
        if not os.path.exists(path):
            return
        with open(path, 'r') as f:
            self.word_to_id = json.load(f)
        self.id_to_word = {v: k for k, v in self.word_to_id.items()}
        self.is_trained = True
        # print(f"Tokenizer loaded. Vocab size: {len(self.word_to_id)}")

# Singleton for easy access if needed, though class is better
tokenizer = AdvancedTokenizer()