-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·46 lines (33 loc) · 1.15 KB
/
utils.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
DOMAIN_DATA_DIR = "multi-domain"
def read_file(fname, transform=lambda x: x):
data = []
with open(fname) as f:
for line in f:
data.append(transform(line.strip()))
return data
def convert_input_to_template(prompts):
return (" </s> ").join([f"{prompt.data['src']} = {prompt.data['tgt']}" for prompt in prompts])
def get_data(domain, src_lang, tgt_lang, split):
src = read_file(f"{DOMAIN_DATA_DIR}/{domain}/{split}.{src_lang}")
tgt = read_file(f"{DOMAIN_DATA_DIR}/{domain}/{split}.{tgt_lang}")
return src, tgt
class FewShotSample(object):
def __init__(
self,
data,
correct_candidates=None,
):
self._data = data
self._correct_candidates = correct_candidates
def __getitem__(self, key):
return self._data[key]
def __contains__(self, item):
return item in self._data
@property
def correct_candidates(self):
return self._correct_candidates
def is_correct(self, candidate):
return candidate in self.correct_candidates
@property
def data(self):
return self._data