-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_analysis_text.py
410 lines (263 loc) · 12.7 KB
/
data_analysis_text.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#importing required libraries
import pandas as pd
import requests
from bs4 import BeautifulSoup
import numpy as np
import re
#importing nltk library and stopwords
import nltk
import string
#importing tokenize library
from nltk.tokenize import word_tokenize
import nltk
nltk.download('punkt')
import warnings
warnings.filterwarnings("ignore")
#importing input file
df=pd.read_excel(r'C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\Input.xlsx')[['URL_ID','URL']]
#importing stop words files that are provided
StopWords_Auditor=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_Auditor.txt",header=None)
with open(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_Currencies.txt") as f:
data = [list(map(str, row.split())) for row in f.read().split('\n\n')]
StopWords_Currencies = pd.DataFrame(data)
StopWords_Currencies = StopWords_Currencies.transpose()
StopWords_DatesandNumbers=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_DatesandNumbers.txt",header=None)
StopWords_Generic=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_Generic.txt",header=None)
StopWords_GenericLong=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_GenericLong.txt",header=None)
StopWords_Geographic=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_Geographic.txt",header=None)
StopWords_Names=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\StopWords\StopWords_Names.txt",header=None)
#importing master Dictionary
positive=pd.read_csv(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\MasterDictionary\positive-words.txt",header=None)
with open(r"C:\Users\tprat\Downloads\DATA_PREP-20230228T164907Z-001\DATA_PREP\MasterDictionary\negative-words.txt") as f:
data_neg = [list(map(str, row.split())) for row in f.read().split('\n\n')]
negative = pd.DataFrame(data_neg)
negative = negative.transpose()
df=df.iloc[0:114]
print(df)
orignal_df = df.copy()
df.drop('URL_ID',axis=1,inplace=True)
################# Data Extraction #######################
#extracting text from all the url
url_id=1
df_data = pd.Series()
no_content_index = []
for i in range(0,len(df)):
j=df.iloc[i].values
#print(i)
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}#giving user access
page=requests.get(j[0],headers=headers)
soup=BeautifulSoup(page.content,'html.parser') #parsing url text
content=soup.findAll(attrs={'class':'td-post-content'}) #extracting only text part
if content:
content=content[0].text.replace('\xa0'," ").replace('\n'," ") #replace end line symbol with space
else:
no_content_index.append(i)
print(f"We find No Content at link {j} which is at index {i}")
continue
title=soup.findAll(attrs={'class':'entry-title'}) #extracting title of website
title=title[16].text.replace('\n'," ").replace('/',"")
text=title+ '.' +content #merging title and content text
text=np.array(text) #converting to array form
text.reshape(1,-1) #changing shape to 1d
df1 = pd.Series(text) #creating series data frame
# print(df1)
df_data = df_data.append(df1).reset_index(drop=True) # getting all data
url_id+=1
## droping no content/text link that found
print(no_content_index) #drop no content
orignal_df.drop(no_content_index, inplace = True)
print(f'no content {orignal_df}')
print("extracted data")
print(df_data)
############ Data Analysis ###########
# extracted files
df_all = df_data.copy()
df_all = df_all.to_frame()
print(df_all.info())
output=pd.DataFrame()
#output.columns=['POSITIVE SCORE','NEGATIVE SCORE','POLARITY SCORE','SUBJECTIVITY SCORE','AVG SENTENCE LENGTH','PERCENTAGE OF COMPLEX WORDS','FOG INDEX','AVG NUMBER OF WORDS PER SENTENCE','COMPLEX WORD COUNT','WORD COUNT','SYLLABLE PER WORD','PERSONAL PRONOUNS','AVG WORD LENGTH']
for row in df_all.iterrows():
print(row)
element = row[1]
element = element.astype(str)
a=element.str.split('([\.]\s)',expand=False) #splitting text on '.'
b=a.explode() #converting to rows
b=pd.DataFrame(b) #creating data frame
b.columns=['abc']
# print(b)
#removing . char from each rows
def abcd(x):
nopunc =[char for char in x if char != '.']
return ''.join(nopunc)
b['abc']=b['abc'].apply(abcd)
#print(b)
#replacing empty space with null values
c=b.replace('',np.nan,regex=True)
c=c.mask(c==" ")
c=c.dropna()
c.reset_index(drop=True,inplace=True)
# print(c)
punc=[punc for punc in string.punctuation]
print(punc)
#creating func for removing stop words
def text_process(text):
nopunc =[char for char in text if char not in punc or char not in [':',',','(',')','’','?']]
nopunc=''.join(nopunc)
txt=' '.join([word for word in nopunc.split() if word.lower() not in StopWords_Auditor])
txt1=' '.join([word for word in txt.split() if word.lower() not in StopWords_Currencies])
txt2=' '.join([word for word in txt1.split() if word.lower() not in StopWords_DatesandNumbers])
txt3=' '.join([word for word in txt2.split() if word.lower() not in StopWords_Generic])
txt4=' '.join([word for word in txt3.split() if word.lower() not in StopWords_GenericLong])
txt5=' '.join([word for word in txt4.split() if word.lower() not in StopWords_Geographic])
return ' '.join([word for word in txt5.split() if word.lower() not in StopWords_Names])
#applying func for each row
c['abc']=c['abc'].apply(text_process)
print(c)
print(f"positive dataframe \n {positive}")
print(f"positive dataframe \n {negative}")
positive.columns=['abc']
negative.columns=['abc']
positive['abc']=positive['abc'].astype(str)
negative['abc']=negative['abc'].astype(str)
#positive and negative dictionary without stopwords
positive['abc']=positive['abc'].apply(text_process)
negative['abc']=negative['abc'].apply(text_process)
#positive list
length=positive.shape[0]
post=[]
for i in range(0,length):
nopunc =[char for char in positive.iloc[i] if char not in string.punctuation or char != '+']
nopunc=''.join(nopunc)
post.append(nopunc)
#negative list
length=negative.shape[0]
neg=[]
for i in range(0,length):
nopunc =[char for char in negative.iloc[i] if char not in string.punctuation or char != '+']
nopunc=''.join(nopunc)
neg.append(nopunc)
#tokenize
txt_list=[]
length=c.shape[0]
for i in range(0,length):
txt=' '.join([word for word in c.iloc[i]])
txt_list.append(txt)
#tokenization of text
tokenize_text=[]
for i in txt_list:
tokenize_text+=(word_tokenize(i))
print(tokenize_text)
print(len(tokenize_text))
### POSITIVE SCORE
positive_score=0
for i in tokenize_text:
if(i.lower() in post):
positive_score+=1
print('postive score=', positive_score)
### NEGATIVE SCORE
negative_score=0
for i in tokenize_text:
if(i.lower() in neg):
negative_score+=1
print('negative score=', negative_score)
### POLARITY SCORE
#Polarity Score = (Positive Score – Negative Score)/ ((Positive Score + Negative Score) + 0.000001)
Polarity_Score=(positive_score-negative_score)/((positive_score+negative_score)+0.000001)
print('polarity_score=', Polarity_Score)
### SUBJECTIVITY SCORE
#Subjectivity Score = (Positive Score + Negative Score)/ ((Total Words after cleaning) + 0.000001)
subjectiivity_score=(positive_score-negative_score)/((len(tokenize_text))+ 0.000001)
print('subjectivity_score',subjectiivity_score)
### AVG SENTENCE LENGTH
length=c.shape[0]
avg_length=[]
for i in range(0,length):
avg_length.append(len(c['abc'].iloc[i]))
avg_senetence_length=sum(avg_length)/len(avg_length)
print('avg sentence length=', avg_senetence_length)
### PERCENTAGE OF COMPLEX WORDS
vowels=['a','e','i','o','u']
count=0
complex_Word_Count=0
for i in tokenize_text:
x=re.compile('[es|ed]$')
if x.match(i.lower()):
count+=0
else:
for j in i:
if(j.lower() in vowels ):
count+=1
if(count>2):
complex_Word_Count+=1
count=0
Percentage_of_Complex_words=complex_Word_Count/len(tokenize_text)
print('percentag of complex words= ',Percentage_of_Complex_words)
### FOG INDEX
#Fog Index = 0.4 * (Average Sentence Length + Percentage of Complex words)
Fog_Index = 0.4 * (avg_senetence_length + Percentage_of_Complex_words)
print('fog index= ',Fog_Index )
### AVG NUMBER OF WORDS PER SENTENCE
length=c.shape[0]
avg_length=[]
for i in range(0,length):
a=[word.split( ) for word in c.iloc[i]]
avg_length.append(len(a[0]))
a=0
avg_no_of_words_per_sentence=sum(avg_length)/length
print("avg no of words per sentence= ",avg_no_of_words_per_sentence)
### COMPLEX WORD COUNT
vowels=['a','e','i','o','u']
count=0
complex_Word_Count=0
for i in tokenize_text:
x=re.compile('[es|ed]$')
if x.match(i.lower()):
count+=0
else:
for j in i:
if(j.lower() in vowels ):
count+=1
if(count>2):
complex_Word_Count+=1
count=0
print('complex words count=', complex_Word_Count)
### WORD COUNT
word_count=len(tokenize_text)
print('word count= ', word_count)
### SYLLABLE PER WORD
vowels=['a','e','i','o','u']
count=0
for i in tokenize_text:
x=re.compile('[es|ed]$')
if x.match(i.lower()):
count+=0
else:
for j in i:
if(j.lower() in vowels ):
count+=1
syllable_count=count
print('syllable_per_word= ',syllable_count)
### PERSONAL PRONOUNS
pronouns=['i','we','my','ours','us' ]
count=0
for i in tokenize_text:
if i.lower() in pronouns:
count+=1
personal_pronouns=count
print('personal pronouns= ',personal_pronouns )
### AVG WORD LENGTH
count=0
for i in tokenize_text:
for j in i:
count+=1
avg_word_length=count/len(tokenize_text)
print('avg word= ', avg_word_length)
data={'POSITIVE SCORE':positive_score,'NEGATIVE SCORE':negative_score,'POLARITY SCORE':Polarity_Score,'SUBJECTIVITY SCORE':subjectiivity_score,'AVG SENTENCE LENGTH':avg_senetence_length,'PERCENTAGE OF COMPLEX WORDS':Percentage_of_Complex_words,'FOG INDEX':Fog_Index,'AVG NUMBER OF WORDS PER SENTENCE':avg_no_of_words_per_sentence,'COMPLEX WORD COUNT':complex_Word_Count,'WORD COUNT':word_count,'SYLLABLE PER WORD':syllable_count,'PERSONAL PRONOUNS':personal_pronouns,'AVG WORD LENGTH':avg_word_length}
output=output.append(data,ignore_index=True)
## Reset index and merge for output structure
orignal_df.reset_index(drop=True, inplace=True)
output.reset_index(drop=True, inplace=True)
df_final_output = pd.concat([orignal_df, output], axis=1)
print(df_final_output)
print(df_final_output.shape)
df_final_output.to_csv('final_output.csv', index=False)