-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
283 lines (210 loc) · 7.75 KB
/
Copy pathpreprocessing.py
File metadata and controls
283 lines (210 loc) · 7.75 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
"""Preprocessing.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1070MYQzdGOBcouLpNZTROFGtLxdP1Qq9
# Data-Cleaning Code
# imports
"""
!pip install langdetect
!pip install -U textblob
!pip install -U textblob-fr
!python -m textblob.download_corpora
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import re
from langdetect import DetectorFactory, detect, detect_langs
from bs4 import BeautifulSoup
import re
import datetime as datetime
from textblob import TextBlob
from textblob_fr import PatternTagger, PatternAnalyzer
text = u"Quelle belle matinée"
blob = TextBlob(text, pos_tagger=PatternTagger(), analyzer=PatternAnalyzer())
df = pd.read_csv('/content/Sheet - Sheet5.csv')
#df=df.set_axis([*df.columns[:-1], 'Reply'], axis=1, inplace=False)
df.head()
# df['Corpus'] = df['Corpus'].replace({r'\s+$': '', r'^\s+': ''}, regex=True).replace(r'\n', ' ', regex=True)
# df['Corpus'] = df['Corpus'].replace('\r', ' ')
# import urllib.request
# from html.parser import HTMLParser
# import re
# page = urllib.request.urlopen('http://netherkingdom.netai.net/pycake.html')
# t = page.read()
# class MyHTMLParser(HTMLParser):
# def handle_data(self, data):
# print(data)
# f = open('/Users/austinhitt/Desktop/Test.py', 'r')
# t = f.read()
# f = open('/Users/austinhitt/Desktop/Test.py', 'w')
# f.write(t + '\n' + data)
# f.close()
# parser = MyHTMLParser()
# t = t.decode()
# parser.feed(t)
df['Corpus'].head()
"""# Seperate code to show how I have seperated the comments for Ayush and Barkha"""
# df1= df.loc[df['Comment'].str.contains("Ayush", case=False)]
#df1.to_csv('Ayush_E6.csv',index=False)
#df2 = df.loc[df['Comment'].str.contains("Barkha", case=False)]
#df2.to_csv('Barkha_E6.csv', index=False)
#df.sample(5)
"""# preprocess"""
#df_copy = df.copy()
#df_copy['Comment'] = [BeautifulSoup(text).get_text() for text in df_copy['Comment']]#remove HTML tags
#df_copy['Comment'] = df_copy['Comment'].apply(lambda x: re.split(' "https:\/\/.*', str(x))[0])#remove HTTP
df["Corpus"] = df["Corpus"].str.lower()
"""##### Clean data creation """
# # lists for the data columns
# Name=[]
# Comment=[]
# Time=[]
# Likes=[]
# Reply=[]
# Time=[]
# # list for checking error in detect Library
# done=[]
# Notedone=[]
# #loop for the new data
# for i in range(len(df_copy)):
# try:
# if detect(df_copy['Comment'][i]) == 'en':
# Comment.append(df_copy['Comment'][i])
# done.append(i)#check
# #others appended
# Name.append(df_copy['Name'][i])
# Time.append(df_copy['Time'][i])
# Likes.append(df_copy['Likes'][i])
# Reply.append(df_copy['Reply'][i])
# Time.append(df_copy['Time'][i])
# except:
# Notedone.append(i)#check
# print(len(Name),len(Comment),len(Time),len(Likes),len(Reply))
# print(len(Notedone),'text languages is not getting detected and gave and error')
# df_cl = pd.DataFrame(list(zip(Name,Comment,Time,Likes,Reply)),columns =['Names','Comment','Time','Likes','Reply_count'])
# #df_cl.to_csv('clean_data.csv',index=False)#save
# #df_cl
# temp= pd.DataFrame(list(Comment),columns=['Comment'])
# temp1= pd.DataFrame(list(Likes),columns=['Likes'])
# temp2= pd.DataFrame(list(Name),columns=['Name'])
# temp3= pd.DataFrame(list(Reply),columns=['Reply_Count'])
# temp4 = pd.DataFrame(list(Time),columns=['Time'])
# temp['Comment'] = temp.astype(str).apply(lambda x: x.str.encode('ascii', 'ignore').str.decode('ascii'))
#removing special characters from comments
spec_chars = ["!",'"',"#","%","&","'","(",")",
"*","+",",","-",".","/",":",";","<",
"=",">","?","@","[","\\","]","^","_",
"`","{","|","}","~","–"]
for char in spec_chars:
df['Corpus'] = df['Corpus'].str.replace(char, ' ')
# df['Pos answer'] = df['Pos answer'].str.split().str.join(" ")
df.head()
#removing emojis
temp2['Name'] = temp2.astype(str).apply(lambda x: x.str.encode('ascii', 'ignore').str.decode('ascii'))
# #removing special characters from name
# spec_chars = ["!",'"',"#","%","&","'","(",")",
# "*","+",",","-",".","/",":",";","<",
# "=",">","?","@","[","\\","]","^","_",
# "`","{","|","}","~","–"]
# for char in spec_chars:
# temp2['Name'] = temp2['Name'].str.replace(char, ' ')
"""### Concating data"""
from datetime import *
# #Extracting month, date and hour from time column
# date = []
# hour = []
# month = []
# for i in range(len(result)):
# d1 = datetime.fromisoformat(result['Time'][i][:-1])
# h = d1.strftime('%H')
# d = d1.strftime('%d')
# m = d1.strftime('%m')
# date.append(d)
# hour.append(h)
# month.append(m)
# result['date']=date
# result['hour']=hour
# result['month']=month
# #lowering the strings
# df['Pos answer'] = df['Pos answer'].str.lower()
df.to_csv('clean_corpus.csv')
#result.head()
"""# Sentimental Scoring methods
# Importing libraries
"""
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem.snowball import SnowballStemmer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
nltk.download('vader_lexicon')
df['Answer3'] = df['Answer3'].fillna('zero')
df['Answer4'] = df['Answer4'].fillna('zero')
df['Answer5'] = df['Answer5'].fillna('zero')
df['Answer 6'] = df['Answer 6'].fillna('zero')
df['Answer 6']= df['Answer 6'].replace(0, 'zero')
#tokenizing the comments
df['Answer 6']= df['Answer 6'].apply(word_tokenize)
#creating stem of comments
stemmer = SnowballStemmer("english")
df['Answer 1'] = df['Answer 1'].apply(lambda x: [stemmer.stem(y) for y in x])
df['Answer2'] = df['Answer2'].apply(lambda x: [stemmer.stem(y) for y in x])
df['Answer3'] = df['Answer3'].apply(lambda x: [stemmer.stem(y) for y in x])
df['Answer4'] = df['Answer4'].apply(lambda x: [stemmer.stem(y) for y in x])
df['Answer5'] = df['Answer5'].apply(lambda x: [stemmer.stem(y) for y in x])
df['Answer 6'] = df['Answer 6'].apply(lambda x: [stemmer.stem(y) for y in x])
df.head()
df.to_csv('imagescore.csv')
#Creating sentiment scores positive, negative, neutral and compound
analyzer = SentimentIntensityAnalyzer()
df['compound'] = [analyzer.polarity_scores(x)['compound'] for x in df['Answer 6']]
df['neg'] = [analyzer.polarity_scores(x)['neg'] for x in df['Answer 6']]
df['neu'] = [analyzer.polarity_scores(x)['neu'] for x in df['Answer 6']]
df['pos'] = [analyzer.polarity_scores(x)['pos'] for x in df['Answer 6']]
#Calculating polarity and subjectivity
df[['polarity_3', 'subjectivity_3']] = df['answer 3'].apply(lambda Text: pd.Series(TextBlob(Text).sentiment))
df.head()
"""### Empath score
"""
!pip install empath
#function for creating dictionary
def returnSum(dict):
sum = 0
for i in dict:
sum = sum + dict[i]
return sum
#calculating empath score
from empath import Empath
lexicon = Empath()
l = []
m = []
for i in range(len(df)):
lex= lexicon.analyze(df['Answer 6'][i], normalize=True)
emp = empath=lexicon.analyze(df['Answer 6'][i], normalize=True)
empath_score= returnSum(empath)
l.append(empath_score)
l = pd.DataFrame(list(l))
l.info()
l.rename(columns = {0:'empath_score'}, inplace = True)
df = pd.concat([df, l], axis=1, join='inner')
display(df)
df.to_csv('last.csv')
def maximum(a, b, c):
if (a >= b) and (a >= c):
largest = 'neg'
elif (b >= a) and (b >= c):
largest = 'neu'
else:
largest = 'pos'
return largest
l=[]
for i in range(len(result)):
x=maximum(df['neg'][i],df['neu'][i],df['pos'][i])
l.append(x)
df['sentiment_status']=l
#Final dataset cleaned and scored
df.to_csv("Barkha E3 score.csv", index=False)