We use a sentiment analysis pipeline to quickly find out if text feels positive, negative, or neutral. It helps us understand opinions in reviews, tweets, or messages without reading all of them.
Sentiment analysis pipeline in NLP
from transformers import pipeline sentiment_pipeline = pipeline('sentiment-analysis') results = sentiment_pipeline(['I love this!', 'This is bad.'])
The pipeline function loads a ready-to-use model for sentiment analysis.
Input is a list of texts, and output gives labels like 'POSITIVE' or 'NEGATIVE' with scores.
from transformers import pipeline sentiment = pipeline('sentiment-analysis') print(sentiment(['I am happy']))
from transformers import pipeline sentiment = pipeline('sentiment-analysis') texts = ['I hate waiting.', 'What a wonderful day!'] results = sentiment(texts) for r in results: print(f"Label: {r['label']}, Score: {r['score']:.2f}")
This program uses a ready-made sentiment analysis model to classify three example sentences. It prints the sentiment label and confidence score for each.
from transformers import pipeline # Load the sentiment analysis pipeline sentiment = pipeline('sentiment-analysis') # Sample texts to analyze texts = [ 'I love this product, it works great!', 'This is the worst experience I have ever had.', 'It is okay, not too bad but not great either.' ] # Get sentiment results results = sentiment(texts) # Print results for text, result in zip(texts, results): print(f"Text: {text}") print(f"Sentiment: {result['label']}, Confidence: {result['score']:.2f}") print()
The pipeline uses a model trained on many examples to guess sentiment quickly.
Confidence scores close to 1 mean the model is very sure about its prediction.
Sometimes neutral or mixed feelings may be labeled as positive or negative depending on the model.
Sentiment analysis pipeline helps find feelings in text automatically.
It is easy to use with just a few lines of code.
Useful for understanding opinions in many real-life situations.