Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -9,80 +9,107 @@ app_file: app.py
|
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
|
|
|
| 12 |
# π§ Interactive Text Tokenization App
|
| 13 |
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.
|
|
|
|
| 14 |
---
|
| 15 |
## π Features
|
| 16 |
-
- Input a sentence and choose a tokenizer (gpt2 or bert-base-uncased)
|
| 17 |
- Display:
|
| 18 |
- Tokens from the selected model
|
| 19 |
-
- Decoded tokens (
|
| 20 |
-
- Stemmed words using PorterStemmer
|
| 21 |
-
- Lemmatized words using
|
| 22 |
-
-
|
| 23 |
|
| 24 |
| Original | Stemmed | Lemmatized |
|
| 25 |
|----------|---------|------------|
|
| 26 |
| running | runn | run |
|
| 27 |
| better | better | good |
|
| 28 |
-
|
| 29 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
---
|
| 31 |
## π§° Technologies Used
|
| 32 |
-
- Gradio β for the user interface
|
| 33 |
-
- Transformers (Hugging Face) β AutoTokenizer for GPT2 and BERT
|
| 34 |
-
- nltk β for stemming and lemmatization
|
| 35 |
-
- spaCy
|
| 36 |
-
- tiktoken β OpenAI tokenizer, if installed
|
|
|
|
| 37 |
---
|
| 38 |
## π How to Use
|
| 39 |
-
1. Enter
|
| 40 |
-
2. Choose a tokenizer (e.g., gpt2)
|
| 41 |
-
3. Click
|
| 42 |
4. View:
|
| 43 |
-
-
|
| 44 |
-
- Stemmed & Lemmatized
|
| 45 |
- Table comparison
|
| 46 |
-
-
|
|
|
|
| 47 |
---
|
| 48 |
## π What is Tokenization?
|
| 49 |
-
Tokenization
|
| 50 |
-
|
| 51 |
-
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
---
|
| 54 |
## πΏ What is Stemming?
|
| 55 |
-
Stemming
|
| 56 |
-
- running, runner, runs β run
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
---
|
| 60 |
## π What is Lemmatization?
|
| 61 |
-
Lemmatization
|
| 62 |
-
- running β run
|
| 63 |
-
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
---
|
| 67 |
## π When to Use Each?
|
| 68 |
| Technique | Use Case | Pros | Cons |
|
| 69 |
|---------------|-------------------------------------------|------------------------------|---------------------------|
|
| 70 |
-
| Tokenization |
|
| 71 |
-
| Stemming |
|
| 72 |
-
| Lemmatization |
|
|
|
|
| 73 |
---
|
| 74 |
## π§ͺ Example Insight
|
| 75 |
-
> Do all models treat running the same
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
---
|
| 78 |
-
## π§ Natural Language Processing assignment
|
|
|
|
| 79 |
---
|
| 80 |
## π Repository Files
|
| 81 |
-
- app.py: Main Gradio application
|
| 82 |
-
- README.md: This file
|
| 83 |
-
- requirements.txt:
|
|
|
|
| 84 |
---
|
| 85 |
## π¦ Installation (for local use)
|
| 86 |
```bash
|
| 87 |
pip install -r requirements.txt
|
| 88 |
-
python app.py
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
+
|
| 13 |
# π§ Interactive Text Tokenization App
|
| 14 |
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.
|
| 15 |
+
|
| 16 |
---
|
| 17 |
## π Features
|
| 18 |
+
- Input a sentence and choose a tokenizer (`gpt2` or `bert-base-uncased`)
|
| 19 |
- Display:
|
| 20 |
- Tokens from the selected model
|
| 21 |
+
- Decoded tokens (token IDs and readable tokens)
|
| 22 |
+
- Stemmed words using **PorterStemmer**
|
| 23 |
+
- Lemmatized words using **spaCy** (if available) or fallback to **WordNetLemmatizer**
|
| 24 |
+
- Comparison table:
|
| 25 |
|
| 26 |
| Original | Stemmed | Lemmatized |
|
| 27 |
|----------|---------|------------|
|
| 28 |
| running | runn | run |
|
| 29 |
| better | better | good |
|
| 30 |
+
|
| 31 |
+
- Comparison of Tokenizers:
|
| 32 |
+
- BERT (WordPiece)
|
| 33 |
+
- GPT2 (BPE)
|
| 34 |
+
- tiktoken (OpenAI's cl100k_base) β *if installed*
|
| 35 |
+
|
| 36 |
+
- Markdown explanations and linguistic insights
|
| 37 |
+
|
| 38 |
---
|
| 39 |
## π§° Technologies Used
|
| 40 |
+
- **Gradio** β for the user interface
|
| 41 |
+
- **Transformers (Hugging Face)** β AutoTokenizer for GPT2 and BERT
|
| 42 |
+
- **nltk** β for stemming and fallback lemmatization
|
| 43 |
+
- **spaCy** β primary lemmatizer (if available)
|
| 44 |
+
- **tiktoken** β OpenAI tokenizer, used if installed
|
| 45 |
+
|
| 46 |
---
|
| 47 |
## π How to Use
|
| 48 |
+
1. Enter a sentence (e.g., `I was running faster than him`)
|
| 49 |
+
2. Choose a tokenizer (e.g., `gpt2` or `bert`)
|
| 50 |
+
3. Click **Analyze**
|
| 51 |
4. View:
|
| 52 |
+
- Token comparisons (tokens, decoded, token IDs)
|
| 53 |
+
- Stemmed & Lemmatized results
|
| 54 |
- Table comparison
|
| 55 |
+
- Linguistic explanation
|
| 56 |
+
|
| 57 |
---
|
| 58 |
## π What is Tokenization?
|
| 59 |
+
Tokenization breaks text into smaller units called tokens β words, subwords, or characters. Different models tokenize differently:
|
| 60 |
+
|
| 61 |
+
- **GPT2** uses *byte-pair encoding* (BPE)
|
| 62 |
+
- **BERT** uses *WordPiece* encoding
|
| 63 |
+
- **tiktoken** simulates tokenization in OpenAI models like ChatGPT
|
| 64 |
+
|
| 65 |
---
|
| 66 |
## πΏ What is Stemming?
|
| 67 |
+
Stemming chops off suffixes to reduce a word to its base form.
|
| 68 |
+
- Example: `running`, `runner`, `runs` β `run`
|
| 69 |
+
- Can produce non-words: `running` β `runn`
|
| 70 |
+
- Tool used: `nltk.PorterStemmer`
|
| 71 |
+
|
| 72 |
---
|
| 73 |
## π What is Lemmatization?
|
| 74 |
+
Lemmatization maps a word to its **dictionary base form** using linguistic rules.
|
| 75 |
+
- Example: `running` β `run`, `better` β `good`
|
| 76 |
+
- More accurate than stemming
|
| 77 |
+
- Tools used:
|
| 78 |
+
- `spaCy` (if available)
|
| 79 |
+
- Fallback: `nltk.WordNetLemmatizer`
|
| 80 |
+
|
| 81 |
---
|
| 82 |
## π When to Use Each?
|
| 83 |
| Technique | Use Case | Pros | Cons |
|
| 84 |
|---------------|-------------------------------------------|------------------------------|---------------------------|
|
| 85 |
+
| Tokenization | Model preprocessing | Fast, essential | Model-dependent results |
|
| 86 |
+
| Stemming | Lightweight, fast normalization | Simple, quick | Less accurate |
|
| 87 |
+
| Lemmatization | Semantic-level analysis | Linguistic, precise | Slower, depends on tools |
|
| 88 |
+
|
| 89 |
---
|
| 90 |
## π§ͺ Example Insight
|
| 91 |
+
> Do all models treat `running` the same?
|
| 92 |
+
|
| 93 |
+
No:
|
| 94 |
+
- **BERT** might split it into `["run", "##ning"]`
|
| 95 |
+
- **GPT2** may tokenize it differently (based on byte-pairs)
|
| 96 |
+
- **spaCy** lemmatizes `running` β `run`, **WordNet** does the same (but context-dependent)
|
| 97 |
+
- **Stemming** gives: `runn`
|
| 98 |
+
|
| 99 |
---
|
| 100 |
+
## π§ Built as part of a Natural Language Processing assignment
|
| 101 |
+
|
| 102 |
---
|
| 103 |
## π Repository Files
|
| 104 |
+
- `app.py`: Main Gradio application
|
| 105 |
+
- `README.md`: This file
|
| 106 |
+
- `requirements.txt`: Project dependencies
|
| 107 |
+
|
| 108 |
---
|
| 109 |
## π¦ Installation (for local use)
|
| 110 |
```bash
|
| 111 |
pip install -r requirements.txt
|
| 112 |
+
python app.py
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
---
|