-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
145 lines (108 loc) · 4.42 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
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
import json
import os
import yaml
import sys
import glob
from datasets import load_dataset, Dataset, DatasetDict
MAFAND_LANGUAGE_NAMES = {
"amh": "Amharic",
"hau": "Hausa",
"kin": "Kinyarwanda",
"lug": "Luganda",
"luo": "Luo",
"nya": "Chichewa",
"pcm": "Nigerian Pidgin",
"sna": "Shona",
"swa": "Swahili",
"tsn": "Setswana",
"twi": "Twi",
"xho": "Xhosa",
"yor": "Yoruba",
"zul": "Zulu"
}
def load_config(config_path, task_name):
if task_name in ["mafand_e2t", "mafand_t2e"]:
task_name = "mafand"
with open(os.path.join(config_path, f'{task_name}.yaml'), 'r') as config_file:
data_config = yaml.safe_load(config_file)
return data_config
def get_predictions_from_file(filename):
dict = json.load(open(filename, 'r'))
return [sample['prediction'] for sample in dict.values()]
def process_xcopa(dataset):
"""XGLM template"""
def _process(example):
# input: (1) remove the comma at the end of the premise (2) add the connecting words
example['premise'] = example["premise"].strip()[:-1]
example['choice1'] = example['choice1'][0].lower() + example['choice1'][1:]
example['choice2'] = example['choice2'][0].lower() + example['choice2'][1:]
conn = 'because' if example['question'] == 'cause' else 'so'
example['premise'] = example["premise"] + f" {conn}"
return example
return dataset.map(_process)
def process_xcopa2(dataset):
"""PromptSource template (for instruction tuning models)"""
def _process(example):
conn = 'This happened because...' if example['question'] == 'cause' else 'As a consequence...'
example['premise'] = example["premise"] + f" {conn}"
return example
return dataset.map(_process)
def process_qa(dataset):
def _process(example):
example['answers'] = example["answers"]["text"][0]
return example
# get the answer span
dataset = dataset.map(_process)
# remove unanswerable questions in IndicQA
dataset = dataset.filter(lambda example: len(example["answers"]) > 0) # e.g., hi 1.5k -> 1k
return dataset
def process_mafand_e2t(dataset):
splits = list(dataset.keys())
target_lang = [l for l in list(dataset[splits[0]][0]['translation'].keys()) if l != 'en'][0]
def _add_column(d):
num_rows = len(d)
d = d.add_column(name="source_lang", column=['English'] * num_rows)
d = d.add_column(name="source_text", column=[a['en'] for a in d['translation']])
d = d.add_column(name="target_lang", column=[MAFAND_LANGUAGE_NAMES[target_lang]] * num_rows)
d = d.add_column(name="target_text", column=[a[target_lang] for a in d['translation']])
return d
for split in splits:
dataset[split] = _add_column(dataset[split])
return dataset
def process_mafand_t2e(dataset):
splits = list(dataset.keys())
source_lang = [l for l in list(dataset[splits[0]][0]['translation'].keys()) if l != 'en'][0]
def _add_column(d):
num_rows = len(d)
d = d.add_column(name="target_lang", column=['English'] * num_rows)
d = d.add_column(name="target_text", column=[a['en'] for a in d['translation']])
d = d.add_column(name="source_lang", column=[MAFAND_LANGUAGE_NAMES[source_lang]] * num_rows)
d = d.add_column(name="source_text", column=[a[source_lang] for a in d['translation']])
return d
for split in splits:
dataset[split] = _add_column(dataset[split])
return dataset
def load_multilingual_data(task_name, hf_dataset, languages, cache_dir, process_func):
d = {}
for lang in languages:
if "mafand" in task_name:
d[lang] = load_dataset(hf_dataset, f"en-{lang}", cache_dir=cache_dir)
else:
d[lang] = load_dataset(hf_dataset, lang, cache_dir=cache_dir)
if process_func is not None:
for lang in languages:
d[lang] = eval(process_func)(d[lang])
return d
def add_template_name(path, template_name):
result_files = glob.glob(os.path.join(path, '**/results.jsonl'), recursive=True)
for file in result_files:
with open(file, 'r') as fp:
print(file)
d = json.load(fp)
d['config']['template'] = template_name
with open(file, 'w') as fp:
json.dump(d, fp, indent=2)
if __name__ == "__main__":
path = sys.argv[1]
template_name = sys.argv[2]
add_template_name(path, template_name)