I have a dataframe in which every row is a text in which I would like to implement sentiment analysis with positive or negative results.

I made the appropriate cleaning to the text removing the stopwords, stemmining, punctuation, lower case letters etc.

Is there any simple example from where I could start to take a positive or negative results?


Most probably this answer is way too late after looking at the date of the OP's question but for others who might stumble here, I am also a beginner in sentiment analysis, but found this link to be quite a good simple start. The example is in Python but others exist for other languages as well.

EDIT: to add relevant information from the linked page:

The example uses training and prediction to analyse sentences. Each sentence is converted into words and each word is then converted into a feature and then tokenised.

This is the whole code with minor changes:

import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import names

def word_feats(words):
    return dict([(word, True) for word in words])

# define vocabularies
positive_vocab = [ the list of all your positive words ]
negative_vocab = [ the list of all your negative words ]
neutral_vocab = [ the list of all your neutral words ]

# convert each word into features
positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab]
negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab]
neutral_features = [(word_feats(neu), 'neu') for neu in neutral_vocab]

train_set = negative_features + positive_features + neutral_features

# train the classifier
classifier = NaiveBayesClassifier.train(train_set) 

# Predict
neg = 0
pos = 0
sentence = "In another life, you should be an aspiring poet or a martyr"
sentence = sentence.lower()
words = sentence.split(' ')
for word in words:
    classResult = classifier.classify( word_feats(word))
    if classResult == 'neg':
        neg = neg + 1
    if classResult == 'pos':
        pos = pos + 1

print('Positive: ' + str(float(pos)/len(words)))
print('Negative: ' + str(float(neg)/len(words)))
| cite | improve this answer | |
  • 1
    $\begingroup$ Welcome to our site! We are wary of link-only answers because they will have no value if the link "rots" (the URL changes or the material is removed from the web altogether). Do you think you could give a brief summary of what the link is saying? Alternatively we can convert this into a comment for you $\endgroup$ – Silverfish Apr 21 '17 at 11:21
  • $\begingroup$ Added code and some information. Hope that is ok. $\endgroup$ – salvu Apr 21 '17 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.