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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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import matplotlib.pyplot as plt
import pandas as pd
import nltk
import re
from collections import Counter
from nltk.tokenize import word_tokenize, sent_tokenize
import spacy

from utils.model_loader import load_spacy, download_nltk_resources
from utils.helpers import fig_to_html, df_to_html_table

def tokenization_handler(text_input):
    """Show tokenization capabilities."""
    output_html = []
    
    # Add result area container
    output_html.append('<div class="result-area">')
    output_html.append('<h2 class="task-header">Tokenization</h2>')
    
    output_html.append("""

    <div class="alert alert-info">

    <i class="fas fa-info-circle"></i>

    Tokenization is the process of breaking text into smaller units called tokens, which can be words, characters, or subwords.

    </div>

    """)
    
    # Model info
    output_html.append("""

    <div class="alert alert-info">

        <h4><i class="fas fa-tools"></i> Tools Used:</h4>

        <ul>

            <li><b>NLTK</b> - Natural Language Toolkit for basic word and sentence tokenization</li>

            <li><b>spaCy</b> - Advanced tokenization with linguistic features</li>

            <li><b>WordPiece</b> - Subword tokenization used by BERT and other transformers</li>

        </ul>

    </div>

    """)
    
    try:
        # Ensure NLTK resources are downloaded
        download_nltk_resources()
        
        # Original Text
        output_html.append('<h3 class="task-subheader">Original Text</h3>')
        output_html.append(f'<div class="card"><div class="card-body"><div class="text-content" style="word-wrap: break-word; word-break: break-word; overflow-wrap: break-word; max-height: 500px; overflow-y: auto; padding: 15px; background-color: #f8f9fa; border-radius: 5px; border: 1px solid #e9ecef; line-height: 1.6;">{text_input}</div></div></div>')
        
        # Word Tokenization
        output_html.append('<h3 class="task-subheader">Word Tokenization</h3>')
        output_html.append('<p>Breaking text into individual words and punctuation marks.</p>')
        
        # NLTK Word Tokenization
        nltk_tokens = word_tokenize(text_input)
        
        # Format tokens
        token_html = ""
        for token in nltk_tokens:
            token_html += f'<span class="token">{token}</span>'
        
        output_html.append(f"""

        <div class="card">

            <div class="card-body">

                <div style="background-color: #f5f5f5; padding: 15px; border-radius: 5px; line-height: 2.5;">

                    {token_html}

                </div>

            </div>

        </div>

        <style>

            .token {{

                background-color: #E3F2FD;

                border: 1px solid #1976D2;

                border-radius: 4px;

                padding: 3px 6px;

                margin: 3px;

                display: inline-block;

            }}

        </style>

        """)
        
        # Token statistics
        token_count = len(nltk_tokens)
        unique_tokens = len(set([t.lower() for t in nltk_tokens]))
        alpha_only = sum(1 for t in nltk_tokens if t.isalpha())
        numeric = sum(1 for t in nltk_tokens if t.isnumeric())
        punct = sum(1 for t in nltk_tokens if all(c in '.,;:!?-"\'()[]{}' for c in t))
        
        output_html.append(f"""

        <div class="row mt-3">

            <div class="col-md-2">

                <div class="card text-center">

                    <div class="card-body">

                        <h5 class="text-primary">{token_count}</h5>

                        <small>Total Tokens</small>

                    </div>

                </div>

            </div>

            <div class="col-md-2">

                <div class="card text-center">

                    <div class="card-body">

                        <h5 class="text-success">{unique_tokens}</h5>

                        <small>Unique Tokens</small>

                    </div>

                </div>

            </div>

            <div class="col-md-2">

                <div class="card text-center">

                    <div class="card-body">

                        <h5 class="text-info">{alpha_only}</h5>

                        <small>Alphabetic</small>

                    </div>

                </div>

            </div>

            <div class="col-md-2">

                <div class="card text-center">

                    <div class="card-body">

                        <h5 class="text-warning">{numeric}</h5>

                        <small>Numeric</small>

                    </div>

                </div>

            </div>

            <div class="col-md-2">

                <div class="card text-center">

                    <div class="card-body">

                        <h5 class="text-danger">{punct}</h5>

                        <small>Punctuation</small>

                    </div>

                </div>

            </div>

        </div>

        """)
        
        # Sentence Tokenization
        output_html.append('<h3 class="task-subheader">Sentence Tokenization</h3>')
        output_html.append('<p>Dividing text into individual sentences.</p>')
        
        # NLTK Sentence Tokenization
        nltk_sentences = sent_tokenize(text_input)
        
        # Format sentences
        sentence_html = ""
        for i, sentence in enumerate(nltk_sentences):
            sentence_html += f'<div class="sentence"><span class="sentence-num">{i+1}</span> {sentence}</div>'
        
        output_html.append(f"""

        <div class="card">

            <div class="card-body">

                <div style="background-color: #f5f5f5; padding: 15px; border-radius: 5px;">

                    {sentence_html}

                </div>

            </div>

        </div>

        <style>

            .sentence {{

                background-color: #E1F5FE;

                border-left: 3px solid #03A9F4;

                padding: 10px;

                margin: 8px 0;

                border-radius: 0 5px 5px 0;

                position: relative;

            }}

            .sentence-num {{

                font-weight: bold;

                color: #0277BD;

                margin-right: 5px;

            }}

        </style>

        """)
        
        output_html.append(f'<p class="mt-3">Text contains {len(nltk_sentences)} sentences with an average of {token_count / len(nltk_sentences):.1f} tokens per sentence.</p>')
        
        # Advanced Tokenization with spaCy
        output_html.append('<h3 class="task-subheader">Linguistic Tokenization (spaCy)</h3>')
        output_html.append('<p>spaCy provides more linguistically-aware tokenization with additional token properties.</p>')
        
        # Load spaCy model
        nlp = load_spacy()
        doc = nlp(text_input)
        
        # Create token table
        token_data = []
        for token in doc:
            token_data.append({
                'Text': token.text,
                'Lemma': token.lemma_,
                'POS': token.pos_,
                'Tag': token.tag_,
                'Dep': token.dep_,
                'Shape': token.shape_,
                'Alpha': token.is_alpha,
                'Stop': token.is_stop
            })
        
        token_df = pd.DataFrame(token_data)
        
        # Display interactive table with expandable rows
        output_html.append("""

        <div class="table-responsive">

            <table class="table table-striped table-hover">

                <thead class="table-primary sticky-top">

                    <tr>

                        <th>Token</th>

                        <th>Lemma</th>

                        <th>POS</th>

                        <th>Tag</th>

                        <th>Dependency</th>

                        <th>Properties</th>

                    </tr>

                </thead>

                <tbody>

        """)
        
        for token in doc:
            # Determine row color based on token type
            row_class = ""
            if token.is_stop:
                row_class = "table-danger"  # Light red for stopwords
            elif token.pos_ == "VERB":
                row_class = "table-success"  # Light green for verbs
            elif token.pos_ == "NOUN" or token.pos_ == "PROPN":
                row_class = "table-primary"  # Light blue for nouns
            elif token.pos_ == "ADJ":
                row_class = "table-warning"  # Light yellow for adjectives
            
            output_html.append(f"""

            <tr class="{row_class}">

                <td><strong>{token.text}</strong></td>

                <td>{token.lemma_}</td>

                <td>{token.pos_}</td>

                <td>{token.tag_}</td>

                <td>{token.dep_}</td>

                <td>

                    <span class="badge {'bg-success' if token.is_alpha else 'bg-danger'}">

                        {'Alpha' if token.is_alpha else 'Non-alpha'}

                    </span>

                    <span class="badge {'bg-danger' if token.is_stop else 'bg-success'}">

                        {'Stopword' if token.is_stop else 'Content'}

                    </span>

                    <span class="badge bg-info">

                        Shape: {token.shape_}

                    </span>

                </td>

            </tr>

            """)
        
        output_html.append("""

                </tbody>

            </table>

        </div>

        """)
        
        # Create visualization for POS distribution
        pos_counts = Counter([token.pos_ for token in doc])
        
        # Create bar chart for POS distribution
        fig = plt.figure(figsize=(10, 6))
        plt.bar(pos_counts.keys(), pos_counts.values(), color='#1976D2')
        plt.xlabel('Part of Speech')
        plt.ylabel('Count')
        plt.title('Part-of-Speech Distribution')
        plt.xticks(rotation=45)
        plt.tight_layout()
        
        output_html.append('<h4>Token Distribution by Part of Speech</h4>')
        output_html.append(fig_to_html(fig))
        
        # Subword Tokenization
        output_html.append('<h3 class="task-subheader">Subword Tokenization (WordPiece/BPE)</h3>')
        output_html.append("""

        <div class="alert alert-light">

            <p>

                Subword tokenization breaks words into smaller units to handle rare words and morphologically rich languages.

                This technique is widely used in modern transformer models like BERT, GPT, etc.

            </p>

        </div>

        """)
        
        try:
            from transformers import BertTokenizer, GPT2Tokenizer
            
            # Load tokenizers
            bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
            gpt2_tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
            
            # Tokenize with BERT
            bert_tokens = bert_tokenizer.tokenize(text_input)
            
            # Tokenize with GPT-2
            # GPT-2 doesn't have a special tokenize method like BERT, so we encode and decode
            gpt2_encoding = gpt2_tokenizer.encode(text_input)
            gpt2_tokens = [gpt2_tokenizer.decode([token]).strip() for token in gpt2_encoding]
            
            # BERT WordPiece Section
            output_html.append('<h4 class="bg-primary text-white p-3 rounded">BERT WordPiece</h4>')
            output_html.append('<p>BERT uses WordPiece tokenization which marks subword units with ##.</p>')
            
            # Create token display
            output_html.append('<div class="card"><div class="card-body">')
            output_html.append('<div style="background-color: #f5f5f5; padding: 15px; border-radius: 5px; line-height: 2.5;">')
            
            for token in bert_tokens:
                if token.startswith("##"):
                    output_html.append(f'<span class="token" style="background-color: #FFECB3; border-color: #FFA000;">{token}</span>')
                else:
                    output_html.append(f'<span class="token">{token}</span>')
            
            output_html.append('</div></div></div>')
            output_html.append(f'<p class="mt-2">Total BERT tokens: {len(bert_tokens)}</p>')
            
            # GPT-2 BPE Section
            output_html.append('<h4 class="bg-primary text-white p-3 rounded mt-4">GPT-2 BPE</h4>')
            output_html.append('<p>GPT-2 uses Byte-Pair Encoding (BPE) tokenization where Ġ represents a space before the token.</p>')
            
            output_html.append('<div class="card"><div class="card-body">')
            output_html.append('<div style="background-color: #f5f5f5; padding: 15px; border-radius: 5px; line-height: 2.5;">')
            
            for token in gpt2_tokens:
                if token.startswith("Ġ"):
                    output_html.append(f'<span class="token">{token}</span>')
                else:
                    output_html.append(f'<span class="token" style="background-color: #FFECB3; border-color: #FFA000;">{token}</span>')
            
            output_html.append('</div></div></div>')
            output_html.append(f'<p class="mt-2">Total GPT-2 tokens: {len(gpt2_tokens)}</p>')
            
            # Compare token counts
            output_html.append('<h4>Token Count Comparison</h4>')
            token_count_data = {
                'Tokenizer': ['Words (spaces)', 'NLTK', 'spaCy', 'BERT WordPiece', 'GPT-2 BPE'],
                'Token Count': [
                    len(text_input.split()),
                    len(nltk_tokens),
                    len(doc),
                    len(bert_tokens),
                    len(gpt2_tokens)
                ]
            }
            
            token_count_df = pd.DataFrame(token_count_data)
            
            # Create comparison chart
            fig = plt.figure(figsize=(10, 6))
            bars = plt.bar(token_count_df['Tokenizer'], token_count_df['Token Count'], color=['#BBDEFB', '#90CAF9', '#64B5F6', '#42A5F5', '#2196F3'])
            
            # Add value labels on top of bars
            for bar in bars:
                height = bar.get_height()
                plt.text(bar.get_x() + bar.get_width()/2., height + 0.5,
                        f'{height}',
                        ha='center', va='bottom')
            
            plt.ylabel('Token Count')
            plt.title('Tokenization Comparison by Method')
            plt.ylim(0, max(token_count_df['Token Count']) * 1.1)  # Add some headroom for labels
            plt.tight_layout()
            
            output_html.append(fig_to_html(fig))
            
            # Add token length distribution analysis
            output_html.append('<h4>Token Length Distribution</h4>')
            token_lengths = [len(token) for token in nltk_tokens]
            
            fig = plt.figure(figsize=(10, 6))
            plt.hist(token_lengths, bins=range(1, max(token_lengths) + 2), color='#4CAF50', alpha=0.7)
            plt.xlabel('Token Length')
            plt.ylabel('Frequency')
            plt.title('Token Length Distribution')
            plt.grid(axis='y', alpha=0.3)
            plt.tight_layout()
            
            output_html.append(fig_to_html(fig))
            
            # Add tokenization statistics summary
            avg_token_length = sum(token_lengths) / len(token_lengths) if token_lengths else 0
            output_html.append(f"""

            <h4>Tokenization Statistics</h4>

            <div class="row mt-3">

                <div class="col-md-4">

                    <div class="card text-center">

                        <div class="card-body">

                            <h3 class="text-success">{token_count}</h3>

                            <p class="mb-0">Total Tokens</p>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <div class="card text-center">

                        <div class="card-body">

                            <h3 class="text-primary">{avg_token_length:.2f}</h3>

                            <p class="mb-0">Average Token Length</p>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <div class="card text-center">

                        <div class="card-body">

                            <h3 class="text-warning">{token_count / len(nltk_sentences):.2f}</h3>

                            <p class="mb-0">Tokens per Sentence</p>

                        </div>

                    </div>

                </div>

            </div>

            """)
            
        except Exception as e:
            output_html.append(f"""

            <div class="alert alert-warning">

                <h4>Subword Tokenization Error</h4>

                <p>Failed to load transformer tokenizers: {str(e)}</p>

                <p>The transformers library may not be installed or there might be network issues when downloading models.</p>

            </div>

            """)
    
    except Exception as e:
        output_html.append(f"""

        <div class="alert alert-danger">

            <h3>Error</h3>

            <p>Failed to process tokenization: {str(e)}</p>

        </div>

        """)
    
    # About Tokenization section
    output_html.append("""

    <div class="card mt-4">

        <div class="card-header">

            <h4 class="mb-0">

                <i class="fas fa-info-circle"></i>

                About Tokenization

            </h4>

        </div>

        <div class="card-body">

            <h5>What is Tokenization?</h5>

            

            <p>Tokenization is the process of breaking down text into smaller units called tokens.

            These tokens can be words, subwords, characters, or symbols, depending on the approach.

            It's typically the first step in most NLP pipelines.</p>

            

            <h5>Types of Tokenization:</h5>

            

            <ul>

                <li><b>Word Tokenization</b> - Splits text on whitespace and punctuation (with various rules)</li>

                <li><b>Sentence Tokenization</b> - Divides text into sentences using punctuation and other rules</li>

                <li><b>Subword Tokenization</b> - Splits words into meaningful subunits (WordPiece, BPE, SentencePiece)</li>

                <li><b>Character Tokenization</b> - Treats each character as a separate token</li>

            </ul>

            

            <h5>Why Subword Tokenization?</h5>

            

            <p>Modern NLP models use subword tokenization because:</p>

            <ul>

                <li>It handles out-of-vocabulary words better</li>

                <li>It represents rare words by decomposing them</li>

                <li>It works well for morphologically rich languages</li>

                <li>It balances vocabulary size and token length</li>

            </ul>

        </div>

    </div>

    """)
    
    output_html.append('</div>')  # Close result-area div
    
    return '\n'.join(output_html)