-
Notifications
You must be signed in to change notification settings - Fork 0
/
sarcasmDetector.py
168 lines (126 loc) · 4.83 KB
/
sarcasmDetector.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
# -*- coding: utf-8 -*-
"""IR_PROJECT(k17_3867.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-lufRukfDeiV5BZonr9l2TUnoUs9jsPK
"""
!pip install seaborn
!pip install spacy
from google.colab import drive
drive.mount('/content/drive')
import numpy as np
import pandas as pd
from sklearn.svm import LinearSVC
import seaborn as sns
import spacy
import matplotlib.pyplot as plt
import random
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from pandas.io.json import json_normalize
from spacy import displacy
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
#from sklearn.metrics import confusion_matrix
#from sklearn.metrics import classification_report
global i
nlp = spacy.load('en_core_web_sm')
data = pd.read_json('/content/drive/My Drive/Sarcasm_Headlines_Dataset.json',lines=True)
data.head()
data.isnull().sum()
#print("Features: ", data.headline)
#print("Labels: ", data.is_sarcastic)
def traverse_headline(text):
doc=nlp(text)
print(f'Headline : {doc}')
print(f'\nTotal number of tokens : {len(doc)} \n')
for token in doc:
print(token.text,end=' | ')
for token in doc:
print(f'{token.text:{12}}{token.pos_:{10}}{token.dep_:{12}}{str(spacy.explain(token.dep_))}')
print(f'\nTotal number of Sentences : {len(list(doc.sents))}')
for sent in doc.sents:
print(sent)
if len(doc.ents)>0:
print(f'\nTotal number of Entity : {len(doc.ents)}\n')
for ent in doc.ents:
print(ent.text+' - '+ent.label_+' - '+str(spacy.explain(ent.label_)))
displacy.render(doc,style='ent',jupyter=True)
displacy.render(doc,style='dep',jupyter=True,options={'distance': 80})
def get_ents(text):
doc=nlp(text)
return len(doc.ents)
def get_tokens(text):
doc=nlp(text)
return len(doc)
def get_sents(text):
doc=nlp(text)
return len(list(doc.sents))
#traverse_headline(data['headline'][0])
#traverse_headline(data['headline'][1])
data_sample = data.sample(frac=.30,random_state=1)
data_sample.head()
data_sample['ents_num'] = data_sample['headline'].apply(get_ents)
data_sample['tokens_num'] = data_sample['headline'].apply(get_tokens)
data_sample['sents_num'] = data_sample['headline'].apply(get_sents)
data_sample.head()
fig,(ax,ax1,ax2)=plt.subplots(nrows=3,ncols=1,figsize=(15,15))
sns.countplot(x='ents_num',data=data_sample,hue='is_sarcastic',ax=ax,palette='spring')
sns.countplot(x='tokens_num',data=data_sample,hue='is_sarcastic',ax=ax1,palette='winter')
sns.countplot(x='sents_num',data=data_sample,hue='is_sarcastic',ax=ax2,palette='cool')
#building classification model
data_sample.drop(['article_link','ents_num','tokens_num','sents_num'],axis=1,inplace=True)
data.drop(['article_link'],axis=1,inplace=True)
blanks = []
for i,he,is_sa in data_sample.itertuples():
if type(he) == str:
if he.isspace():
blanks.append(i)
#print(len(blanks), 'blanks: ', blanks)
#train_test_split
X = data_sample['headline']
y = data_sample['is_sarcastic']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
#Building a pipeline
classifier_svc = Pipeline([('tfidf',TfidfVectorizer()),
('clf',LinearSVC())])
classifier_svc.fit(X_train,y_train)
classifier_svc.fit(X_test,y_test)
y_pred = classifier_svc.predict(X_test)
yt_pred = classifier_svc.predict(X_train)
#print("Test predict :", y_pred)
print("Train predict :", yt_pred)
#print(f'Test Set Accuracy Score :\n {accuracy_score(y_test,y_pred)}\n')
#print(f'Train Set Accuracy Score :\n {accuracy_score(y_train,yt_pred)}\n')
my_index=0
user_tweet = input("user tweet: ")
#print(user_tweet)
my_index = y_pred.shape[0]-1
X_test[my_index] = user_tweet
classifier_svc.fit(X_train,y_train)
y_pred = classifier_svc.predict(X_test)
print("result2: ", y_pred[my_index])
#traverse_headline(y_pred[my_index])
print(f'Test Set Accuracy Score :\n {accuracy_score(y_test,y_pred)}\n')
print(f'Train Set Accuracy Score :\n {accuracy_score(y_train,yt_pred)}\n')
!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16 &') # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0'
from tkinter import *
root = Tk()
root.geometry("500x300")
root.title('Sarcams Detector For Twitter')
t_var=StringVar()
w = Label(root, text='Sarcasm Detector')
w.pack()
e = Entry(root, textvariable = t_var,font=('calibre',10,'normal'))
e.pack(side = TOP, ipadx = 150, ipady = 100,)
e.focus_set()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack(anchor="center")
blackbutton = Button(bottomframe, text ='Check', fg ='black')
blackbutton.pack()
root.mainloop()