File size: 3,012 Bytes
9cd5782
 
 
 
 
 
09f0e17
 
 
 
 
 
 
 
 
9cd5782
09f0e17
9cd5782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09f0e17
 
9cd5782
 
09f0e17
9cd5782
 
 
 
09f0e17
 
 
 
 
 
 
9cd5782
 
 
 
 
 
09f0e17
9cd5782
 
 
 
09f0e17
 
 
 
bca9064
09f0e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bca9064
09f0e17
9cd5782
09f0e17
 
 
9cd5782
09f0e17
 
 
 
 
 
 
 
 
 
 
 
9cd5782
09f0e17
 
9cd5782
09f0e17
9cd5782
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
import os
import pandas as pd
import numpy as np
import streamlit as st
import re
import pickle
import nltk
import string
import contractions

from nltk.corpus import stopwords

nltk.download("stopwords")
sw_list = stopwords.words('english')

def remove_tags(text):
    return re.sub(re.compile('<.*?>'), '', text)

def lwr(text):
    return text.lower()

def stopword(text):
    return " ".join([word for word in text.split() if word not in sw_list])

def remove_punctuation(text):
    return text.translate(str.maketrans('', '', string.punctuation))

def remove_contractions(text):
    return contractions.fix(text)

def dec_vector(doc):
    with open("Sentimental_Analysis_WV.pkl", 'rb') as file:  
        model = pickle.load(file)
    doc = [word for word in doc.split() if word in model.wv.index_to_key]
    return np.mean(model.wv[doc], axis=0)

def xvalue(text):
    X = []
    X.append(dec_vector(text))
    return X

def preprocessed(text):
    text = remove_tags(text)
    text = lwr(text)
    text = stopword(text)
    text = remove_punctuation(text)
    text = remove_contractions(text)
    X = xvalue(text)
    X = np.array(X)
    return X

def clear_text():
    st.session_state["text"] = ""

def main():
    st.set_page_config(page_title="Sentiment Analysis AI", page_icon=":smiley:", layout="wide")
    
    with open("Sentimental_Analysis_Word2Vec.pkl", 'rb') as file1:  
        rf = pickle.load(file1)
    
    st.title('Sentiment Analysis AI')
    st.markdown("""
        <style>
        .main {
            background-color: #0a3d57;
            padding: 20px;
            border-radius: 10px;
        }
        .stButton > button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .stButton > button:hover {
            background-color: #45a049;
        }
        .stTextInput > div > input {
            padding: 10px;
            border: 2px solid #ccc;
            border-radius: 4px;
        }
        </style>
        """, unsafe_allow_html=True)

    st.markdown("<div class='main'>", unsafe_allow_html=True)
    
    st.header('Welcome to Sentiment Analysis AI')
    st.subheader('Check if reviews or any text have a positive or negative sentiment instantly!')

    text = st.text_input(
        "Enter some text πŸ‘‡", key="text", help="Type in any text to analyze its sentiment.")
    
    col1, col2 = st.columns(2)
    
    with col1:
        if st.button('Classify'):
            if text:
                z = preprocessed(text)
                if rf.predict(z)[0] == 1:
                    st.success("Positive")
                else:
                    st.success("Negative")
            else:
                st.warning("Please enter some text to analyze.")
    
    with col2:
        st.button("Clear", on_click=clear_text)
    
    st.markdown("</div>", unsafe_allow_html=True)

if __name__ == '__main__':
    main()