File size: 1,463 Bytes
2ed7f35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3c28f
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
import streamlit as st
import pickle
import re
from sklearn.feature_extraction.text import  CountVectorizer

with open('count_vectorizer.pkl','rb')as vectorizer_file:
    count_vectorizer = pickle.load(vectorizer_file)

with open('nb_classifier.pkl','rb')as classifier_file:
    nb_classifier = pickle.load(classifier_file)

def process_text(text):
    text = text.lower()
    text = re.sub(r'http\S+', '', text)
    text = re.sub(r'@[a-zA-Z0-9_]+', '', text)
    text = re.sub(r'#', '', text)
    text = re.sub(r'[^a-zA-Z\s]', '', text)
    return text

sentiment_mapping = {
        "Negative" : "Negative πŸ˜”",
        "Positive" : "Positive 😊",
        "Neutral" : "Neutral πŸ™„",
        "Irrelevant" : "Irrelevant πŸ€·β€β™‚οΈ"
    }

def main():
    col1 , col2 , col3 ,col4 = st.columns([1,1,3,1])
    with col3:
        st.image("./pngwing.com (1).png" , width=100)
    st.title("Twitter Sentiment Classifier")
    st.write("Enter twitter tweet below :")
    input_text = st.text_area("Input Text :","")
    if st.button("Predict"):
        cleaned_text = process_text(input_text)
        vectorizer_text = count_vectorizer.transform([cleaned_text])
        sentiment_prediction = nb_classifier.predict(vectorizer_text)[0]
        
        predicted_sentiment =  sentiment_mapping.get(sentiment_prediction , "Unknown Sentiment")

        st.write("Predicted Sentimen :")
        st.title(predicted_sentiment)


if __name__  == "__main__":
    main()