-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExcelToText.py
More file actions
185 lines (139 loc) · 5.83 KB
/
Copy pathExcelToText.py
File metadata and controls
185 lines (139 loc) · 5.83 KB
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
'''
Copyright (c) 2018 MD ATAUR RAHMAN
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
# Read the Excel File and copy the label and document
def generateTextFile(filename):
document = []
label = []
df = pd.read_excel(filename, sheet_name='Sheet1')
print("Column headings:")
print(df.columns)
# print(df['Unnamed: 2'])
for lbl, doc in zip(df['Unnamed: 2'][8:], df['Unnamed: 1'][8:]):
lbl = str(lbl)
doc = str(doc)
if check_Labels(lbl):
label.append(lbl)
document.append(doc)
# print(str(lbl)+" "+str(doc))
return document, label
# Append the contents of 3 files in a single document and labels
def append_Doc_Lbl(document, label, doc_new, lbl_new):
for doc, lbl in zip(doc_new, lbl_new):
document.append(doc)
label.append(lbl)
return document, label
# Checking and correcting the labels
def check_Labels(lbl):
# There are also some rubbish labels so a simple string compare with empty field won't work
if lbl == 'sad' or lbl == 'happy' or lbl == 'disgust' or lbl == 'surprise' or lbl == 'fear' or lbl == 'angry':
return True
return False
def create_balanced_dataset(label, document):
out_train = []
out_test = []
everything_else = []
count = [0, 0, 0, 0, 0 ,0]
# First 300 to trainset, upto second 100 to testset, everything else to separate list
for lbl, doc in zip(label, document):
# out.append(lbl+" "+doc)
if lbl == 'sad':
if count[0]<1000:
out_train.append(lbl+" "+doc)
elif count[0]<1000+200:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[0]+=1
elif lbl == 'happy':
if count[1]<1500:
out_train.append(lbl+" "+doc)
elif count[1]<1500+300:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[1]+=1
elif lbl == 'disgust':
if count[2]<500:
out_train.append(lbl+" "+doc)
elif count[2]<500+100:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[2]+=1
elif lbl == 'surprise':
if count[3]<400:
out_train.append(lbl+" "+doc)
elif count[3]<400+80:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[3]+=1
elif lbl == 'fear':
if count[4]<300:
out_train.append(lbl+" "+doc)
elif count[4]<300+60:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[4]+=1
elif lbl == 'angry':
if count[5]<1000:
out_train.append(lbl+" "+doc)
elif count[5]<1000+200:
out_test.append(lbl+" "+doc)
else:
everything_else.append(lbl+" "+doc)
count[5]+=1
return out_train, out_test, everything_else
def main():
document = []
label = []
doc, lbl = generateTextFile('corpus/EkattorTV_Comments.xlsx')
document, label = append_Doc_Lbl(document, label, doc, lbl)
doc, lbl = generateTextFile('corpus/Magistrate_Comments.xlsx')
document, label = append_Doc_Lbl(document, label, doc, lbl)
doc, lbl = generateTextFile('corpus/ImranHSarker_Comment.xlsx')
document, label = append_Doc_Lbl(document, label, doc, lbl)
# for doc, lbl in zip(doc[:10], lbl[:10]):
# print(str(lbl)+" "+str(doc))
print("Total Number of Documents and Labels:")
print("Documents = {0}".format(len(document)))
print("Labels = {0}".format(len(label)))
# combining the label and documents in the same string
# out = []
# for lbl, doc in zip(label, document):
# out.append(lbl+" "+doc)
#
# # creating the corpus_all.txt file
# with open("corpus_all.txt", 'w', encoding='utf-8') as f:
# f.write('\n'.join(out))
# Creating a balanced training set
out_train, out_test, everything_else = create_balanced_dataset(label, document)
# creating the separate training, testing and other file
with open("train.txt", 'w', encoding='utf-8') as f:
f.write('\n'.join(out_train))
with open("test.txt", 'w', encoding='utf-8') as f:
f.write('\n'.join(out_test))
with open("everything_else.txt", 'w', encoding='utf-8') as f:
f.write('\n'.join(everything_else))
if __name__ == '__main__':
main()