---
name: nltk-e15d867f8993
description: Expert NLTK (Natural Language Toolkit) assistance covering tokenization, stemming, lemmatization, sentiment analysis, and POS tagging. Use when preprocessing text corpora and classical NLP tasks.
---

# NLTK

NLTK is the classic library for teaching and researching NLP. While slower than spaCy, it offers **comprehensive** linguistic data.

## When to Use

- **Classical NLP & Linguistics**: Tokenization, stemming, lemmatization, part-of-speech (POS) tagging, and syntactic parsing.
- **Rule-Based Sentiment Analysis**: Quick heuristic sentiment scoring with VADER for social media texts.
- **Text Preprocessing for Classical ML**: Cleaning corpus text for TF-IDF, Naive Bayes, and SVM models.
- **Educational & Linguistic Research**: Analyzing word frequencies, collocations, and lexical dispersion across corpora.

## Quick Start

```python
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords

nltk.download('punkt_tab')
nltk.download('stopwords')

text = "Natural Language Toolkit provides classical algorithms for text analysis."
tokens = word_tokenize(text)
stop_words = set(stopwords.words('english'))
filtered = [w for w in tokens if w.lower() not in stop_words and w.isalnum()]

print("Filtered tokens:", filtered)
```

## Core Concepts

### Tokenization, Stopwords & WordNet Lemmatization

Standard text normalization pipeline:

```python
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import string

# Ensure required corpora are downloaded
nltk.download('punkt_tab', quiet=True)
nltk.download('stopwords', quiet=True)
nltk.download('wordnet', quiet=True)

def preprocess_text(raw_text: str) -> list[str]:
    # 1. Lowercase and tokenize
    tokens = word_tokenize(raw_text.lower())

    # 2. Filter punctuation and English stopwords
    stop_words = set(stopwords.words('english'))
    filtered = [w for w in tokens if w.isalnum() and w not in stop_words]

    # 3. Lemmatize words to base dictionary form
    lemmatizer = WordNetLemmatizer()
    lemmatized = [lemmatizer.lemmatize(w) for w in filtered]

    return lemmatized

sample = "The distributed microservices were running exceptionally fast and scaling reliably!"
print("Cleaned Tokens:", preprocess_text(sample))
```

### VADER Sentiment Analysis for Fast Heuristic Scoring

Rule-based sentiment intensity scoring:

```python
from nltk.sentiment.vader import SentimentIntensityAnalyzer

nltk.download('vader_lexicon', quiet=True)
sia = SentimentIntensityAnalyzer()

reviews = [
    "Antigravity IDE is amazingly responsive and saves hours of tedious refactoring!",
    "The legacy codebase crashes constantly with unhelpful error messages.",
    "The software was delivered on Tuesday afternoon."
]

for review in reviews:
    scores = sia.polarity_scores(review)
    compound = scores['compound']
    sentiment = 'positive' if compound >= 0.05 else ('negative' if compound <= -0.05 else 'neutral')
    print(f"Sentiment: {sentiment:8s} (Score: {compound:+.2f}) | {review[:50]}...")
```

### Part-of-Speech (POS) Tagging & Named Entity Chunking

Extracting grammatical structures from text:

```python
nltk.download('averaged_perceptron_tagger_eng', quiet=True)
nltk.download('maxent_ne_chunker_tab', quiet=True)
nltk.download('words', quiet=True)

sentence = "Sundar Pichai announced new Gemini models at Google headquarters."
tokens = word_tokenize(sentence)
pos_tags = nltk.pos_tag(tokens)

# Extract named entities
chunks = nltk.ne_chunk(pos_tags)
for chunk in chunks:
    if hasattr(chunk, 'label'):
        print(f"Entity: {' '.join(c[0] for c in chunk)} [{chunk.label()}]")
```

## Common Patterns

### Lemmatization with POS (Part of Speech) Tagging

**Problem**: Simple stemmers truncate words unnaturally (`running` -> `run`, but `better` -> `better`).

**Solution**:
Combine WordNetLemmatizer with POS tags:

```python
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
import nltk

nltk.download('averaged_perceptron_tagger_eng')
nltk.download('wordnet')

lemmatizer = WordNetLemmatizer()
# Lemmatize with explicit verb POS tag
print(lemmatizer.lemmatize("running", wordnet.VERB)) # Output: run
print(lemmatizer.lemmatize("better", wordnet.ADJ))   # Output: good
```

## Best Practices

**Do**:

- Prefer lemmatization over stemming (`PorterStemmer`) for human-readable root words.
- Download specific corpora explicitly in setup scripts (`nltk.download('punkt_tab')`) to prevent runtime failures in Docker.
- Leverage VADER for social media text where punctuation and capitalization convey sentiment.
- Migrate to spaCy or Hugging Face transformers when semantic understanding or modern deep learning is required.

**Don't**:

- Call `nltk.download()` repeatedly on every request inside web handlers; download once at build/container time.
- Use NLTK for high-throughput production tokenization; use Hugging Face tokenizers or spaCy.
- Use WordNet lemmatizer without specifying POS tags if fine-grained verb/noun distinction is needed.

## Troubleshooting

| Error                                   | Cause                                                               | Solution                                                              |
| :-------------------------------------- | :------------------------------------------------------------------ | :-------------------------------------------------------------------- |
| `Resource punkt_tab not found`          | Required NLTK dataset not downloaded locally.                       | Add `nltk.download('punkt_tab')` before tokenization call.            |
| `LookupError: WordNet resource missing` | WordNet database not installed.                                     | Execute `nltk.download('wordnet')`.                                   |
| `Slow tokenization on massive text`     | NLTK regex tokenizers running single-threaded on millions of lines. | Batch processing with multiprocessing or use Hugging Face tokenizers. |

## References

- [NLTK Documentation](https://www.nltk.org/)


---
Source attribution: G1Joshi
Original project: https://github.com/G1Joshi/Agent-Skills/blob/6a74cf03a8a86dc7d993ead3091b41123dbd0181/skills/ai-ml/nltk/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 G1Joshi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: G1Joshi
Original project: https://github.com/G1Joshi/Agent-Skills/blob/6a74cf03a8a86dc7d993ead3091b41123dbd0181/skills/ai-ml/nltk/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering, Education & Research
Roles: Data Scientist, ML / AI Engineer, Researcher
Specialties: API, Cross Platform
Tools: Nltk
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: b61ec3fa147b71cc15612b9dca85cd6b947f45cd6f7c8eb3c8b202443af816af
