forked from rohitharitash/EDGAR-reports-Text-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDGAR extraction and Analysis.py
475 lines (317 loc) · 14.7 KB
/
EDGAR extraction and Analysis.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# coding: utf-8
# # Data Extraction and Text Analysis
#
# First function reading edar files.It will remove html tags and extract requried informtion
# In[13]:
# Requried imports
import os
import re
import pandas as pd
from nltk.tokenize import RegexpTokenizer, sent_tokenize
import numpy as np
# In[14]:
# Text extraction patterns
mda_regex = r"item[^a-zA-Z\n]*\d\s*\.\s*management\'s discussion and analysis.*?^\s*item[^a-zA-Z\n]*\d\s*\.*"
qqd_regex = r"item[^a-zA-Z\n]*\d[a-z]?\.?\s*Quantitative and Qualitative Disclosures about " r"Market Risk.*?^\s*item\s*\d\s*"
riskfactor_regex = r"item[^a-zA-Z\n]*\d[a-z]?\.?\s*Risk Factors.*?^\s*item\s*\d\s*"
# In[15]:
# Filepath locations
stopWordsFile = 'D:/data science/Blackcoffer project/StopWords_Generic.txt'
positiveWordsFile = 'D:/data science/Blackcoffer project/PositiveWords.txt'
nagitiveWordsFile = 'D:/data science/Blackcoffer project/NegativeWords.txt'
uncertainty_dictionaryFile = 'D:/data science/Blackcoffer project/uncertainty_dictionary.txt'
constraining_dictionaryFile = 'D:/data science/Blackcoffer project/constraining_dictionary.txt'
# In[16]:
# Function for extracting requried text
def rawdata_extract(path, cikListFile):
html_regex = re.compile(r'<.*?>')
extraxted_data=[]
cikListFile = pd.read_csv(cikListFile)
for index, row in cikListFile.iterrows():
processingFile=row['SECFNAME'].split('/')
inputFile = processingFile[3]
cik=row['CIK']
coname=row['CONAME']
fyrmo=row['FYRMO']
fdate = row['FDATE']
form = row['FORM']
secfname=row['SECFNAME']
for fileName in os.listdir(path):
filenameopen = os.path.join(path, fileName)
dirFileName = filenameopen.split('\\')
currentFile=dirFileName[1]
if os.path.isfile(filenameopen) and currentFile == inputFile :
resultdict = dict()
resultdict['CIK'] = cik
resultdict['CONAME'] = coname
resultdict['FYRMO'] = fyrmo
resultdict['FDATE'] = fdate
resultdict['FORM'] = form
resultdict['SECFNAME'] = secfname
with open(filenameopen, 'r', encoding='utf-8', errors="replace") as in_file:
content = in_file.read()
content = re.sub(html_regex,'',content)
content = content.replace(' ','')
content = re.sub(r'&#\d+;', '', content)
matches_mda = re.findall(mda_regex, content, re.IGNORECASE | re.DOTALL | re.MULTILINE)
if matches_mda:
result = max(matches_mda, key=len)
result = str(result).replace('\n', '')
resultdict['mda_extract'] = result
else:
resultdict['mda_extract'] = ""
match_qqd = re.findall(qqd_regex, content, re.IGNORECASE | re.DOTALL | re.MULTILINE)
if match_qqd:
result_qqd = max(match_qqd, key=len)
result_qqd = str(result_qqd).replace('\n','')
resultdict['qqd_extract']= result_qqd
else:
resultdict['qqd_extract'] = ""
match_riskfactor = re.findall(riskfactor_regex, content, re.IGNORECASE | re.DOTALL | re.MULTILINE)
if match_riskfactor:
result_riskfactor = max(match_riskfactor, key=len)
result_riskfactor = str(result_riskfactor).replace('\n', '')
resultdict['riskfactor_extract'] = result_riskfactor
else:
resultdict['riskfactor_extract'] = ""
extraxted_data.append(resultdict)
in_file.close()
return extraxted_data
# # Section 1.1: Positive score, negative score, polarity score
# Loading stop words dictionary for removing stop words
# In[17]:
with open(stopWordsFile ,'r') as stop_words:
stopWords = stop_words.read().lower()
stopWordList = stopWords.split('\n')
stopWordList[-1:] = []
# tokenizeing module and filtering tokens using stop words list, removing punctuations
# In[18]:
# Tokenizer
def tokenizer(text):
text = text.lower()
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
filtered_words = list(filter(lambda token: token not in stopWordList, tokens))
return filtered_words
# In[19]:
# Loading positive words
with open(positiveWordsFile,'r') as posfile:
positivewords=posfile.read().lower()
positiveWordList=positivewords.split('\n')
# In[20]:
# Loading negative words
with open(nagitiveWordsFile ,'r') as negfile:
negativeword=negfile.read().lower()
negativeWordList=negativeword.split('\n')
# In[21]:
# Calculating positive score
def positive_score(text):
numPosWords = 0
rawToken = tokenizer(text)
for word in rawToken:
if word in positiveWordList:
numPosWords += 1
sumPos = numPosWords
return sumPos
# In[22]:
# Calculating Negative score
def negative_word(text):
numNegWords=0
rawToken = tokenizer(text)
for word in rawToken:
if word in negativeWordList:
numNegWords -=1
sumNeg = numNegWords
sumNeg = sumNeg * -1
return sumNeg
# In[23]:
# Calculating polarity score
def polarity_score(positiveScore, negativeScore):
pol_score = (positiveScore - negativeScore) / ((positiveScore + negativeScore) + 0.000001)
return pol_score
# # Section 2 -Analysis of Readability - Average Sentence Length, percentage of complex words, fog index
# In[24]:
# Calculating Average sentence length
# It will calculated using formula --- Average Sentence Length = the number of words / the number of sentences
def average_sentence_length(text):
sentence_list = sent_tokenize(text)
tokens = tokenizer(text)
totalWordCount = len(tokens)
totalSentences = len(sentence_list)
average_sent = 0
if totalSentences != 0:
average_sent = totalWordCount / totalSentences
average_sent_length= average_sent
return round(average_sent_length)
# In[25]:
# Calculating percentage of complex word
# It is calculated using Percentage of Complex words = the number of complex words / the number of words
def percentage_complex_word(text):
tokens = tokenizer(text)
complexWord = 0
complex_word_percentage = 0
for word in tokens:
vowels=0
if word.endswith(('es','ed')):
pass
else:
for w in word:
if(w=='a' or w=='e' or w=='i' or w=='o' or w=='u'):
vowels += 1
if(vowels > 2):
complexWord += 1
if len(tokens) != 0:
complex_word_percentage = complexWord/len(tokens)
return complex_word_percentage
# In[26]:
# calculating Fog Index
# Fog index is calculated using -- Fog Index = 0.4 * (Average Sentence Length + Percentage of Complex words)
def fog_index(averageSentenceLength, percentageComplexWord):
fogIndex = 0.4 * (averageSentenceLength + percentageComplexWord)
return fogIndex
# # Section 4: Complex word count
# In[27]:
# Counting complex words
def complex_word_count(text):
tokens = tokenizer(text)
complexWord = 0
for word in tokens:
vowels=0
if word.endswith(('es','ed')):
pass
else:
for w in word:
if(w=='a' or w=='e' or w=='i' or w=='o' or w=='u'):
vowels += 1
if(vowels > 2):
complexWord += 1
return complexWord
# # Section 5: Word count
# In[28]:
#Counting total words
def total_word_count(text):
tokens = tokenizer(text)
return len(tokens)
# In[29]:
# calculating uncertainty_score
with open(uncertainty_dictionaryFile ,'r') as uncertain_dict:
uncertainDict=uncertain_dict.read().lower()
uncertainDictionary = uncertainDict.split('\n')
def uncertainty_score(text):
uncertainWordnum =0
rawToken = tokenizer(text)
for word in rawToken:
if word in uncertainDictionary:
uncertainWordnum +=1
sumUncertainityScore = uncertainWordnum
return sumUncertainityScore
# In[30]:
# calculating constraining score
with open(constraining_dictionaryFile ,'r') as constraining_dict:
constrainDict=constraining_dict.read().lower()
constrainDictionary = constrainDict.split('\n')
def constraining_score(text):
constrainWordnum =0
rawToken = tokenizer(text)
for word in rawToken:
if word in constrainDictionary:
constrainWordnum +=1
sumConstrainScore = constrainWordnum
return sumConstrainScore
# In[31]:
# Calculating positive word proportion
def positive_word_prop(positiveScore,wordcount):
positive_word_proportion = 0
if wordcount !=0:
positive_word_proportion = positiveScore / wordcount
return positive_word_proportion
# In[32]:
# Calculating negative word proportion
def negative_word_prop(negativeScore,wordcount):
negative_word_proportion = 0
if wordcount !=0:
negative_word_proportion = negativeScore / wordcount
return negative_word_proportion
# In[33]:
# Calculating uncertain word proportion
def uncertain_word_prop(uncertainScore,wordcount):
uncertain_word_proportion = 0
if wordcount !=0:
uncertain_word_proportion = uncertainScore / wordcount
return uncertain_word_proportion
# In[34]:
# Calculating constraining word proportion
def constraining_word_prop(constrainingScore,wordcount):
constraining_word_proportion = 0
if wordcount !=0:
constraining_word_proportion = constrainingScore / wordcount
return constraining_word_proportion
# In[35]:
# calculating Constraining words for whole report
def constrain_word_whole(mdaText,qqdmrText,rfText):
wholeDoc = mdaText + qqdmrText + rfText
constrainWordnumWhole =0
rawToken = tokenizer(wholeDoc)
for word in rawToken:
if word in constrainDictionary:
constrainWordnumWhole +=1
sumConstrainScoreWhole = constrainWordnumWhole
return sumConstrainScoreWhole
# In[36]:
inputDirectory = 'D:/data science/Blackcoffer project/test'
masterFile = 'D:/data science/Blackcoffer project/cik_list1.csv'
dataList = rawdata_extract( inputDirectory , masterFile )
df = pd.DataFrame(dataList)
df['mda_positive_score'] = df.mda_extract.apply(positive_score)
df['mda_negative_score'] = df.mda_extract.apply(negative_word)
df['mda_polarity_score'] = np.vectorize(polarity_score)(df['mda_positive_score'],df['mda_negative_score'])
df['mda_average_sentence_length'] = df.mda_extract.apply(average_sentence_length)
df['mda_percentage_of_complex_words'] = df.mda_extract.apply(percentage_complex_word)
df['mda_fog_index'] = np.vectorize(fog_index)(df['mda_average_sentence_length'],df['mda_percentage_of_complex_words'])
df['mda_complex_word_count']= df.mda_extract.apply(complex_word_count)
df['mda_word_count'] = df.mda_extract.apply(total_word_count)
df['mda_uncertainty_score']=df.mda_extract.apply(uncertainty_score)
df['mda_constraining_score'] = df.mda_extract.apply(constraining_score)
df['mda_positive_word_proportion'] = np.vectorize(positive_word_prop)(df['mda_positive_score'],df['mda_word_count'])
df['mda_negative_word_proportion'] = np.vectorize(negative_word_prop)(df['mda_negative_score'],df['mda_word_count'])
df['mda_uncertainty_word_proportion'] = np.vectorize(uncertain_word_prop)(df['mda_uncertainty_score'],df['mda_word_count'])
df['mda_constraining_word_proportion'] = np.vectorize(constraining_word_prop)(df['mda_constraining_score'],df['mda_word_count'])
df['qqdmr_positive_score'] = df.qqd_extract.apply(positive_score)
df['qqdmr_negative_score'] = df.qqd_extract.apply(negative_word)
df['qqdmr_polarity_score'] = np.vectorize(polarity_score)(df['qqdmr_positive_score'],df['qqdmr_negative_score'])
df['qqdmr_average_sentence_length'] = df.qqd_extract.apply(average_sentence_length)
df['qqdmr_percentage_of_complex_words'] = df.qqd_extract.apply(percentage_complex_word)
df['qqdmr_fog_index'] = np.vectorize(fog_index)(df['qqdmr_average_sentence_length'],df['qqdmr_percentage_of_complex_words'])
df['qqdmr_complex_word_count']= df.qqd_extract.apply(complex_word_count)
df['qqdmr_word_count'] = df.qqd_extract.apply(total_word_count)
df['qqdmr_uncertainty_score']=df.qqd_extract.apply(uncertainty_score)
df['qqdmr_constraining_score'] = df.qqd_extract.apply(constraining_score)
df['qqdmr_positive_word_proportion'] = np.vectorize(positive_word_prop)(df['qqdmr_positive_score'],df['qqdmr_word_count'])
df['qqdmr_negative_word_proportion'] = np.vectorize(negative_word_prop)(df['qqdmr_negative_score'],df['qqdmr_word_count'])
df['qqdmr_uncertainty_word_proportion'] = np.vectorize(uncertain_word_prop)(df['qqdmr_uncertainty_score'],df['qqdmr_word_count'])
df['qqdmr_constraining_word_proportion'] = np.vectorize(constraining_word_prop)(df['qqdmr_constraining_score'],df['qqdmr_word_count'])
df['rf_positive_score'] = df.riskfactor_extract.apply(positive_score)
df['rf_negative_score'] = df.riskfactor_extract.apply(negative_word)
df['rf_polarity_score'] = np.vectorize(polarity_score)(df['rf_positive_score'],df['rf_negative_score'])
df['rf_average_sentence_length'] = df.riskfactor_extract.apply(average_sentence_length)
df['rf_percentage_of_complex_words'] = df.riskfactor_extract.apply(percentage_complex_word)
df['rf_fog_index'] = np.vectorize(fog_index)(df['rf_average_sentence_length'],df['rf_percentage_of_complex_words'])
df['rf_complex_word_count']= df.riskfactor_extract.apply(complex_word_count)
df['rf_word_count'] = df.riskfactor_extract.apply(total_word_count)
df['rf_uncertainty_score']=df.riskfactor_extract.apply(uncertainty_score)
df['rf_constraining_score'] = df.riskfactor_extract.apply(constraining_score)
df['rf_positive_word_proportion'] = np.vectorize(positive_word_prop)(df['rf_positive_score'],df['rf_word_count'])
df['rf_negative_word_proportion'] = np.vectorize(negative_word_prop)(df['rf_negative_score'],df['rf_word_count'])
df['rf_uncertainty_word_proportion'] = np.vectorize(uncertain_word_prop)(df['rf_uncertainty_score'],df['rf_word_count'])
df['rf_constraining_word_proportion'] = np.vectorize(constraining_word_prop)(df['rf_constraining_score'],df['rf_word_count'])
df['constraining_words_whole_report'] = np.vectorize(constrain_word_whole)(df['mda_extract'],df['qqd_extract'],df['riskfactor_extract'])
# In[37]:
df.shape
# # Final Output
# In[38]:
inputTextCol = ['mda_extract','qqd_extract','riskfactor_extract']
finalOutput = df.drop(inputTextCol,1)
finalOutput.head(150)
# In[39]:
# Writing to csv file
finalOutput.to_csv('textAnalysisOutput.csv', sep=',', encoding='utf-8')