-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 tokanization and lemmatization.py
288 lines (231 loc) · 10.1 KB
/
1 tokanization and lemmatization.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
#!/usr/bin/env python3
import xlwt
from xlwt import Workbook
import spacy
import operator
import numpy as np
import math
import pandas as pd
from pandas import DataFrame
import json
# INITIALIZING SPACY AND ITS 'en' MODEL
nlp = spacy.load("en_core_web_sm")
# OPENING JSON SENTIMENT DICTIONARY
with open('afinn-165.json') as f:
items_afinn = json.load(f)
# OPENING LOUGHRAN MCDONALD SENTIMENT WORD LIST
file_path = 'LoughranMcDonald_SentimentWordLists_2018.xlsx'
items_mcdonals=[0]*8
for i in range(1,8):
items_mcdonals[i-1]=pd.read_excel(file_path, sheet_name=i)
items_mcdonals[i-1]=items_mcdonals[i-1].values.tolist()
items_mcdonals[i-1] = [item.lower() for sublist in items_mcdonals[i-1] for item in sublist]
#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','<','>','/']
#ABBREVIATIONS TO RETAIN
abbreviations=['co2','ch4','n2o','hfcs', 'pfcs', 'sf6', 'nf3', 'pop', 'voc', 'hap', 'pm', 'gwp', 'cfc11', 'ods', 'nox', 'so', 'mwh', 'kw', 'ir', 'odr', 'ldr', 'ar', 'ilo', 'oecd', 'who', 'lgbt', 'csr', "iccs", 'gst', 'ebitda','ifrs', 'iasb', 'ipsas', 'ifac', 'evgd', 'pl', 'eca']
class reports:
def __init__(self, name, path):
self.company = name
self.filepath = path
# READING COMPANY REPORTS
def read_file(self):
report= open(self.filepath, "r", encoding = "ISO-8859-1")
report_text= report.read()
return report_text
# TOKANIZING REPORT
def tokenify_glossary(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
#GLORRARY SRANDARDS
def divide_glossary(self,sentences):
glossary_nouns = []
glossary_verbs = []
glossary_adverbs = []
glossary_adjectives = []
POS=[]
for sentence in sentences:
doc = nlp(sentence)
print(math.trunc(sentences.index(sentence)/len(sentences)*100))
for token in doc:
word=[]
word.append(token.text)
word.append(token.lemma_)
word.append(token.pos_)
#afinn sentiments
if token.text in items_afinn: #apoorva create a function for this later
word.append(items_afinn[token.text])
else:
word.append('')
#parts of speech segregation
if token.pos_ == 'NOUN' or token.pos_ == 'PROPN':
glossary_nouns.append(word)
elif token.pos_ == 'VERB':
glossary_verbs.append(word)
elif token.pos_ == 'ADV' or token.pos_ == 'ADP':
glossary_adverbs.append(word)
elif token.pos_ == 'ADJ':
glossary_adjectives.append(word)
# 3D List
POS.append(glossary_nouns)
POS.append(glossary_verbs)
POS.append(glossary_adverbs)
POS.append(glossary_adjectives)
return POS
#RETAINING ABBREVIATIONS
def is_abbreviation(self,word):
if word in abbreviations:
return 1
else:
return 0
#REDUCE DUPLICATE WORDS AND FREQUENCY
def reduce_glossary(self,sorted_words):
glossary=[]
while(len(sorted_words)>0):
abb=0
count=1
word_frequency=[]
while(len(sorted_words)>1 and sorted_words[0][1]==sorted_words[1][1]):
count=count+1
sorted_words=np.delete(sorted_words, 1, 0)
if self.is_abbreviation(sorted_words[0][0]) == 1:
word_frequency.append(count)
word_frequency.extend(sorted_words[0])
glossary.append(word_frequency)
abb=1
if sorted_words[0][0] not in stop_words and len(sorted_words[0][0])>2 and sorted_words[0][0].isalpha() and abb==0:
word_frequency.append(count)
word_frequency.extend(sorted_words[0])
glossary.append(word_frequency)
sorted_words=np.delete(sorted_words, 0, 0)
return glossary
#CREATE GLORRARY
def sort_glossary(self,POS):
sorted_POS=[]
unsorted_nouns = np.array(POS[0])
sorted_nouns=unsorted_nouns[unsorted_nouns[:, 1].argsort()]
sorted_nouns=self.reduce_glossary(sorted_nouns)
df_nouns = pd.DataFrame(sorted_nouns)
df_nouns.columns=['frequency','text','lemma','pos','afinn sentiment']
unsorted_verbs = np.array(POS[1])
sorted_verbs=unsorted_verbs[unsorted_verbs[:, 1].argsort()]
sorted_verbs=self.reduce_glossary(sorted_verbs)
df_verbs = pd.DataFrame(sorted_verbs)
df_verbs.columns=['frequency','text','lemma','pos','afinn sentiment']
unsorted_adverbs = np.array(POS[2])
sorted_adverbs=unsorted_adverbs[unsorted_adverbs[:, 1].argsort()]
sorted_adverbs=self.reduce_glossary(sorted_adverbs)
df_adverbs = pd.DataFrame(sorted_adverbs)
df_adverbs.columns=['frequency','text','lemma','pos','afinn sentiment']
unsorted_adjectives = np.array(POS[3])
sorted_adjectives=unsorted_adjectives[unsorted_adjectives[:, 1].argsort()]
sorted_adjectives=self.reduce_glossary(sorted_adjectives)
df_adjective = pd.DataFrame(sorted_adjectives)
df_adjective.columns=['frequency','text','lemma','pos','afinn sentiment']
sorted_POS.append(sorted_nouns)
sorted_POS.append(sorted_verbs)
sorted_POS.append(sorted_adverbs)
sorted_POS.append(sorted_adjectives)
#glossary to excel
with pd.ExcelWriter("companies_glossary/"+self.company+".xlsx") as writer:
df_nouns.to_excel(writer, sheet_name='Nouns')
df_verbs.to_excel(writer, sheet_name='Verbs')
df_adverbs.to_excel(writer, sheet_name='Adverbs')
df_adjective.to_excel(writer, sheet_name='Adjectives')
writer.save()
return sorted_POS
HUL = reports("HUL", "HUL 2018-2019_Annual Report.txt")
print(HUL.sort_glossary(HUL.divide_glossary(HUL.tokenify_glossary(HUL.read_file()))))
Colgate = reports("Colgate", "Colgate 2018-2019_Annual Report.txt")
print(Colgate.sort_glossary(Colgate.divide_glossary(Colgate.tokenify_glossary(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()))))
class glossary:
def __init__(self, glossary, path):
self.glossary = glossary
self.filepath = path
# READING COMPANY REPORTS
def read_sheet(self):
sheet=[]
sheet= pd.read_excel(self.filepath)
sheet = sheet.values.tolist()
print(sheet)
return sheet
#GLORRARY SRANDARDS
def divide_glossary(self,sheet):
glossary_nouns = []
glossary_verbs = []
glossary_adverbs = []
glossary_adjectives = []
POS=[]
for item in sheet:
doc = nlp(item[2])
for token in doc:
word=[]
word.append(item[0])
word.append(item[1])
word.append(token.text)
word.append(token.lemma_)
word.append(token.pos_)
#parts of speech segregation
if token.pos_ == 'NOUN' or token.pos_ == 'PROPN':
glossary_nouns.append(word)
elif token.pos_ == 'VERB':
glossary_verbs.append(word)
elif token.pos_ == 'ADV' or token.pos_ == 'ADP':
glossary_adverbs.append(word)
elif token.pos_ == 'ADJ':
glossary_adjectives.append(word)
# 3D List
POS.append(glossary_nouns)
POS.append(glossary_verbs)
POS.append(glossary_adverbs)
POS.append(glossary_adjectives)
return POS
#CREATE GLORRARY
def create_glossary(self,POS):
nouns = POS[0]
df_nouns = pd.DataFrame(nouns)
df_nouns.columns=['standard','sub-standard','text','lemma','pos']
verbs = POS[1]
df_verbs = pd.DataFrame(verbs)
df_verbs.columns=['standard','sub-standard','text','lemma','pos']
adverbs = POS[2]
df_adverbs = pd.DataFrame(adverbs)
df_adverbs.columns=['standard','sub-standard','text','lemma','pos']
adjectives = POS[3]
df_adjectives = pd.DataFrame(adjectives)
df_adjectives.columns=['standard','sub-standard','text','lemma','pos']
#glossary to excel
with pd.ExcelWriter("companies_glossary/faulteredglossary.xlsx") as writer:
df_nouns.to_excel(writer, sheet_name='Nouns')
df_verbs.to_excel(writer, sheet_name='Verbs')
df_adverbs.to_excel(writer, sheet_name='Adverbs')
df_adjectives.to_excel(writer, sheet_name='Adjectives')
writer.save()
Gl = glossary("Glossary", "consolidatedkeywords.xlsx")
print(Gl.create_glossary(Gl.divide_glossary(Gl.read_sheet())))