-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
197 lines (173 loc) · 6.56 KB
/
app.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from tracemalloc import stop
import streamlit as st
import numpy as np
import pandas as pd
import re
import string
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestClassifier
nltk.download('punkt')
nltk.download('stopwords')
sw=nltk.corpus.stopwords.words("english")
rad=st.sidebar.radio("Navigation",["Home","Spam or Ham Detection","Sentiment Analysis","Stress Detection","Hate and Offensive Content Detection","Sarcasm Detection"])
#Home Page
if rad=="Home":
st.title("Complete Text Analysis App")
st.text(" ")
st.text("The Following Text Analysis Options Are Available->")
st.text(" ")
st.text("1. Spam or Ham Detection")
st.text("2. Sentiment Analysis")
st.text("3. Stress Detection")
st.text("4. Hate and Offensive Content Detection")
st.text("5. Sarcasm Detection")
#function to clean and transform the user input which is in raw format
def transform_text(text):
text=text.lower()
text=nltk.word_tokenize(text)
y=[]
for i in text:
if i.isalnum():
y.append(i)
text=y[:]
y.clear()
for i in text:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
text=y[:]
y.clear()
ps=PorterStemmer()
for i in text:
y.append(ps.stem(i))
return " ".join(y)
#Spam Detection Prediction
tfidf1=TfidfVectorizer(stop_words=sw,max_features=20)
def transform1(txt1):
txt2=tfidf1.fit_transform(txt1)
return txt2.toarray()
df1=pd.read_csv("Spam Detection.csv")
df1.columns=["Label","Text"]
x=transform1(df1["Text"])
y=df1["Label"]
x_train1,x_test1,y_train1,y_test1=train_test_split(x,y,test_size=0.1,random_state=0)
model1=LogisticRegression()
model1.fit(x_train1,y_train1)
#Spam Detection Analysis Page
if rad=="Spam or Ham Detection":
st.header("Detect Whether A Text Is Spam Or Ham??")
sent1=st.text_area("Enter The Text")
transformed_sent1=transform_text(sent1)
vector_sent1=tfidf1.transform([transformed_sent1])
prediction1=model1.predict(vector_sent1)[0]
if st.button("Predict"):
if prediction1=="spam":
st.warning("Spam Text!!")
elif prediction1=="ham":
st.success("Ham Text!!")
#Sentiment Analysis Prediction
tfidf2=TfidfVectorizer(stop_words=sw,max_features=20)
def transform2(txt1):
txt2=tfidf2.fit_transform(txt1)
return txt2.toarray()
df2=pd.read_csv("Sentiment Analysis.csv")
df2.columns=["Text","Label"]
x=transform2(df2["Text"])
y=df2["Label"]
x_train2,x_test2,y_train2,y_test2=train_test_split(x,y,test_size=0.1,random_state=0)
model2=LogisticRegression()
model2.fit(x_train2,y_train2)
#Sentiment Analysis Page
if rad=="Sentiment Analysis":
st.header("Detect The Sentiment Of The Text!!")
sent2=st.text_area("Enter The Text")
transformed_sent2=transform_text(sent2)
vector_sent2=tfidf2.transform([transformed_sent2])
prediction2=model2.predict(vector_sent2)[0]
if st.button("Predict"):
if prediction2==0:
st.warning("Negetive Text!!")
elif prediction2==1:
st.success("Positive Text!!")
#Stress Detection Prediction
tfidf3=TfidfVectorizer(stop_words=sw,max_features=20)
def transform3(txt1):
txt2=tfidf3.fit_transform(txt1)
return txt2.toarray()
df3=pd.read_csv("Stress Detection.csv")
df3=df3.drop(["subreddit","post_id","sentence_range","syntax_fk_grade"],axis=1)
df3.columns=["Text","Sentiment","Stress Level"]
x=transform3(df3["Text"])
y=df3["Stress Level"].to_numpy()
x_train3,x_test3,y_train3,y_test3=train_test_split(x,y,test_size=0.1,random_state=0)
model3=DecisionTreeRegressor(max_leaf_nodes=2000)
model3.fit(x_train3,y_train3)
#Stress Detection Page
if rad=="Stress Detection":
st.header("Detect The Amount Of Stress In The Text!!")
sent3=st.text_area("Enter The Text")
transformed_sent3=transform_text(sent3)
vector_sent3=tfidf3.transform([transformed_sent3])
prediction3=model3.predict(vector_sent3)[0]
if st.button("Predict"):
if prediction3>=0:
st.warning("Stressful Text!!")
elif prediction3<0:
st.success("Not A Stressful Text!!")
#Hate & Offensive Content Prediction
tfidf4=TfidfVectorizer(stop_words=sw,max_features=20)
def transform4(txt1):
txt2=tfidf4.fit_transform(txt1)
return txt2.toarray()
df4=pd.read_csv("Hate Content Detection.csv")
df4=df4.drop(["Unnamed: 0","count","neither"],axis=1)
df4.columns=["Hate Level","Offensive Level","Class Level","Text"]
x=transform4(df4["Text"])
y=df4["Class Level"]
x_train4,x_test4,y_train4,y_test4=train_test_split(x,y,test_size=0.1,random_state=0)
model4=RandomForestClassifier()
model4.fit(x_train4,y_train4)
#Hate & Offensive Content Page
if rad=="Hate and Offensive Content Detection":
st.header("Detect The Level Of Hate & Offensive Content In The Text!!")
sent4=st.text_area("Enter The Text")
transformed_sent4=transform_text(sent4)
vector_sent4=tfidf4.transform([transformed_sent4])
prediction4=model4.predict(vector_sent4)[0]
if st.button("Predict"):
if prediction4==0:
st.exception("Highly Offensive Text!!")
elif prediction4==1:
st.warning("Offensive Text!!")
elif prediction4==2:
st.success("Non Offensive Text!!")
#Sarcasm Detection Prediction
tfidf5=TfidfVectorizer(stop_words=sw,max_features=20)
def transform5(txt1):
txt2=tfidf5.fit_transform(txt1)
return txt2.toarray()
df5=pd.read_csv("Sarcasm Detection.csv")
df5.columns=["Text","Label"]
x=transform5(df5["Text"])
y=df5["Label"]
x_train5,x_test5,y_train5,y_test5=train_test_split(x,y,test_size=0.1,random_state=0)
model5=LogisticRegression()
model5.fit(x_train5,y_train5)
#Sarcasm Detection Page
if rad=="Sarcasm Detection":
st.header("Detect Whether The Text Is Sarcastic Or Not!!")
sent5=st.text_area("Enter The Text")
transformed_sent5=transform_text(sent5)
vector_sent5=tfidf5.transform([transformed_sent5])
prediction5=model5.predict(vector_sent5)[0]
if st.button("Predict"):
if prediction5==1:
st.exception("Sarcastic Text!!")
elif prediction5==0:
st.success("Non Sarcastic Text!!")