-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
91 lines (71 loc) · 2.18 KB
/
main.py
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
import pickle
import nltk
from nltk.tokenize import word_tokenize
import streamlit as st
from utils import remove_noise
def get_text():
"""
Gets user input text/comment.
"""
input_text = st.text_input(
"Your comment: ", "I really like this product! It's awesome."
)
return input_text
@st.cache(show_spinner=False)
def _initialize_model():
"""
Initializes the sentiment classification model.
"""
nltk.download("wordnet")
nltk.download("averaged_perceptron_tagger")
nltk.download("punkt")
with open("models/naive_bayes.mdl", "rb") as file:
model = pickle.load(file)
return model
def main():
# Set page config
st.set_page_config(
page_title="Sentiment Analysis",
page_icon=None,
layout="centered",
initial_sidebar_state="auto",
)
# Set page title text
st.title(
"""
Comment Sentiment Analysis
This app will detect the sentiment of an user's comment as either positive or negative.
"""
)
# Initialize sidebar
st.sidebar.title("Details")
st.sidebar.text("")
# Sidebar information
st.sidebar.text("Preprocessing:")
st.sidebar.markdown(
"""
* URL removal
* @ Mention removal
* Lemmatization
* Tokenization
"""
)
st.sidebar.text("")
st.sidebar.text("Model: ")
st.sidebar.text("Naive Bayes Classifier")
input_comment = get_text()
classifier = _initialize_model()
if (not input_comment) or (input_comment.isspace()):
st.write("Write a comment and press the Enter key...")
else:
input_tokens = remove_noise(word_tokenize(input_comment.replace("'", "")))
dist = classifier.prob_classify(dict([token, True] for token in input_tokens))
prob = [dist.prob(label) for label in dist.samples()]
if prob[0] > prob[1]:
st.image("images/positive.png", width=200)
else:
st.image("images/negative.png", width=200)
confidence = max(prob)
st.write(f"Confidence score = {confidence:.4f}")
if __name__ == "__main__":
main()