Spaces:
Sleeping
Sleeping
File size: 3,728 Bytes
3a85e75 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c 819ae22 8e6e79c | 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 | ---
title: TokenLab
emoji: π
colorFrom: purple
colorTo: blue
sdk: gradio
sdk_version: 5.34.2
app_file: app.py
pinned: false
license: mit
---
# π§ Interactive Text Tokenization App
An interactive Gradio app for exploring how different language models tokenize and process natural language inputs. The app demonstrates Tokenization, Stemming, and Lemmatization using modern NLP tools.
---
## π Features
- Input a sentence and choose a tokenizer (`gpt2` or `bert-base-uncased`)
- Display:
- Tokens from the selected model
- Decoded tokens (token IDs and readable tokens)
- Stemmed words using **PorterStemmer**
- Lemmatized words using **spaCy** (if available) or fallback to **WordNetLemmatizer**
- Comparison table:
| Original | Stemmed | Lemmatized |
|----------|---------|------------|
| running | runn | run |
| better | better | good |
- Comparison of Tokenizers:
- BERT (WordPiece)
- GPT2 (BPE)
- tiktoken (OpenAI's cl100k_base) β *if installed*
- Markdown explanations and linguistic insights
---
## π§° Technologies Used
- **Gradio** β for the user interface
- **Transformers (Hugging Face)** β AutoTokenizer for GPT2 and BERT
- **nltk** β for stemming and fallback lemmatization
- **spaCy** β primary lemmatizer (if available)
- **tiktoken** β OpenAI tokenizer, used if installed
---
## π How to Use
1. Enter a sentence (e.g., `I was running faster than him`)
2. Choose a tokenizer (e.g., `gpt2` or `bert`)
3. Click **Analyze**
4. View:
- Token comparisons (tokens, decoded, token IDs)
- Stemmed & Lemmatized results
- Table comparison
- Linguistic explanation
---
## π What is Tokenization?
Tokenization breaks text into smaller units called tokens β words, subwords, or characters. Different models tokenize differently:
- **GPT2** uses *byte-pair encoding* (BPE)
- **BERT** uses *WordPiece* encoding
- **tiktoken** simulates tokenization in OpenAI models like ChatGPT
---
## πΏ What is Stemming?
Stemming chops off suffixes to reduce a word to its base form.
- Example: `running`, `runner`, `runs` β `run`
- Can produce non-words: `running` β `runn`
- Tool used: `nltk.PorterStemmer`
---
## π What is Lemmatization?
Lemmatization maps a word to its **dictionary base form** using linguistic rules.
- Example: `running` β `run`, `better` β `good`
- More accurate than stemming
- Tools used:
- `spaCy` (if available)
- Fallback: `nltk.WordNetLemmatizer`
---
## π When to Use Each?
| Technique | Use Case | Pros | Cons |
|---------------|-------------------------------------------|------------------------------|---------------------------|
| Tokenization | Model preprocessing | Fast, essential | Model-dependent results |
| Stemming | Lightweight, fast normalization | Simple, quick | Less accurate |
| Lemmatization | Semantic-level analysis | Linguistic, precise | Slower, depends on tools |
---
## π§ͺ Example Insight
> Do all models treat `running` the same?
No:
- **BERT** might split it into `["run", "##ning"]`
- **GPT2** may tokenize it differently (based on byte-pairs)
- **spaCy** lemmatizes `running` β `run`, **WordNet** does the same (but context-dependent)
- **Stemming** gives: `runn`
---
## π§ Built as part of a Natural Language Processing assignment
---
## π Repository Files
- `app.py`: Main Gradio application
- `README.md`: This file
- `requirements.txt`: Project dependencies
---
## π¦ Installation (for local use)
```bash
pip install -r requirements.txt
python app.py
```
---
|