Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I'm trying to write a program that takes text(article) as input and outputs the polarity of this text, whether its a positive or negative sentiment. I've read extensively about different approaches but I am still confused. I read about many techniques like classifiers and machine learning. I would like direction and clear instructions on where to start. For example, I have a classifier that requires a dataset but how do I convert the text(article) into a dataset for the classifier. If anyone can tell me the logical sequence to approach this problem that would be great. Thanks in advance! PS: please mention any related algorithms or open-source implementation.

2 Answers

0 votes
by (108k points)

You can easily analyze the positivity or negativity of the texts by VADER Sentiment. Analysis 

You can install the vanderSentiment by the following code:

pip install vaderSentiment

VADER (Valence Aware Dictionary and sEntiment Reasoner) is a rule-based sentiment analysis tool that is specifically attuned for the sentiments that are being expressed in social media. VADER uses a combination of A sentiment lexicon is a list of lexical features (e.g., words) which are generally labeled according to their semantic orientation as either positive or negative. VADER not only tells about the Positivity and Negativity score but also tells us about how positive or negative sentiment is. 

You can refer the following code:

# import SentimentIntensityAnalyzer class 

# from vaderSentiment.vaderSentiment module. from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer 

# function to print sentiments 

# of the sentence. def sentiment_scores(sentence): 

# Create a SentimentIntensityAnalyzer object. sid_obj = SentimentIntensityAnalyzer(

# polarity_scores method of SentimentIntensityAnalyzer 

# oject gives a sentiment dictionary. 

# which contains pos, neg, neu, and compound scores. sentiment_dict = sid_obj.polarity_scores(sentence) print("Overall sentiment dictionary is : ", sentiment_dict) 

print("sentence was rated as ", sentiment_dict['neg']*100, "% Negative") 

print("sentence was rated as ", sentiment_dict['neu']*100, "% Neutral") 

print("sentence was rated as ", sentiment_dict['pos']*100, "% Positive") 

print("Sentence Overall Rated As", end = " ") 

# decide sentiment as positive, negative and neutral 

if sentiment_dict['compound'] >= 0.05 : print("Positive") elif sentiment_dict['compound'] <= - 0.05 : print("Negative") else : 

print("Neutral") 

# Driver code if __name__ == "__main__" : 

print("\n1st statement :") sentence = "Geeks For Geeks is the best portal for \ the computer science engineering students." 

# function calling sentiment_scores(sentence) 

print("\n2nd Statement :") sentence = "study is going on as usual" sentiment_scores(sentence) 

print("\n3rd Statement :") sentence = "I am vey sad today." sentiment_scores(sentence)

The output score is a metric that calculates the sum of all the ratings which have been normalized between -1(most extreme negative) and +1 (most extreme positive).

positive sentiment : (compound score >= 0.05)

neutral sentiment : (compound score > -0.05) and (compound score < 0.05)

negative sentiment : (compound score <= -0.05)

Hope this helps!!!

If you wish to know NLP and Machine Learning for Sentiment Analysis then visit this Machine Learning Course.

0 votes
by (33.1k points)

For Python, I'd suggest looking at NLTK and the NLTK book.

This blog: streamhacker.com has some very good articles to get you started.

There's been lots of research in this area since the late 2000s.

Stanford researches made a breakthrough in sentiment analysis that has achieved more than 85% accuracy on average.

Browse Categories

...