Skip to content

Instantly share code, notes, and snippets.

@abhisheksoni27
Created July 6, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhisheksoni27/dcf39313384cdf7a953a7c1f21ec5099 to your computer and use it in GitHub Desktop.
Save abhisheksoni27/dcf39313384cdf7a953a7c1f21ec5099 to your computer and use it in GitHub Desktop.
import sys
import re
import matplotlib.pyplot as plt
import nltk
from utilities import cleanText
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sentiment_analyzer = SentimentIntensityAnalyzer() # Our Great Sentiment Analyzer
def analyze(name):
linesList = cleanText(name + '.txt')
neutral, negative, positive = 0, 0, 0
for index, sentence in enumerate(linesList):
print("Processing {0}%".format(str((index * 100) / len(linesList))))
# Ignore Emoji
if re.match(r'^[\w]', sentence):
continue
scores = sentiment_analyzer.polarity_scores(sentence)
# We don't need that component
scores.pop('compound', None)
maxAttribute = max(scores, key=lambda k: scores[k])
if maxAttribute == "neu":
neutral += 1
elif maxAttribute == "neg":
negative += 1
else:
positive += 1
total = neutral + negative + positive
print("Negative: {0}% | Neutral: {1}% | Positive: {2}%".format(
negative*100/total, neutral*100/total, positive*100/total))
# Plot
#### Code Omitted ####
analyze(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment