You can download this code by clicking the button below.
This code is now available for download.
This function uses a pre-trained sentiment classifier from the Flair library to classify the sentiment of the input text and returns the sentiment label (e.g., positive, negative).
Technology Stack : Flair, TextClassifier, Sentence
Code Type : Text classification
Code Difficulty : Intermediate
import random
from flair.models import TextClassifier
from flair.data import Sentence
import pandas as pd
def classify_sentiment(text):
# Load a pre-trained sentiment classifier
classifier = TextClassifier.load('en-sentiment')
# Create a sentence from the input text
sentence = Sentence(text)
# Use the classifier to predict the sentiment of the sentence
classifier.predict(sentence)
# Extract the sentiment label
sentiment = sentence.labels[0].value
return sentiment