Raghavendra0827 commited on
Commit
9f48ffe
Β·
verified Β·
1 Parent(s): 42a5278

Upload 3 files

Browse files
Files changed (3) hide show
  1. Sentiment.py +281 -0
  2. requirements.txt +6 -0
  3. sentimentdataset.csv +0 -0
Sentiment.py ADDED
@@ -0,0 +1,281 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[41]:
5
+
6
+
7
+ import pandas as pd
8
+ import nltk
9
+ nltk.download('stopwords')
10
+ nltk.download('punkt')
11
+ import numpy as np
12
+ from nltk.tokenize import word_tokenize
13
+ from nltk.corpus import stopwords
14
+
15
+ stop_words = set(stopwords.words("english"))
16
+
17
+
18
+ # In[42]:
19
+
20
+
21
+ data = pd.read_csv("sentimentdataset.csv")
22
+ data.drop(columns = [i for i in data.columns if i not in ["Text","Sentiment"]], inplace = True)
23
+
24
+
25
+ # In[ ]:
26
+
27
+
28
+ def extract_words(sentence):
29
+ cleaned_text = [w.lower() for w in word_tokenize(sentence) if w.lower() not in stop_words]
30
+ return cleaned_text
31
+
32
+
33
+ # In[ ]:
34
+
35
+
36
+ def vocab(corpus):
37
+ vocabulary = []
38
+
39
+ for doc in corpus:
40
+ words = extract_words(doc)
41
+ vocabulary.extend(words)
42
+
43
+ vocabulary = sorted(list(set(vocabulary)))
44
+
45
+ return vocabulary
46
+
47
+
48
+ # In[ ]:
49
+
50
+
51
+ def bow(sentence, vocabulary):
52
+ words = extract_words(sentence)
53
+ bag = np.zeros(len(vocabulary))
54
+ for word in words:
55
+ for i, vocab in enumerate(vocabulary):
56
+ if vocab == word:
57
+ bag[i] += 1
58
+ return bag
59
+
60
+
61
+ # In[ ]:
62
+
63
+
64
+ vocabulary = vocab(data.Text.to_list())
65
+
66
+
67
+ # In[ ]:
68
+
69
+
70
+ arrays = np.empty((0, len(vocabulary)), int)
71
+ for val in data.Text.to_list():
72
+
73
+ bow_representation = bow(val, vocabulary)
74
+ arrays = np.append(arrays, [bow_representation], axis=0)
75
+
76
+
77
+
78
+ # In[ ]:
79
+
80
+
81
+ from sklearn.preprocessing import LabelEncoder
82
+ label_encoder = LabelEncoder()
83
+ data['Encoded_Sentiment'] = label_encoder.fit_transform(data['Sentiment'])
84
+
85
+
86
+ # In[ ]:
87
+
88
+
89
+ print("Mapping of original labels to encoded labels:")
90
+ for original_label, encoded_label in zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)):
91
+ print(f"{original_label}: {encoded_label}")
92
+
93
+
94
+ # In[ ]:
95
+
96
+
97
+ X = arrays
98
+ y = data['Encoded_Sentiment']
99
+
100
+ from sklearn.ensemble import RandomForestClassifier
101
+ from sklearn.model_selection import train_test_split
102
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
103
+
104
+ # Split the data into training and testing sets
105
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
106
+
107
+ # Initialize the Random Forest model
108
+ rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)
109
+
110
+ # Train the model on the training set
111
+ rf_classifier.fit(X, y)
112
+
113
+ Labels = dict(zip(label_encoder.transform(label_encoder.classes_),label_encoder.classes_))
114
+
115
+ def pred(text):
116
+ bag = bow(text, vocabulary)
117
+ input_array = np.array([bag])
118
+ y_pred = rf_classifier.predict(input_array)
119
+ return y_pred
120
+
121
+ # inputt = input("Enter the Text input: ")
122
+ # predicted_label = pred(inputt)[0]
123
+ # print("Predicted Label:", Labels[predicted_label])
124
+
125
+
126
+ # In[ ]:
127
+
128
+
129
+ import streamlit as st
130
+
131
+
132
+ # In[43]:
133
+ emojis = {
134
+ ' Acceptance ': '😊',
135
+ ' Acceptance ': '😊',
136
+ ' Accomplishment ': 'πŸ†',
137
+ ' Admiration ': '😍',
138
+ ' Admiration ': '😍',
139
+ ' Admiration ': '😍',
140
+ ' Adoration ': '😍',
141
+ ' Adrenaline ': '🀯',
142
+ ' Adventure ': 'πŸš€',
143
+ ' Affection ': '❀️',
144
+ ' Amazement ': '😲',
145
+ ' Ambivalence ': '😐',
146
+ ' Ambivalence ': '😐',
147
+ ' Amusement ': 'πŸ˜„',
148
+ ' Amusement ': 'πŸ˜„',
149
+ ' Anger ': '😑',
150
+ ' Anticipation ': '😬',
151
+ ' Anticipation ': '😬',
152
+ ' Anxiety ': '😰',
153
+ ' Anxiety ': '😰',
154
+ ' Appreciation ': 'πŸ™',
155
+ ' Apprehensive ': '😟',
156
+ ' Arousal ': '😏',
157
+ ' ArtisticBurst ': '🎨',
158
+ ' Awe ': '😲',
159
+ ' Awe ': '😲',
160
+ ' Awe ': '😲',
161
+ ' Awe ': '😲',
162
+ ' Bad ': '😞',
163
+ ' Betrayal ': 'πŸ˜”',
164
+ ' Betrayal ': 'πŸ˜”',
165
+ ' Bitter ': 'πŸ˜–',
166
+ ' Bitterness ': 'πŸ˜–',
167
+ ' Bittersweet ': 'πŸ˜”πŸ˜Š',
168
+ ' Blessed ': 'πŸ™',
169
+ ' Boredom ': 'πŸ˜‘',
170
+ ' Boredom ': 'πŸ˜‘',
171
+ ' Breakthrough ': 'πŸŽ‰',
172
+ ' Calmness ': '😌',
173
+ ' Calmness ': '😌',
174
+ ' Captivation ': '😍',
175
+ ' Celebration ': 'πŸŽ‰',
176
+ ' Celestial Wonder ': '🌟',
177
+ ' Challenge ': 'πŸ’ͺ',
178
+ ' Charm ': '😊',
179
+ ' Colorful ': '🎨',
180
+ ' Compassion': '❀️',
181
+ ' Compassion ': '❀️',
182
+ ' Compassionate ': '❀️',
183
+ ' Confidence ': '😎',
184
+ ' Confident ': '😎',
185
+ ' Confusion ': 'πŸ˜•',
186
+ ' Confusion ': 'πŸ˜•',
187
+ ' Confusion ': 'πŸ˜•',
188
+ ' Connection ': 'πŸ’‘',
189
+ ' Contemplation ': 'πŸ€”',
190
+ ' Contentment ': '😊',
191
+ ' Contentment ': '😊',
192
+ ' Coziness ': '🏠',
193
+ ' Creative Inspiration ': '🎨',
194
+ ' Creativity ': '🎨',
195
+ ' Creativity ': '🎨',
196
+ ' Culinary Adventure ': '🍽️',
197
+ ' CulinaryOdyssey ': '🍽️',
198
+ ' Curiosity ': 'πŸ€”',
199
+ ' Curiosity ': 'πŸ€”',
200
+ ' Curiosity ': 'πŸ€”',
201
+ ' Curiosity ': 'πŸ€”',
202
+ ' Curiosity ': 'πŸ€”',
203
+ ' Darkness ': 'πŸŒ‘',
204
+ ' Dazzle ': '✨',
205
+ ' Desolation ': '😞',
206
+ ' Despair ': '😒',
207
+ ' Despair ': '😒',
208
+ ' Despair ': '😒',
209
+ ' Despair ': '😒',
210
+ ' Desperation ': '😟',
211
+ ' Determination ': 'πŸ’ͺ',
212
+ ' Determination ': 'πŸ’ͺ',
213
+ ' Devastated ': '😭',
214
+ ' Disappointed ': '😞',
215
+ ' Disappointment ': '😞',
216
+ ' Disgust ': '🀒',
217
+ ' Disgust ': '🀒',
218
+ ' Disgust ': '🀒',
219
+ ' Dismissive ': 'πŸ˜’',
220
+ ' DreamChaser ': 'πŸš€',
221
+ ' Ecstasy ': 'πŸ˜†',
222
+ ' Elation ': 'πŸ˜†',
223
+ ' Elation ': 'πŸ˜†',
224
+ ' Elegance ': 'πŸ’ƒ',
225
+ ' Embarrassed ': '😳',
226
+ ' Emotion ': 'πŸ˜„',
227
+ ' EmotionalStorm ': '😱',
228
+ ' Empathetic ': '❀️',
229
+ ' Empowerment ': 'πŸ’ͺ',
230
+ ' Enchantment ': '😍',
231
+ ' Enchantment ': '😍',
232
+ ' Energy ': '⚑',
233
+ ' Engagement ': 'πŸ’',
234
+ ' Enjoyment ': '😊',
235
+ ' Enthusiasm ': 'πŸ˜ƒ',
236
+ ' Enthusiasm ': 'πŸ˜ƒ',
237
+ ' Envious ': '😠',
238
+ ' Envisioning History ': '🏰',
239
+ ' Envy ': '😠',
240
+ ' Euphoria ': '😁',
241
+ ' Euphoria ': '😁',
242
+ ' Euphoria ': '😁',
243
+ ' Euphoria ': '😁',
244
+ ' Excitement ': 'πŸ˜ƒ',
245
+ ' Excitement ': 'πŸ˜ƒ',
246
+ ' Excitement ': 'πŸ˜ƒ',
247
+ ' Exhaustion ': '😩',
248
+ ' Exploration ': '🌍',
249
+ ' Fear ': '😨',
250
+ ' Fearful ': '😨',
251
+ ' FestiveJoy ': 'πŸŽ‰',
252
+ ' Free-spirited ': 'πŸ¦‹',
253
+ ' Freedom ': 'πŸ—½',
254
+ ' Friendship ': 'πŸ‘«',
255
+ ' Frustrated ': '😀',
256
+ ' Frustration ': '😀',
257
+ ' Frustration ': '😀',
258
+ ' Fulfillment ': '😊',
259
+ ' Fulfillment ': '😊'}
260
+
261
+ def main():
262
+ st.title("Text Sentiment Classifier")
263
+ selected_text = st.selectbox("Select Text", data['Text'].tolist())
264
+ if st.button("Predict"):
265
+ predicted_label = pred(selected_text)[0]
266
+ try:
267
+ st.write("Predicted Sentiment:", Labels[predicted_label], emojis[Labels[predicted_label]])
268
+ except:
269
+ st.write("Predicted Sentiment:", Labels[predicted_label])
270
+
271
+
272
+ if __name__ == "__main__":
273
+ main()
274
+
275
+
276
+ # In[ ]:
277
+
278
+
279
+
280
+
281
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ nltk==3.6.3
2
+ numpy==1.21.2
3
+ pandas==1.3.3
4
+ scikit-learn==0.24.2
5
+ streamlit==1.3.0
6
+
sentimentdataset.csv ADDED
The diff for this file is too large to render. See raw diff