-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6 analysis.py
323 lines (281 loc) · 11.4 KB
/
6 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
#!/usr/bin/env python3
import xlwt
from xlwt import Workbook
import spacy
import operator
import numpy as np
import pandas as pd
from pandas import DataFrame
import json
import re
# INITIALIZING SPACY AND ITS 'en' MODEL
nlp = spacy.load("en_core_web_sm")
# OPENING JSON SENTIMENT DICTIONARY
with open('afinn-165.json') as f:
data = json.load(f)
#CLEANING DATA
# f_stop_words=open("StopWords_GenericLong.txt", "r")
# stop_words=[str(i[0:-1]) for i in f_stop_words]
avoid=['@','#','$','%','^','&','*','(',')','_','=','+','[',']','|','\n','\t','<','>','/']
# READING GLOSSARY EXCEL
def read_standards():
sheet= pd.read_excel("Standards.xlsx")
return sheet
# READING SENTIMENTS EXCEL
def read_sentiments():
sheet= pd.read_excel("Sentiments.xlsx")
return sheet
standards_data=read_standards()
sentiments_data=read_sentiments()
df_standards = pd.DataFrame(standards_data)
df_sentiments = pd.DataFrame(sentiments_data)
# df_sentiments = pd.DataFrame(sentiments_data, columns=['keyword', 'sentiment'])
# print(df_standards)
# print(df_sentiments)
found=df_standards.loc[df_standards['sub-standard'] == "water"].head(1).values.tolist()
if found:
print([found[0][0], found[0][1]])
found=df_sentiments.loc[df_sentiments['keyword'] == "attempt"].head(1).values.tolist()
if found:
print([found[0][0], found[0][1]])
class reports:
def __init__(self, name, path):
self.company = name
self.filepath = path
subject=0
# READING COMPANY REPORTS
def read_file(self):
report= open(self.filepath, "r", encoding = "ISO-8859-1")
report_text= report.read()
return report_text
#print(read_file())
# TOKANIZING REPORT
def tokenify_sentences(self,report):
buff = ''
sentences=[]
for letter in report:
letter=letter.lower()
if letter in avoid:
if buff != '':
sentences.append(buff)
buff = ''
elif (buff is not None):
buff += letter
if buff is not None:
sentences.append(buff)
buff=''
return sentences
#print(tokenify_glossary(read_file()))
#READ SENTENCES
def read_sentences(self,sentences):
master_list=pd.DataFrame(columns=['standards','sub-standard','sentence', 'sentiment'])
for sentence in sentences:
doc=nlp(sentence)
try:
root = [token for token in doc if token.head == token][0]
except:
continue
root=[root]
tree=[]
stakeholders = 0
materiality = self.select_standards(doc)
if materiality:
tree=self.create_tree(root,doc,tree)
sentiment = self.calculate_sentiments(tree)
for sub_standard in materiality:
# print(root,doc,sub_standard[0],sub_standard[1],sentence,sentiment)
data_point=[]
data_point.append(sub_standard[0])
data_point.append(sub_standard[1])
data_point.append(sentence)
data_point.append(sentiment)
to_append = data_point
df_length = len(master_list)
master_list.loc[df_length] = to_append
# print(data_point)
# df_data_point=pd.DataFrame(data_point)
# master_list=master_list.append(df_data_point)
return master_list
#LIST-TREE
def create_tree(self,temp_head,doc,TREE):
# doc = self.break_sentence(doc)
# doc = self.merge_compounds(doc)
# doc = self.combine_commas(doc)
for sub_head in temp_head:
self.track_subjects(sub_head)
if not self.ignore_fillers(sub_head):
# BRANCH =[]
# BRANCH.append(sub_head.i)
# BRANCH.append(sub_head.dep_)
# BRANCH.append(sub_head.text)
# BRANCH.append(sub_head.head.i)
# TREE.append(BRANCH)
TREE.append(sub_head)
sub_tree=[child for child in sub_head.children]
if not sub_tree:
continue
else:
self.create_tree(sub_tree,doc,TREE,)
return TREE
#Merge dep
def merge_compounds(self,doc):
len=0
index_c=0
index_h=0
index_max=doc[-1].i
buffer=doc
for token in doc:
if token.dep_=="compound" or (token.dep_=="conj" and (token.text!="and" or token.text!="or" or token.text!="nor")):
index_c=token.i
index_h=token.head.i
with doc.retokenize() as retokenizer:
if index_h>index_c:
retokenizer.merge(doc[index_c:index_h+1], attrs={"dep":token.head.dep_})
elif index_c>index_h:
retokenizer.merge(doc[index_h:index_c+1], attrs={"dep":token.head.dep_})
if doc!=buffer:
self.merge_compounds(doc)
else:
continue
else:
len=len+1
if len >= index_max:
return doc
#Combine descriptive
def combine_commas(self,doc):
len=0
index_c=0
index_h=0
if doc[-1].i>=3:
index_max=doc[-1].i
for token in doc:
if (token.pos_=="ADV" or token.pos_=="ADJ" or token.pos_=="NOUN" or token.pos_=="VERB" or token.pos_=="PNON")and doc[token.i+1].text=="," and doc[token.i+2].pos_==token.pos_:
index_c=token.i
index_h=token.i+2
with doc.retokenize() as retokenizer:
if index_h<index_max-1:
retokenizer.merge(doc[index_c:index_h+1], attrs={"dep":token.head.dep_})
self.combine_commas(doc)
else:
len=len+1
if len >= index_max:
return doc
#Address sentence breaks
def break_sentence(self,doc):
for token in doc:
if (token.dep_=="conj" and (token.text!="and" or token.text!="or" or token.text!="nor")) :
token.text=","
return doc
#Track subjects and pronouns
def track_subjects(self,sub_head):
if sub_head.dep_=="nsubj" or sub_head.dep_=="csubj":
subject=sub_head.text
elif sub_head.dep_=="PRON" and (sub_head.text=="It" or sub_head.text=="it"):
sub_head.text=subject
elif sub_head.dep_=="PRON" and (sub_head.text!="It" or sub_head.text!="it"):
stakeholders=stakeholders+1
#Ignore dep
def ignore_fillers(self,sub_head):
if sub_head.dep_=="aux" or sub_head.pos_=="DET" or sub_head.pos_=="PUNCT" or sub_head.dep_=="preconj" or sub_head.dep_=="prep":
return 1
else:
return 0
# INDENTIFY MATERIALITY
def select_standards(self,doc):
materiality=[]
for token in doc:
found=df_standards.loc[df_standards['sub-standard'] == token].head(1).values.tolist()
# try:
if found:
# materiality.append([found[0][0], found[0][1]])
sub_standard=[]
sub_standard.append(found[0][0])
sub_standard.append(found[0][1])
materiality.append(sub_standard)
break
else:
found=df_standards.loc[df_standards['text'] == token.lemma_].head(1).values.tolist()
if found:
# materiality.append([found[0][0], found[0][1]])
sub_standard=[]
sub_standard.append(found[0][0])
sub_standard.append(found[0][1])
materiality.append(sub_standard)
else:
continue
# except:
# pass
if not materiality:
return 0
return materiality
#CALCULATE SENTIMENTS
def calculate_sentiments(self,TREE):
#add afinn
count_descriptive_words=0
sentiment=0
for item in reversed(TREE):
item_sentiment=0
try:
found=df_sentiments.loc[df_sentiments["keyword"] == item.lemma_].head(1).values.tolist()
if found:
item_sentiment=item_sentiment+found[0][1]
count_descriptive_words=count_descriptive_words+1
elif item in data:
item_sentiment=item_sentiment+data[item]
count_descriptive_words=count_descriptive_words+1
except TypeError:
pass
if not item_sentiment:
item_sentiment=1
if str(item)=="not" or str(item)=="nor":
print(item)
sentiment=item_sentiment+(-1)*sentiment
else:
sentiment=item_sentiment+sentiment
# print(sentiment,count_descriptive_words)
if count_descriptive_words:
sentiment=sentiment/count_descriptive_words
return sentiment
# #CALCULATE SENTIMENTS
# def calculate_sentiments(self,TREE):
# count_descriptive_words=0
# sentiment=0
# # TREE=TREE.inverse()
# for items in reversed(TREE):
# item_sentiment=0
# try:
# items=re.split('and ,',items)
# for item in items:
# found=df_sentiments.loc[df_sentiments['keyword'] == item].head(1).values.tolist()
# if found:
# item_sentiment=item_sentiment+1
# count_descriptive_words=count_descriptive_words+1
# except TypeError:
# pass
# # items.split("and",",")
# if not item_sentiment:
# item_sentiment=1
# sentiment=item_sentiment+len(items)*sentiment
# return sentiment
# CREATE DATABASE
def create_database(self,df_master_list):
# database to excel
with pd.ExcelWriter(self.company+"database.xlsx") as writer:
df_master_list.to_excel(writer)
writer.save()
HUL = reports("HUL", "HUL 2018-2019_Annual Report.txt")
print(HUL.create_database(HUL.read_sentences(HUL.tokenify_sentences(HUL.read_file()))))
Colgate = reports("Colgate", "Colgate 2018-2019_Annual Report.txt")
print(Colgate.create_database(Colgate.read_sentences(Colgate.tokenify_sentences(Colgate.read_file()))))
ITC = reports("ITC", "ITC 2018-2019 Annual Report.txt")
print(ITC.sort_glossary(ITC.divide_glossary(ITC.tokenify_glossary(ITC.read_file()))))
Dabur = reports("Dabur", "Dabur 2018-19_Annual Report.txt")
print(Dabur.sort_glossary(Dabur.divide_glossary(Dabur.tokenify_glossary(Dabur.read_file()))))
Godrej = reports("Godrej", "Godrej 2018-2019_Annual Report.txt")
print(Godrej.sort_glossary(Godrej.divide_glossary(Godrej.tokenify_glossary(Godrej.read_file()))))
Marico = reports("Marico", "Marico 2018-2019_Annual Report.txt")
print(Marico.sort_glossary(Marico.divide_glossary(Marico.tokenify_glossary(Marico.read_file()))))
Nestle = reports("Nestle", "Nestle 2017-2018_Annual Report.txt")
print(Nestle.sort_glossary(Nestle.divide_glossary(Nestle.tokenify_glossary(Nestle.read_file()))))
PnG = reports("PnG", "P&G 2018-2019_Annual Report.txt")
print(PnG.sort_glossary(PnG.divide_glossary(PnG.tokenify_glossary(PnG.read_file()))))