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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|