--- 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 ``` ---