Program 6: Use a pre-trained Hugging Face model to analyze sentiment in text.
Assume a real-
world application, load the sentiment analysis pipeline. Analyze the sentiment by giving
sentences to input.
Aim
To implement sentiment analysis on customer reviews using the Hugging Face Transformers
library in Python.
Objective
To understand the concept of sentiment analysis.
To use a pre-trained NLP model for classifying text as Positive or Negative.
To analyze real-world customer feedback automatically.
Theory
Sentiment Analysis
Sentiment Analysis is a Natural Language Processing (NLP) technique used to determine
whether a piece of text expresses positive, negative, or neutral sentiment. It is widely used in:
Customer feedback analysis
Product review evaluation
Social media monitoring
Market research
Transformers Library
The transformers library by Hugging Face provides pre-trained models for various NLP tasks,
such as:
Sentiment Analysis
Text Classification
Question Answering
Text Generation
In this program, we use the pipeline() function, which simplifies the use of pre-trained models.
Pipeline
The pipeline("sentiment-analysis") automatically loads a pre-trained sentiment classification
model and tokenizer.
The model:
Takes text as input
Predicts sentiment (POSITIVE / NEGATIVE)
Returns confidence score
Algorithm
1. Install the required library (transformers).
2. Import the pipeline function from the transformer’s module.
3. Load the pre-trained sentiment analysis model.
4. Define a function to analyze sentiment.
5. Provide customer review inputs.
6. Pass each review to the sentiment analyzer.
7. Display sentiment label and confidence score.
Source Code:
OUTPUT:
Result
Thus, the sentiment analysis model was successfully implemented using the Hugging Face
Transformers library, and customer reviews were classified as Positive or Negative with
confidence scores.
Applications
E-commerce product review analysis
Brand reputation monitoring
Social media sentiment tracking
Automated feedback evaluation