-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
54 lines (44 loc) · 1.38 KB
/
logic.py
File metadata and controls
54 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import matplotlib.pyplot as plt
import string
import csv
import os
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import matplotlib
matplotlib.use('Agg')
def sentiment_analyze(input_text):
text = input_text
lower_text = text.lower()
cleaned_text = lower_text.translate(
str.maketrans('', '', string.punctuation))
tokenized_words = word_tokenize(cleaned_text, "english")
final_words = ''
for word in tokenized_words:
if word not in stopwords.words('english'):
final_words += word + ' '
score = SentimentIntensityAnalyzer().polarity_scores(input_text)
print(score)
emotions = ['Positive', 'Negative', 'Neutral', 'Compound']
scores = [score['pos'], score['neg'], score['neu'], abs(score['compound'])]
positions = [0, 1, 2, 3]
plt.bar(positions, scores, width=0.5)
plt.xticks(positions, emotions)
loc = 'static/images/graph.png'
if os.path.isfile(loc):
os.remove(loc)
plt.savefig(loc)
plt.show()
plt.close()
neg = score['neg']
pos = score['pos']
if neg > pos:
print('neg')
return "Negative Sentiment"
elif pos > neg:
print('pos')
return "Positive Sentiment"
else:
print('neu')
return "Neutral Vibe"
# sentiment_analyze('I am really hungry and sad')