-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.py
More file actions
209 lines (155 loc) · 5.13 KB
/
FileReader.py
File metadata and controls
209 lines (155 loc) · 5.13 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import csv
import json
import string
import os
VOCABULARY = set()
IS_LABEL_INT = False
WORD_INDEX = {}
PROCESS_INDEXES = [
2, # TITLE
3, # ABSTRACT
]
PROCESS_FIELDS = {'title', 'abstract'}
CLASSES = [
'treatment', # 0
'diagnosis', # 1
'prevention', # 2
'mechanism', # 3
'transmission', # 4
'epidemic forecasting', # 5
'case report' # 6
]
TOTAL_CLASS_NUM = 7
with open('stopwords.txt') as fp: # Load Stopwords
STOPWORDS = set(fp.read().splitlines())
class Document:
def __init__(self, input :list):
self.pmid: int = input[0]
self.journal: str = input[1]
self.title: list = input[2]
self.abstract: list = input[3]
self.keywords: list = input[4]
self.pub_type: list = input[5]
self.authors: list = input[6]
self.doi: str = input[7]
self.label: list = input[8]
def get_bag_of_words(self):
ret = {}
words = self.title + self.abstract
for word in words:
if word in ret.keys():
ret[word] = ret[word] + 1
else:
ret[word] = 1
return ret
def to_string(self):
return_val = {
"pmid": self.pmid,
"title": self.title,
"abstract": self.abstract,
"label": self.label
}
return json.dumps(return_val)
def is_equal(self, other):
if self.pmid == other.pmid:
return True
return False
def get_train_and_test_data_as_list() -> tuple:
train_filename = "./Datasets/BC7-LitCovid-Train.csv"
test_filename = "./Datasets/BC7-LitCovid-Dev.csv"
train_data = read_csv(train_filename)
train_data = normalize_data(train_data, IS_LABEL_INT)
test_data = read_csv(test_filename)
test_data = normalize_data(test_data, IS_LABEL_INT)
train_data = [Document(row) for row in train_data]
test_data = [Document(row) for row in test_data]
return train_data, test_data
def read_csv(file_name:str) -> list:
data = csv.reader(open(file_name, "rt"))
return list(data)[1:]
def normalize_data(data: list, labels_int: bool) -> list:
for data_index in PROCESS_INDEXES:
for i in range(len(data)):
data[i][data_index] = get_words(data[i][data_index])
update_labels(data, labels_int)
return data
def get_words(input: str, is_remove_stopwords: bool = True) -> list:
input_words = input.translate(str.maketrans('', '', string.punctuation)).lower()
if is_remove_stopwords:
input_words = remove_stopwords(input_words.split())
return input_words
def remove_stopwords(words: list) -> list:
ret = []
for word in words:
if word not in STOPWORDS:
ret.append(word)
return ret
def update_labels(original_data, is_int: bool):
for i in range(len(original_data)):
labels = original_data[i][-1]
labels = labels.lower()
labels = labels.split(';')
original_data[i][-1] = encode_label(labels) if is_int else labels
assert original_data[i][-1]
def encode_label(labels: str) -> int:
ret = 0
for j in range(len(CLASSES)):
if CLASSES[j] in labels:
ret = ret | (1 << j)
return ret
def create_dump_path(path):
os.makedirs(os.path.dirname(path), exist_ok=True)
def dump(train_list: list, test_list: list):
path = os.path.join(".", "dump_files", "")
path_train = os.path.join(".", "dump_files", "train")
path_test = os.path.join(".", "dump_files", "test")
create_dump_path(path)
docnum: int = 0
train_dump = open(path_train, "w")
to_write = {}
for single_doc in train_list:
single_dict = single_doc.to_string()
to_write[docnum] = single_dict
docnum = docnum + 1
json.dump(to_write, train_dump)
train_dump.close()
docnum = 0
test_dump = open(path_test, "w")
to_write = {}
for single_doc in test_list:
single_dict = single_doc.to_string()
to_write[docnum] = single_dict
docnum = docnum + 1
json.dump(to_write, test_dump)
test_dump.close()
def write_dump_files():
train_data, test_data = get_train_and_test_data_as_list()
dump(train_data, test_data)
def read_dump_files():
train_list = []
test_list = []
raw_file_train = open("./dump_files/train")
raw = json.load(raw_file_train)
for i in range(len(raw)):
key = str(i)
raw_doc = json.loads(raw[key])
pmid = raw_doc["pmid"]
title = raw_doc["title"]
abstract = raw_doc["abstract"]
label = raw_doc["label"]
params = [pmid, "", title, abstract, "", "", "", "", label]
train_list.append(Document(params))
raw_file_train.close()
raw_file_test = open("./dump_files/test")
raw = json.load(raw_file_test)
for i in range(len(raw)):
key = str(i)
raw_doc = json.loads(raw[key])
pmid = raw_doc["pmid"]
title = raw_doc["title"]
abstract = raw_doc["abstract"]
label = raw_doc["label"]
params = [pmid, "", title, abstract, "", "", "", "", label]
test_list.append(Document(params))
raw_file_test.close()
return train_list, test_list