Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negation handling in sentiment analysis

I am in need of a little help here, I need to identify the negative words like "not good","not bad" and then identify the polarity (negative or positive) of the sentiment. I did everything except handling the negations. I just want to know how I can include negations into it. How do I go about it?

like image 727
user1565960 Avatar asked Mar 31 '15 16:03

user1565960


People also ask

What is negation handling in sentiment analysis?

Negation handling is a method of automatically detecting the extent of negation and inverting the polarity of opinionated words that are impacted by a negation. The area of the phrase that negation impacts are referred to as the vicinity or scope of negation.

What is negation in NLP?

Negation handling is an important sub-task in sentiment analysis in natural language processing (NLP) and considered as one of the hardest problem in NLP in opinion mining. Negation handling in NLP deals with automated detection of polarity shift in opinion expressed in natural lan- guage text format.

What is the effect of negation?

One of the effects that negation can have is that it forces the reader/listener to first think of the thing that is being negated, before not thinking about it. In other words: you can't not think about something before thinking about it first!


1 Answers

Negation handling is quite a broad field, with numerous different potential implementations. Here I can provide sample code that negates a sequence of text and stores negated uni/bi/trigrams in not_ form. Note that nltk isn't used here in favor of simple text processing.

# negate_sequence(text)
#   text: sentence to process (creation of uni/bi/trigrams
#    is handled here)
#
# Detects negations and transforms negated words into 'not_' form
#
def negate_sequence(text):
    negation = False
    delims = "?.,!:;"
    result = []
    words = text.split()
    prev = None
    pprev = None
    for word in words:
        stripped = word.strip(delims).lower()
        negated = "not_" + stripped if negation else stripped
        result.append(negated)
        if prev:
            bigram = prev + " " + negated
            result.append(bigram)
            if pprev:
                trigram = pprev + " " + bigram
                result.append(trigram)
            pprev = prev
        prev = negated

        if any(neg in word for neg in ["not", "n't", "no"]):
            negation = not negation

        if any(c in word for c in delims):
            negation = False

    return result

If we run this program on a sample input text = "I am not happy today, and I am not feeling well", we obtain the following sequences of unigrams, bigrams, and trigrams:

[   'i',
    'am',
    'i am',
    'not',
    'am not',
    'i am not',
    'not_happy',
    'not not_happy',
    'am not not_happy',
    'not_today',
    'not_happy not_today',
    'not not_happy not_today',
    'and',
    'not_today and',
    'not_happy not_today and',
    'i',
    'and i',
    'not_today and i',
    'am',
    'i am',
    'and i am',
    'not',
    'am not',
    'i am not',
    'not_feeling',
    'not not_feeling',
    'am not not_feeling',
    'not_well',
    'not_feeling not_well',
    'not not_feeling not_well']

We may subsequently store these trigrams in an array for future retreival and analysis. Process the not_ words as negative of the [sentiment, polarity] that you have defined for their counterparts.

like image 190
manan Avatar answered Sep 18 '22 20:09

manan