-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_excel.py
198 lines (140 loc) · 4.7 KB
/
convert_excel.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
import sys
import os
import pandas as pd
import csv
from pathlib import Path
from io import BytesIO
from pyxlsb import open_workbook as open_xlsb
import xlsxwriter
spamList = []
tokenID =1
textIndex = 3
smell_word_tag = "Smell_Word"
frameElements = "Smell_Source,Quality,Odour_Carrier,Evoked_Odorant,Location,Perceiver,Time,Circumstances,Effect".split(",")
def to_excel(df):
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, index=False, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']
format1 = workbook.add_format({'num_format': '0.00'})
worksheet.set_column('A:A', None, format1)
writer.close()
processed_data = output.getvalue()
return processed_data
def pocess_annotations(myAnnotations:list):
annotationsDict = dict()
outputDict = dict()
s = [a for a in myAnnotations if smell_word_tag in " ".join(a)]
# save all annotations in annotationsDict
if len(s) == 0:
return(None)
annotationsDict[smell_word_tag] = s
for f in frameElements:
fe = [a for a in myAnnotations if f in " ".join(a)]
annotationsDict[f] = fe
#parse the annotations in annotationsDict merging tokens in the same span and puth them in outputDict
# smell words:
outputDict[smell_word_tag] = []
firstID = int(annotationsDict[smell_word_tag][0][tokenID].split("-")[1])-1
span = []
for sw in annotationsDict[smell_word_tag]:
myID = sw[tokenID].split("-")[1]
myToken = sw[textIndex]
if int(myID) == int(firstID)+1:
span.append(myToken)
firstID = myID
else:
outputDict[smell_word_tag].append(" ".join(span))
firstID = myID
span = []
span.append(myToken)
outputDict[smell_word_tag].append(" ".join(span))
#frame-elements
for frameElement in frameElements:
span = []
outputDict[frameElement] = []
if len(annotationsDict[frameElement]) == 0:
continue
firstID = int(annotationsDict[frameElement][0][tokenID].split("-")[1])-1
for f in annotationsDict[frameElement]:
myID = f[tokenID].split("-")[1]
myToken = f[textIndex]
if int(myID) == int(firstID)+1:
span.append(myToken)
firstID = myID
else:
if " ".join(span).lower() not in spamList:
outputDict[frameElement].append(" ".join(span))
firstID = myID
span = []
span.append(myToken)
if " ".join(span).lower() not in spamList:
outputDict[frameElement].append(" ".join(span))
return(outputDict)
def dictToString (myDict):
myList = []
myList.append("|".join(myDict[smell_word_tag]))
for f in frameElements:
myList.append("|".join(myDict[f]))
return("\t".join(myList)+"\t")
def make_excel(myFile):
annotations_list = []
sentence_list = []
all_lines = []
counter = 0
all_annotations_dict = dict()
all_sentences_dict = dict()
all_sentences_dict[0] = []
all_titles_dict = dict()
with open(myFile, 'r') as file:
for line in file:
line = line.strip()
parts = line.split("\t")
if line == "":
counter += 1
all_annotations_dict[counter] = annotations_list
all_sentences_dict[counter] = sentence_list
all_titles_dict[counter] = title
sentence_list = []
annotations_list = []
continue
title = parts[0]
try:
sentence_list.append(parts[textIndex])
except:
aaaa=1
for p in parts[textIndex+1:]:
if p != "O":
annotations_list.append(parts)
continue
all_sentences_dict[counter+1] = []
for i in all_annotations_dict:
# print(i)
# print(i,all_titles_dict[i])
# print(i,all_sentences_dict[i])
# print(i,all_annotations_dict[i])
# print()
annotations_list = all_annotations_dict[i]
sentence_list_before = all_sentences_dict[i-1]
sentence_list = all_sentences_dict[i]
sentence_list_after = all_sentences_dict[i+1]
title = all_titles_dict[i]
if len(annotations_list) > 1:
dictAnnotations = pocess_annotations(annotations_list)
if dictAnnotations != None:
stringToPrint = dictToString(dictAnnotations)
tmpString = title+"\t"+stringToPrint+" ".join(sentence_list_before)+"\t"+" ".join(sentence_list)+"\t"+" ".join(sentence_list_after)
parts = tmpString.split("\t")
all_lines.append(parts)
if "\t\t" not in stringToPrint:
tmpString = title+"\t"+stringToPrint+" ".join(sentence_list_before)+"\t"+" ".join(sentence_list)+"\t"+" ".join(sentence_list_after)
parts = tmpString.split("\t")
all_lines.append(parts)
myHeaderString = "Book\t"+smell_word_tag+"\t"+"\t".join(frameElements)+"\tSentenceBefore\tSentence\tSentenceAfter"
myHeader = myHeaderString.split("\t")
df = pd.DataFrame(all_lines)
df.columns=myHeader[:len(df.columns)]
# df.to_excel(Path(myOut), sheet_name="FrameElements")
# df.to_csv(outPath, index=False)
return(df)