Python - Sentiment Analysis


Semantic Analysis is about analysing the general opinion of the audience. It may be a reaction to a piece of news, movie or any a tweet about some matter under discussion. Generally, such reactions are taken from social media and clubbed into a file to be analysed through NLP. We will take a simple case of defining positive and negative words first. Then taking an approach to analyse those words as part of sentences using those words. We use the sentiment_analyzer module from nltk. We first carry out the analysis with one word and then with paired words also called bigrams. Finally, we mark the words with negative sentiment as defined in the mark_negation function.

import nltk
import nltk.sentiment.sentiment_analyzer 

# Analysing for single words
def OneWord(): 
	positive_words = ['good', 'progress', 'luck']
   	text = 'Hard Work brings progress and good luck.'.split()                 
	analysis = nltk.sentiment.util.extract_unigram_feats(text, positive_words) 
	print(' ** Sentiment with one word **\n')

# Analysing for a pair of words	
def WithBigrams(): 
	word_sets = [('Regular', 'fit'), ('fit', 'fine')] 
	text = 'Regular excercise makes you fit and fine'.split() 
	analysis = nltk.sentiment.util.extract_bigram_feats(text, word_sets) 
	print('\n*** Sentiment with bigrams ***\n') 
	print analysis

# Analysing the negation words. 
def NegativeWord():
	text = 'Lack of good health can not bring success to students'.split() 
	analysis = nltk.sentiment.util.mark_negation(text) 
	print('\n**Sentiment with Negative words**\n')

When we run the above program we get the following output −

 ** Sentiment with one word **

{'contains(luck)': False, 'contains(good)': True, 'contains(progress)': True}

*** Sentiment with bigrams ***

{'contains(fit - fine)': False, 'contains(Regular - fit)': False}

**Sentiment with Negative words**

['Lack', 'of', 'good', 'health', 'can', 'not', 'bring_NEG', 'success_NEG', 'to_NEG', 'students_NEG']