You can download this code by clicking the button below.
This code is now available for download.
This function uses the spaCy library to analyze the sentiment of the input text and returns the sentiment result (positive, negative, or neutral).
Technology Stack : spaCy
Code Type : Function
Code Difficulty : Intermediate
import spacy
from spacy.tokens import Doc
def analyze_text_sentiment(text):
# Load English tokenizer, tagger, parser, NER and word vectors
nlp = spacy.load('en_core_web_sm')
# Process whole documents
doc = nlp(text)
# Analyze sentiment
sentiment_score = 0
for token in doc:
if token.sentiment:
sentiment_score += token.sentiment
# Determine overall sentiment based on the average score
if sentiment_score > 0:
overall_sentiment = 'Positive'
elif sentiment_score < 0:
overall_sentiment = 'Negative'
else:
overall_sentiment = 'Neutral'
return overall_sentiment