-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
169 lines (152 loc) · 6.82 KB
/
Copy pathpreprocess.py
File metadata and controls
169 lines (152 loc) · 6.82 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
import json
import logging
from typing import List
import hydra
import tqdm
from omegaconf import DictConfig
def get_questions_re(entity_name: str, context: str, collapse: bool) -> List:
if collapse:
sources = [f'question: is the {entity_name} passively involved in the study? context: {context}',
f'question: is the {entity_name} actively involved in the study? context: {context}']
else:
sources = [f'question: did {entity_name} perform analysis in the study? context: {context}',
f'question: did {entity_name} collect data in the study? context: {context}',
f'question: did {entity_name} coordinate the study? context: {context}',
f'question: did {entity_name} design the study? context: {context}',
f'question: did {entity_name} fund the study? context: {context}',
f'question: did {entity_name} participate in the study? context: {context}',
f'question: did {entity_name} review the study? context: {context}',
f'question: did {entity_name} supply the study? context: {context}',
f'question: did {entity_name} supply data to the study? context: {context}',
f'question: did {entity_name} support the study? context: {context}',
f'question: did {entity_name} write the study? context: {context}']
return sources
def get_target_re(cells: List, collapse: bool) -> List:
targets = []
passive_cells = [cells[5], cells[10], cells[3]]
active_cells = [cells[1], cells[2], cells[4], cells[6],
cells[7], cells[8], cells[9], cells[11]]
if collapse:
total_active_yes = [1 for c in active_cells if 'yes' in c.strip().lower()]
total_passive_yes = [1 for c in passive_cells if 'yes' in c.strip().lower()]
total_active_no = [1 for c in active_cells if 'no' == c.strip().lower()]
total_passive_no = [1 for c in passive_cells if 'no' == c.strip().lower()]
if sum(total_passive_yes) > 0:
targets.append('yes')
elif sum(total_passive_no) > 0:
targets.append('no')
else:
targets.append('unknown')
if sum(total_active_yes) > 0:
targets.append('yes')
elif sum(total_active_no) > 0:
targets.append('no')
else:
targets.append('unknown')
else:
for i in range(1, 12):
targets.append(cells[i])
return targets
def get_question_ent(context: str) -> str:
return f'question: What organizations are involved in the study? context: {context}'
def combine_unlabeled(train_path: str, prediction_path: str) -> List:
f = open(train_path).readlines()
fp = open(prediction_path).readlines()
assert len(f) == len(fp), f'{f} != {fp}'
results = []
for question, answer in tqdm.tqdm(zip(f, fp)):
answer = answer.strip()
question = json.loads(question.strip())['question']
results.append(
json.dumps({
'question': question,
'answer': answer
})
)
return results
def read_unlabeled(path: str, ent_path: str = '', gen_entities: bool = False,
collapse: bool = False) -> List:
f = open(path)
if ent_path != '':
fe = open(ent_path)
else:
fe = None
sources = []
targets = []
if gen_entities:
for line in tqdm.tqdm(f.readlines()):
data = json.loads(line)
sources.append(get_question_ent(data['text']))
targets.append('')
else:
for line, ent_line in tqdm.tqdm(zip(f.readlines(), fe.readlines())):
entities = ent_line.strip().split('|')
data = json.loads(line)
for entity_name in entities:
entity_name = entity_name.strip()
if entity_name != '':
sources.extend(get_questions_re(entity_name, data["text"], collapse))
for i in range(12):
targets.append('')
output_data = []
for src, tgt in zip(sources, targets):
output_data.append(json.dumps({
'question': src,
'answer': tgt
}))
return output_data
def read_raw(path: str, gen_entities: bool = False, collapse: bool = False) -> List:
with open(path) as f:
sources = []
targets = []
for line in tqdm.tqdm(f.readlines()):
data = json.loads(line)
header = True
entities = []
for row in data['summary'].split('<newline>'):
if header:
header = False
else:
cells = row.split('|')
cells = cells[1:len(cells) - 1]
entity_name = cells[0].strip()
entities.append(entity_name)
if not gen_entities:
sources.extend(get_questions_re(entity_name, data["text"], collapse))
targets.extend(get_target_re(cells, collapse))
if gen_entities:
sources.append(get_question_ent(data['text']))
targets.append(' | '.join(entities))
output_data = []
for src, tgt in zip(sources, targets):
output_data.append(json.dumps({
'question': src,
'answer': tgt
}))
return output_data
@hydra.main(version_base=None, config_path='config', config_name='config')
def main(cfg: DictConfig) -> None:
logging.info('Preprocessing start')
if cfg.preprocess.dataset.combine and cfg.preprocess.dataset.unlabeled:
predicted_labels = combine_unlabeled(
train_path=cfg.preprocess.dataset.train_path,
prediction_path=cfg.preprocess.dataset.prediction_path
)
open(cfg.preprocess.dataset.train_output_path, 'w').write('\n'.join(predicted_labels))
elif cfg.preprocess.dataset.unlabeled:
unlabeled = read_unlabeled(
path=cfg.preprocess.dataset.train_path,
ent_path=cfg.preprocess.dataset.ent_path,
gen_entities=cfg.preprocess.generate_entities)
open(cfg.preprocess.dataset.train_output_path, 'w').write('\n'.join(unlabeled))
else:
train = read_raw(path=cfg.preprocess.dataset.train_path,
gen_entities=cfg.preprocess.generate_entities,
collapse=cfg.preprocess.dataset.collapse)
valid = read_raw(path=cfg.preprocess.dataset.valid_path,
gen_entities=cfg.preprocess.generate_entities,
collapse=cfg.preprocess.dataset.collapse)
open(cfg.preprocess.dataset.train_output_path, 'w').write('\n'.join(train))
open(cfg.preprocess.dataset.valid_output_path, 'w').write('\n'.join(valid))
if __name__ == '__main__':
main()