-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMachineLearning.py
154 lines (134 loc) · 5.4 KB
/
MachineLearning.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
import tkinter
from textblob import TextBlob
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from string import punctuation
from nltk.corpus import stopwords
main = tkinter.Tk()
main.title("Analysis of Women Safety in Indian Cities Using Machine Learning on Tweets") #designing main screen
main.geometry("1300x1200")
global filename
tweets_list = []
clean_list = []
global pos, neu, neg
def tweetCleaning(doc):
tokens = doc.split()
table = str.maketrans('', '', punctuation)
tokens = [w.translate(table) for w in tokens]
tokens = [word for word in tokens if word.isalpha()]
stop_words = set(stopwords.words('english'))
tokens = [w for w in tokens if not w in stop_words]
tokens = [word for word in tokens if len(word) > 1]
tokens = ' '.join(tokens) #here upto for word based
return tokens
def upload(): #function to upload tweeter profile
global filename
filename = filedialog.askopenfilename(initialdir="dataset")
pathlabel.config(text=filename)
text.delete('1.0', END)
text.insert(END,filename+" loaded\n");
def read():
text.delete('1.0', END)
tweets_list.clear()
train = pd.read_csv(filename,encoding='iso-8859-1')
for i in range(len(train)):
tweet = train.get_value(i, 'Text')
tweets_list.append(tweet)
text.insert(END,tweet+"\n")
text.insert(END,"\n\nTotal tweets found in dataset is : "+str(len(tweets_list))+"\n\n\n")
def clean():
text.delete('1.0', END)
clean_list.clear()
for i in range(len(tweets_list)):
tweet = tweets_list[i]
tweet = tweet.strip("\n")
tweet = tweet.strip()
tweet = tweetCleaning(tweet.lower())
clean_list.append(tweet)
text.insert(END,tweet+"\n")
text.insert(END,"\n\nTotal tweets found in dataset is : "+str(len(clean_list))+"\n\n\n")
def machineLearning():
text.delete('1.0', END)
global pos, neu, neg
pos = 0
neu = 0
neg = 0
for i in range(len(clean_list)):
tweet = clean_list[i]
blob = TextBlob(tweet)
if blob.polarity <= 0.2:
neg = neg + 1
text.insert(END,tweet+"\n")
text.insert(END,"Predicted Sentiment : NEGATIVE\n")
text.insert(END,"Polarity Score : "+str(blob.polarity)+"\n")
text.insert(END,'====================================================================================\n')
if blob.polarity > 0.2 and blob.polarity <= 0.5:
neu = neu + 1
text.insert(END,tweet+"\n")
text.insert(END,"Predicted Sentiment : NEUTRAL\n")
text.insert(END,"Polarity Score : "+str(blob.polarity)+"\n")
text.insert(END,'====================================================================================\n')
if blob.polarity > 0.5:
pos = pos + 1
text.insert(END,tweet+"\n")
text.insert(END,"Predicted Sentiment : POSITIVE\n")
text.insert(END,"Polarity Score : "+str(blob.polarity)+"\n")
text.insert(END,'====================================================================================\n')
def graph():
label_X = []
category_X = []
text.delete('1.0', END)
text.insert(END,"Saftey Factor\n\n")
text.insert(END,'Positive : '+str(pos)+"\n")
text.insert(END,'Negative : '+str(neg)+"\n")
text.insert(END,'Neutral : '+str(neu)+"\n\n")
text.insert(END,'Length of tweets : '+str(len(clean_list))+"\n")
text.insert(END,'Positive : '+str(pos)+' / '+ str(len(clean_list))+' = '+str(pos/len(clean_list))+'%\n')
text.insert(END,'Negative : '+str(neg)+' / '+ str(len(clean_list))+' = '+str(neg/len(clean_list))+'%\n')
text.insert(END,'Neutral : '+str(neu)+' / '+ str(len(clean_list))+' = '+str(neu/len(clean_list))+'%\n')
label_X.append('Positive')
label_X.append('Negative')
label_X.append('Neutral')
category_X.append(pos)
category_X.append(neg)
category_X.append(neu)
plt.pie(category_X,labels=label_X,autopct='%1.1f%%')
plt.title('Women Saftey & Sentiment Graph')
plt.axis('equal')
plt.show()
font = ('times', 16, 'bold')
title = Label(main, text='Analysis of Women Safety in Indian Cities Using Machine Learning on Tweets')
title.config(bg='brown', fg='white')
title.config(font=font)
title.config(height=3, width=120)
title.place(x=0,y=5)
font1 = ('times', 14, 'bold')
uploadButton = Button(main, text="Upload Tweets Dataset", command=upload)
uploadButton.place(x=50,y=100)
uploadButton.config(font=font1)
pathlabel = Label(main)
pathlabel.config(bg='brown', fg='white')
pathlabel.config(font=font1)
pathlabel.place(x=370,y=100)
readButton = Button(main, text="Read Tweets", command=read)
readButton.place(x=50,y=150)
readButton.config(font=font1)
cleanButton = Button(main, text="Tweets Cleaning", command=clean)
cleanButton.place(x=210,y=150)
cleanButton.config(font=font1)
mlButton = Button(main, text="Run Machine Learning Algorithm", command=machineLearning)
mlButton.place(x=400,y=150)
mlButton.config(font=font1)
graphButton = Button(main, text="Women Saftey Graph", command=graph)
graphButton.place(x=730,y=150)
graphButton.config(font=font1)
font1 = ('times', 12, 'bold')
text=Text(main,height=25,width=150)
scroll=Scrollbar(text)
text.configure(yscrollcommand=scroll.set)
text.place(x=10,y=200)
text.config(font=font1)
main.config(bg='brown')
main.mainloop()