-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
246 lines (171 loc) · 8.22 KB
/
app.py
File metadata and controls
246 lines (171 loc) · 8.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import pandas as pd
import numpy as np
import streamlit as st
import joblib
from PIL import Image
import pickle
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
import re
import string
import matplotlib.pyplot as plt
import time
@st.cache(allow_output_mutation=True)
def load(vectoriser_path, model_path):
# Load the vectoriser.
file = open(vectoriser_path, 'rb')
vectoriser = pickle.load(file)
file.close()
# Load the LR Model.
file = open(model_path, 'rb')
LRmodel = pickle.load(file)
file.close()
return vectoriser, LRmodel
def inference(vectoriser, model, tweets, cols):
# if uploaded_file:
# data = pd.read_excel(uploaded_file)
# st.dataframe(data)
text = tweets.split(";")
finaldata = []
textdata = vectoriser.transform(lemmatize_process(preprocess(text)))
sentiment = model.predict(textdata)
# print(model.classes_)
sentiment_prob = model.predict_proba(textdata)
for index,tweet in enumerate(text):
if sentiment[index] == 1:
sentiment_probFinal = sentiment_prob[index][1]
else:
sentiment_probFinal = sentiment_prob[index][0]
sentiment_probFinal2 = "{}%".format(round(sentiment_probFinal*100,2))
finaldata.append((tweet, sentiment[index], sentiment_probFinal2))
# Convert the list into a Pandas DataFrame.
df = pd.DataFrame(finaldata, columns = ['Tweet','Sentiment', 'Probability(Confidence Level)'])
df = df.replace([0,1], ["Negative","Positive"])
return df
# Defining a method to return the 2nd parameter for lemmatization that is POS tag
def get_wordnet_pos_tag(tag):
if tag.startswith("J"):
return wordnet.ADJ
if tag.startswith("V"):
return wordnet.VERB
if tag.startswith("N"):
return wordnet.NOUN
if tag.startswith("R"):
return wordnet.ADV
else:
return wordnet.NOUN
# Created a method to perform lemmatization with POS tags identified via a pos_tag method
def lemmatize_process(preprocessedtext):
# Create Lemmatizer
lemma = WordNetLemmatizer()
finalprocessedtext = []
for tweet in preprocessedtext:
text_pos = pos_tag(word_tokenize(tweet))
words = [x[0] for x in text_pos]
pos = [x[1] for x in text_pos]
tweet_lemma = " ".join([lemma.lemmatize(a,get_wordnet_pos_tag(b)) for a,b in zip(words,pos)])
finalprocessedtext.append(tweet_lemma)
return finalprocessedtext
def plot(df):
positive = round(np.count_nonzero(df['Sentiment'] == "Positive")/len(df['Sentiment'])*100,2)
negative = round(np.count_nonzero(df['Sentiment'] == "Negative")/len(df['Sentiment'])*100,2)
labels = ['Positive','Negative']
values = np.array([positive,negative])
myexplode = [0.2, 0]
mycolors = ["green", "red"]
fig,ax = plt.subplots()
ax.pie(values, labels = labels, explode = myexplode, shadow = True, colors = mycolors)
ax.legend()
ax.set_title("Positive vs Negative Tweet(%)")
st.pyplot(fig)
def preprocess(textdata):
# Defining dictionary containing all emojis with their meanings.
emojis = {':)': 'smile', ':-)': 'smile', ';d': 'wink', ':-E': 'vampire', ':(': 'sad',
':-(': 'sad', ':-<': 'sad', ':P': 'raspberry', ':O': 'surprised',
':-@': 'shocked', ':@': 'shocked',':-$': 'confused', ':\\': 'annoyed',
':#': 'mute', ':X': 'mute', ':^)': 'smile', ':-&': 'confused', '$_$': 'greedy',
'@@': 'eyeroll', ':-!': 'confused', ':-D': 'smile', ':-0': 'yell', 'O.o': 'confused',
'<(-_-)>': 'robot', 'd[-_-]b': 'dj', ":'-)": 'sadsmile', ';)': 'wink',
';-)': 'wink', 'O:-)': 'angel','O*-)': 'angel','(:-D': 'gossip', '=^.^=': 'cat'}
stopwordlist = ['a', 'about', 'above', 'after', 'again', 'ain', 'all', 'am', 'an',
'and','any','are', 'as', 'at', 'be', 'because', 'been', 'before',
'being', 'below', 'between','both', 'by', 'can', 'd', 'did', 'do',
'does', 'doing', 'down', 'during', 'each','few', 'for', 'from',
'further', 'had', 'has', 'have', 'having', 'he', 'her', 'here',
'hers', 'herself', 'him', 'himself', 'his', 'how', 'i', 'if', 'in',
'into','is', 'it', 'its', 'itself', 'just', 'll', 'm', 'ma',
'me', 'more', 'most','my', 'myself', 'now', 'o', 'of', 'on', 'once',
'only', 'or', 'other', 'our', 'ours','ourselves', 'out', 'own', 're',
's', 'same', 'she', "shes", 'should', "shouldve",'so', 'some', 'such',
't', 'than', 'that', "thatll", 'the', 'their', 'theirs', 'them',
'themselves', 'then', 'there', 'these', 'they', 'this', 'those',
'through', 'to', 'too','under', 'until', 'up', 've', 'very', 'was',
'we', 'were', 'what', 'when', 'where','which','while', 'who', 'whom',
'why', 'will', 'with', 'won', 'y', 'you', "youd","youll", "youre",
"youve", 'your', 'yours', 'yourself', 'yourselves']
processedText = []
# Create Lemmatizer
wordLemm = WordNetLemmatizer()
# Defining regex patterns.
urlPattern = r"((http://)[^ ]*|(https://)[^ ]*|( www\.)[^ ]*)"
userPattern = '@[^\s]+'
# alphaPattern = "[^a-zA-Z0-9]"
alphaPattern = "[^a-zA-Z]"
sequencePattern = r"(.)\1\1+"
seqReplacePattern = r"\1\1"
for tweet in textdata:
tweet = tweet.lower()
# Replace all URls with 'URL'
tweet = re.sub(urlPattern,' URL',tweet)
# Replace all emojis.
for emoji in emojis.keys():
tweet = tweet.replace(emoji, "EMOJI" + emojis[emoji])
# Replace @USERNAME to 'USER'.
tweet = re.sub(userPattern,' USER', tweet)
# Replace all non alphabets.
tweet = re.sub(alphaPattern, " ", tweet)
# Replace 3 or more consecutive letters by 2 letter.
tweet = re.sub(sequencePattern, seqReplacePattern, tweet)
#Removing punctuations if any left post removing all all non alphabets
all_char_list = []
all_char_list = [char for char in tweet if char not in string.punctuation]
tweet = ''.join(all_char_list)
# Removing all stopwords as per custom list defined above
tweetwords = ''
for word in tweet.split():
if word not in (stopwordlist):
if len(word)>1:
# Lemmatizing the word.
# text_pos = pos_tag(word_tokenize(word))
# word = lemma.lemmatize(text_pos[0][0],get_wordnet_pos_tag(text_pos[0][1]))
# word = wordLemm.lemmatize(word)
tweetwords += (word+' ')
processedText.append(tweetwords)
return processedText
def progressbar():
my_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.01)
my_bar.progress(percent_complete + 1)
st.title('Sentiment Analysis App')
st.write('Performing Sentiment Analysis')
image = Image.open('data/sentiment.jpg')
st.image(image, use_column_width=True)
# st.write('Enter some random tweets in the left sidebar and click on Predict Sentiment!')
# uploaded_file = st.sidebar.file_uploader("Choose a csv file", type="csv")
# st.sidebar.write("or")
st.sidebar.subheader("Enter single/multiple tweets separated by semicolon : ")
tweets = st.sidebar.text_area("Some samples are provided below for reference..", value="I hate twitter;I do not like the movie;Mr. Stark, I don't feel so good;May the Force be with you.;I read the book, the content is not good;This is a new beginning for us", height=500, max_chars=None, key=None)
cols = ["tweet"]
if (st.sidebar.button('Predict Sentiment')):
progressbar()
vectoriser, model = load('vectoriser.pickle', 'Sentiment-LR.pickle')
result_df = inference(vectoriser, model, tweets, cols)
st.table(result_df)
st.text("")
st.text("")
st.text("")
plot(result_df)
# data = pd.read_csv("data/real_data.csv", encoding = "ISO-8859-1")