# Import a text processing library
from textblob import TextBlob
# Some test sentences
sentences = ["It is a pleasant day and the weather is such a bliss.",
"It is a damn awful weather and I hate to be outside today."]
# Analysis method
def analysis():
for sentence in sentences:
# Instantiate TextBlob
analysis = TextBlob(sentence)
# Parts of speech
print("{}\n\nParts of speech:\n{}\n".format(sentence, analysis.tags))
# Sentiment analysis
print("Sentiment:\n{}\n\n".format(analysis.sentiment))
analysis()
The sentiment of the sentences are expressed in terms of polarity and subjectivity. Polarity is scored from -1.0 to +1.0 and subjectivity is scored from 0.0 to 1.0.
In the above results, the polarity of the first sentence is +0.36
, reflecting the positive sentiment. For the second sentence, the polarity is negative -0.6
, classifying it as a negative sentiment.