performing Sentiment Analysis on the downloaded tweets.
After downloading the tweets in a file select those tweets on which you want to perform Sentiment Analysis.
The below code shows step by step process to perform Sentiment Analysis.
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
from nltk.sentiment.util import *
n_instances = 100
subj_docs = [(sent,'subj') for sent in subjectivity.sents(categories='subj')[:n_instances]]
obj_docs = [(sent, 'obj') for sent in subjectivity.sents(categories='obj')[:n_instances]]
train_subj_docs = subj_docs[:80]
test_subj_docs = subj_docs[80:100]
train_obj_docs = obj_docs[:80]
test_obj_docs = obj_docs[80:100]
training_docs = train_subj_docs+train_obj_docs
testing_docs = test_subj_docs+test_obj_docs
sentim_analyzer = SentimentAnalyzer()
all_words_neg = sentim_analyzer.all_words([mark_negation(doc) for doc in training_docs])
unigram_feats = sentim_analyzer.unigram_word_feats(all_words_neg,min_freq=4)
sentim_analyzer.add_feat_extractor(extract_unigram_feats, unigrams=unigram_feats)
training_set = sentim_analyzer.apply_features(training_docs)
test_set = sentim_analyzer.apply_features(testing_docs)
trainer = NaiveBayesClassifier.train
classifier = sentim_analyzer.train(trainer, training_set)
for key,value in sorted(sentim_analyzer.evaluate(test_set).items()):
print('{0}: {1}'.format(key, value))
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sentences = [ "The book was kind of good.",
"I love coding",
"Sentiment analysis is so enjoying",
"my project is too good"
]
paragraph = "It was one of the worst movies I've seen, despite good reviews. \
unbelievably bad acting!! Poor direction. VERY poor production. \
The movie was bad. Very bad movie."
from nltk import tokenize
tokenize.sent_tokenize(paragraph)
lines_list = sentences.extend(lines_list)
tricky_tweets = ["RT @vinaydokania: This is not jus d victory of @harishrawatcmuk\
or @INCIndia This UK HC judgement is d victory of the people of Uttarakhand",
"Those @INCIndia morons celebrating HC verdict on UK government crisis not aware that with floor \
test BJP will form next",
"@TimesNow @rssurjewala first u n ur @INCIndia party tender an unconditional apology for \
compromising d national security n sin fr what u did",
"When any thing goes against @BJP4India u comment but when against @INCIndia u just \
disappear@ArvindKejriwal why so? ",
"@harishrawatcmuk would continue as the CM Congress needs 31 MLAs to prove majority and we have \
33 in house#HCexposesBJPinUkhand @INCIndia",
"RT @OfficeOfRG: I assured them that @INCIndia is fully committed to a strong Real Estate Bill \
to protect them",
"RT @shall_isaytruth: @INCIndia is a anti Hindu party! So they got only minority vote &\
reduced to a minority party 44 in LS #CongressVsH",
"@timesofindia bribe tu de harish aur galliyan Modiji khaaye !! Shameless @INCIndia",
"@INCIndia @dashingassu You are known by the company you keep said my dad and he is so right",
"RT @OfficeOfRG: Proud that @INCIndia stood alongside Delta's family & fought hard to put\
pressure on the Rajasthan Govt",
"@PardhiVijendra @htTweets @INCIndia : Ask Modi to bring back block money know ..\
Where is my 15Laks ?",
"RT @DrGPradhan: yesterday or day before I tweeted that TDK spoke to @bdutt and asked to change news focus.\
Today Horse DEAD?\n\n@INCIndia",
"Nainital HC verdict a big slap on centra govt\nCongress returns in Uttarakhand\
Congrats on democracy restoring \n@INCIndia"
]
sentences.extend(tricky_tweets)
sid = SentimentIntensityAnalyzer()
for sentence in sentences:
print(sentence)
ss = sid.polarity_scores(sentence)
for k in sorted(ss):
print '{0}: {1}, '.format(k, ss[k]),
print "\n"
observe the output:
The book was kind of good.
compound: 0.3832, neg: 0.0, neu: 0.657, pos: 0.343,
I love coding
compound: 0.6369, neg: 0.0, neu: 0.192, pos: 0.808,
It was one of the worst movies I've seen, despite good reviews.
compound: -0.7584, neg: 0.394, neu: 0.606, pos: 0.0,
unbelievably bad acting!!
compound: -0.6572, neg: 0.686, neu: 0.314, pos: 0.0,
Those @INCIndia morons celebrating HC verdict on UK government crisis not aware that with floor test BJP will form next
compound: -0.2732, neg: 0.231, neu: 0.578, pos: 0.191,
RT @OfficeOfRG: I assured them that @INCIndia is fully committed to a strong Real Estate Bill to protect them
compound: 0.8766, neg: 0.0, neu: 0.54, pos: 0.46,
@timesofindia bribe tu de harish aur galliyan Modiji khaaye !! Shameless @INCIndia
compound: -0.5837, neg: 0.324, neu: 0.676, pos: 0.0,
Nainital HC verdict a big slap on centra govt
Congress returns in UttarakhandCongrats on democracy restoring
@INCIndia
compound: 0.5267, neg: 0.0, neu: 0.707, pos: 0.293,
note:
- paste the selected tweets in the code.
- there are some sample sentences to check the expected results.
No comments:
Post a Comment