-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathassignment.py
316 lines (205 loc) · 8.74 KB
/
assignment.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import requests
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize
from bs4 import BeautifulSoup
import re
import nltk
# In[2]:
nltk.download('punkt')
nltk.download("stopwords")
# In[3]:
df = pd.read_excel('cik_list.xlsx')
#df.head()
# In[4]:
y = 'https://www.sec.gov/Archives/'
links = [y+x for x in df['SECFNAME']]
#print(links)
# In[ ]:
print('Downloading reports...')
reports = []
for url in links:
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "html.parser")
reports.append(soup.get_text())
print(f'Total {len(reports)} reports saved')
# In[ ]:
with open('StopWords_Generic.txt','r') as f:
stop_words = f.read()
stop_words = stop_words.split('\n')
print(f'Total number of Stop Words are {len(stop_words)}')
#print(stop_words)
# In[ ]:
master_dic = pd.read_excel('LoughranMcDonald_MasterDictionary_2018.xlsx')
#master_dic.head()
# In[ ]:
positive_dictionary = [x for x in master_dic[master_dic['Positive'] != 0]['Word']]
negative_dictionary = [x for x in master_dic[master_dic['Negative'] != 0]['Word']]
print(f"Total positve words in dictionary are {len(positive_dictionary)}")
print(f"Total negative words in dictionary are {len(negative_dictionary)}")
# In[ ]:
uncertainity = pd.read_excel('uncertainty_dictionary.xlsx')
uncertainity_words = list(uncertainity['Word'])
constraining = pd.read_excel('constraining_dictionary.xlsx')
constraining_words = list(constraining['Word'])
# In[ ]:
def tokenize(text):
text = re.sub(r'[^A-Za-z]',' ',text.upper())
tokenized_words = word_tokenize(text)
return tokenized_words
def remove_stopwords(words, stop_words):
return [x for x in words if x not in stop_words]
def countfunc(store, words):
score = 0
for x in words:
if(x in store):
score = score+1
return score
def sentiment(score):
if(score < -0.5):
return 'Most Negative'
elif(score >= -0.5 and score < 0):
return 'Negative'
elif(score == 0):
return 'Neutral'
elif(score > 0 and score < 0.5):
return 'Positive'
else:
return 'Very Positive'
def polarity(positive_score, negative_score):
return (positive_score - negative_score)/((positive_score + negative_score)+ 0.000001)
def subjectivity(positive_score, negative_score, num_words):
return (positive_score+negative_score)/(num_words+ 0.000001)
def syllable_morethan2(word):
if(len(word) > 2 and (word[-2:] == 'es' or word[-2:] == 'ed')):
return False
count =0
vowels = ['a','e','i','o','u']
for i in word:
if(i.lower() in vowels):
count = count +1
if(count > 2):
return True
else:
return False
def fog_index_cal(average_sentence_length, percentage_complexwords):
return 0.4*(average_sentence_length + percentage_complexwords)
# In[ ]:
sections = ["Management's Discussion and Analysis",
"Quantitative and Qualitative Disclosures about Market Risk\n",
"Risk Factors\n"]
caps = [x.upper() for x in sections]
caps.extend(sections)
# In[ ]:
col = ['mda','qqdmr','rf']
var = ['positive_score',
'negative_score',
'polarity_score',
'average_sentence_length',
'percentage_of_complex_words',
'fog_index',
'complex_word_count',
'word_count',
'uncertainity_score',
'constraining_score',
'positive_word_proportion',
'negative_word_proportion',
'uncertainity_word_proportion',
'constraining_word_proportion',
'constraining_words_whole_report']
for c in col:
for v in var[:-1]:
df[c+'_'+v] = 0.0
df[var[-1]] = 0.0
# In[ ]:
#df.head()
# In[ ]:
section_map = {i:j for i,j in zip(sections, col)}
s_map = {i.upper():j for i,j in zip(sections, col)}
section_map.update(s_map)
#print(section_map)
# In[ ]:
for i in range(len(reports)):
print(f'{i}th row processing')
text = re.sub('Item','ITEM',reports[i])
for j in caps:
x = re.search('ITEM\s+[\d]\(*[A-Za-z]*\)*.*\s+\-*\s*'+j, text)
if x:
start,end = x.span()
content = (text[start:]).split('ITEM')[1]
if ('...' not in content) and ('. . .' not in content) and len(content) > 100:
tokenized_words = tokenize(content)
#print(f'Total tokenized words are {len(tokenized_words)}')
words = remove_stopwords(tokenized_words, stop_words)
num_words = len(words)
#print(f'Total words after removing stop words are {len(words)}')
positive_score = countfunc(positive_dictionary, words)
negative_score = countfunc(negative_dictionary, words)
#print(f'Total positive score is {positive_score}')
#print(f'Total negative score is {negative_score}')
polarity_score = polarity(positive_score, negative_score)
#print(polarity_score)
subjectivity_score = subjectivity(positive_score, negative_score, num_words)
#print(subjectivity_score)
#print(sentiment(polarity_score))
sentences = sent_tokenize(content)
num_sentences = len(sentences)
average_sentence_length = num_words/num_sentences
#print(average_sentence_length)
num_complexword =0
uncertainity_score = 0
constraining_score = 0
for word in words:
if(syllable_morethan2(word)):
num_complexword = num_complexword+1
if(word in uncertainity_words):
uncertainity_score = uncertainity_score+1
if(word in constraining_words):
constraining_score = constraining_score+1
#print(num_complexword)
#print(uncertainity_score)
#print(constraining_score)
percentage_complexwords = num_complexword/num_words
#print(percentage_complexwords)
fog_index = fog_index_cal(average_sentence_length, percentage_complexwords)
#print(fog_index)
positive_word_proportion = positive_score/num_words
negative_word_proportion = negative_score/num_words
uncertainity_word_proportion = uncertainity_score/num_words
constraining_word_proportion = constraining_score/num_words
#print(positive_word_proportion)
#print(negative_word_proportion)
#print(uncertainity_word_proportion)
#print(constraining_word_proportion)
df.at[i,section_map[j]+'_positive_score'] = positive_score
df.at[i,section_map[j]+'_negative_score'] = negative_score
df.at[i,section_map[j]+'_polarity_score'] = polarity_score
df.at[i,section_map[j]+'_average_sentence_length'] = average_sentence_length
df.at[i,section_map[j]+'_percentage_of_complex_words'] = percentage_complexwords
df.at[i,section_map[j]+'_fog_index'] = fog_index
df.at[i,section_map[j]+'_complex_word_count'] = num_complexword
df.at[i,section_map[j]+'_word_count'] = num_words
df.at[i,section_map[j]+'_uncertainity_score'] = uncertainity_score
df.at[i,section_map[j]+'_constraining_score'] = constraining_score
df.at[i,section_map[j]+'_positive_word_proportion'] = positive_word_proportion
df.at[i,section_map[j]+'_negative_word_proportion'] = negative_word_proportion
df.at[i,section_map[j]+'_uncertainity_word_proportion'] = uncertainity_word_proportion
df.at[i,section_map[j]+'_constraining_word_proportion'] = constraining_word_proportion
constraining_words_whole_report = 0
tokenized_report_words = tokenize(reports[i])
report_words = remove_stopwords(tokenized_report_words, stop_words)
for word in report_words:
if word in constraining_words:
constraining_words_whole_report = 1+ constraining_words_whole_report
#print(constraining_words_whole_report)
df.at[i,'constraining_words_whole_report'] = constraining_words_whole_report
# In[ ]:
#df.head()
# In[ ]:
df.to_excel('output.xlsx')
print('File saved')