
- Python - Text Processing
- Python - Text Processing Introduction
- Python - Text Processing Environment
- Python - String Immutability
- Python - Sorting Lines
- Python - Reformatting Paragraphs
- Python - Counting Token in Paragraphs
- Python - Binary ASCII Conversion
- Python - Strings as Files
- Python - Backward File Reading
- Python - Filter Duplicate Words
- Python - Extract Emails from Text
- Python - Extract URL from Text
- Python - Pretty Print
- Python - Text Processing State Machine
- Python - Capitalize and Translate
- Python - Tokenization
- Python - Remove Stopwords
- Python - Synonyms and Antonyms
- Python - Text Translation
- Python - Word Replacement
- Python - Spelling Check
- Python - WordNet Interface
- Python - Corpora Access
- Python - Tagging Words
- Python - Chunks and Chinks
- Python - Chunk Classification
- Python - Text Classification
- Python - Bigrams
- Python - Process PDF
- Python - Process Word Document
- Python - Reading RSS feed
- Python - Sentiment Analysis
- Python - Search and Match
- Python - Text Munging
- Python - Text wrapping
- Python - Frequency Distribution
- Python - Text Summarization
- Python - Stemming Algorithms
- Python - Constrained Search
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') 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 = 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') print(analysis) OneWord() WithBigrams() NegativeWord()
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