python logo

sentiment analysis python


Python hosting: Host, run, and code Python in the cloud!

In the realm of Natural Language Processing (NLP), Sentiment Analysis stands as a crucial technique.

When given a movie review or a tweet, it’s possible to automatically categorize the content into predefined categories such as positive or negative. Additionally, you have the flexibility to define your own categories.

Sentiment Analysis Flow

Recommended Courses:

How Does Sentiment Analysis Work?


The process of Sentiment Analysis involves two main stages: training and prediction.

For the training phase, we require a dataset containing example sentiments. Using this training data, the classifier learns to predict the sentiment of new, unseen data.

Sentiment Analysis Process

The foundation lies in defining three primary classes: positive, negative, and neutral. These classes are represented by specific vocabularies:

1
2
3
positive_vocab = [ 'awesome', 'outstanding', 'fantastic', 'terrific', 'good', 'nice', 'great', ':)' ]
negative_vocab = [ 'bad', 'terrible','useless', 'hate', ':(' ]
neutral_vocab = [ 'movie','the','sound','was','is','actors','did','know','words','not' ]

In the next step, each word is transformed into a feature using a bag of words model:

1
2
3
4
5
6
def word_feats(words):
return dict([(word, True) for word in words])

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]

Following this, we combine these three feature sets to form our training dataset:

1
train_set = negative_features + positive_features + neutral_features

With the training set ready, we proceed to train our classifier:

1
classifier = NaiveBayesClassifier.train(train_set) 

Finally, we utilize the trained classifier to make predictions on new data.

Sample Python Code for Sentiment Analysis:
This sample demonstrates how sentences are classified based on the provided training set.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import names

# ... [rest of the code from above] ...

# Prediction
neg = 0
pos = 0
sentence = "Awesome movie, I liked it"
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)))

If you wish to enter the input sentence manually, make use of the input or raw_input functions. Remember, the efficiency of your sentiment analysis largely depends on the quality and volume of your training data.

Available Training Datasets:
To achieve optimal results, consider leveraging comprehensive training datasets. Some notable options include:

The efficacy of your sentiment classifier greatly hinges on the quality of the dataset you use.

Back to Natural Language Processing Prediction





Leave a Reply: