0
0
NLPml~5 mins

Python NLP ecosystem (NLTK, spaCy, Hugging Face)

Choose your learning style9 modes available
Introduction

Python NLP ecosystem helps computers understand and work with human language. It makes tasks like reading, analyzing, and generating text easier.

You want to analyze the sentiment of customer reviews.
You need to extract names and places from news articles.
You want to build a chatbot that understands user questions.
You want to translate text from one language to another.
You want to summarize long documents automatically.
Syntax
NLP
import nltk
import spacy
from transformers import pipeline

NLTK is great for learning and simple text processing.

spaCy is fast and good for real-world applications like entity recognition.

Hugging Face offers powerful pre-trained models for many NLP tasks.

Examples
NLTK example: splitting text into words (tokens).
NLP
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
text = "Hello world!"
tokens = word_tokenize(text)
print(tokens)
spaCy example: finding named entities like companies and locations.
NLP
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp("Apple is looking at buying a startup in the UK.")
for ent in doc.ents:
    print(ent.text, ent.label_)
Hugging Face example: analyzing sentiment with a pre-trained model.
NLP
from transformers import pipeline
sentiment = pipeline('sentiment-analysis')
result = sentiment('I love learning NLP!')
print(result)
Sample Model

This program shows how to use NLTK to split text into words, spaCy to find named entities, and Hugging Face to analyze sentiment.

NLP
import nltk
import spacy
from transformers import pipeline

# NLTK: Tokenize text
nltk.download('punkt')
from nltk.tokenize import word_tokenize
text = "Python NLP ecosystem is fun and powerful."
tokens = word_tokenize(text)
print('NLTK tokens:', tokens)

# spaCy: Named Entity Recognition
nlp = spacy.load('en_core_web_sm')
doc = nlp("Google is a big tech company based in the USA.")
entities = [(ent.text, ent.label_) for ent in doc.ents]
print('spaCy entities:', entities)

# Hugging Face: Sentiment Analysis
sentiment = pipeline('sentiment-analysis')
sent_result = sentiment('I enjoy learning new things in AI!')
print('Hugging Face sentiment:', sent_result)
OutputSuccess
Important Notes

Make sure to install required packages: nltk, spacy, transformers.

Download spaCy language model with: python -m spacy download en_core_web_sm

Hugging Face models require internet connection to download pre-trained weights the first time.

Summary

NLTK, spaCy, and Hugging Face are popular Python tools for NLP.

NLTK is good for learning and basic tasks.

spaCy is fast and great for real-world text processing.

Hugging Face provides powerful pre-trained models for advanced NLP tasks.