Python Text Processing Useful Resources

Python Text Processing - Sentiment Analysis



Sentiment 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.

Example - Sentiment Analysis

main.py

import nltk
from nltk.sentiment.util import extract_unigram_feats
from nltk.sentiment.util import extract_bigram_feats
from nltk.sentiment.util import mark_negation

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

# Analysing for a pair of words	
def WithBigrams(): 
   word_sets = [('Regular', 'fit'), ('fit', 'fine')] 
   text = 'Regular excercise makes you fit and fine'.split() 
   analysis = 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 = mark_negation(text) 
   print('\n**Sentiment with Negative words**\n')
   print(analysis) 
    
OneWord()
WithBigrams() 
NegativeWord() 

Output

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']
Advertisements