Update xor-tydi.py
Browse files- xor-tydi.py +29 -10
xor-tydi.py
CHANGED
|
@@ -121,14 +121,31 @@ class XORTyDi(datasets.GeneratorBasedBuilder):
|
|
| 121 |
positive_ctxs = data["positive_ctxs"]
|
| 122 |
hard_negative_ctxs = data["hard_negative_ctxs"]
|
| 123 |
# each ctx: {'title':... , 'text': ....}
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
_id = f"{_id}-{random.randint(*RANGE)}"
|
|
|
|
| 126 |
return _id, {
|
| 127 |
"query_id": _id,
|
| 128 |
"query": data["question"],
|
| 129 |
"answers": data.get("answers", []),
|
| 130 |
-
"positive_passages":
|
| 131 |
-
"negative_passages":
|
| 132 |
}
|
| 133 |
|
| 134 |
def process_dev_test_entry(data):
|
|
@@ -142,16 +159,18 @@ class XORTyDi(datasets.GeneratorBasedBuilder):
|
|
| 142 |
|
| 143 |
assert len(files) == 1
|
| 144 |
filepath = files[0]
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
with open(filepath, encoding="utf-8") as f:
|
| 147 |
for line in f:
|
| 148 |
data = json.loads(line)
|
| 149 |
|
| 150 |
if "id" in data and "query_id" not in data:
|
| 151 |
yield process_dev_test_entry(data)
|
| 152 |
-
|
| 153 |
-
with open(filepath, encoding="utf-8") as f:
|
| 154 |
-
all_data = json.load(f)
|
| 155 |
-
for i, data in enumerate(all_data):
|
| 156 |
-
yield process_train_entry(data, i)
|
| 157 |
-
|
|
|
|
| 121 |
positive_ctxs = data["positive_ctxs"]
|
| 122 |
hard_negative_ctxs = data["hard_negative_ctxs"]
|
| 123 |
# each ctx: {'title':... , 'text': ....}
|
| 124 |
+
|
| 125 |
+
def process_ctx(ctxs, tag):
|
| 126 |
+
# assert len(doc["text"]) == 1
|
| 127 |
+
for doc in ctxs:
|
| 128 |
+
if isinstance(doc["text"], list):
|
| 129 |
+
assert len(doc["text"]) == 1
|
| 130 |
+
else:
|
| 131 |
+
assert isinstance(doc["text"], str)
|
| 132 |
+
|
| 133 |
+
return [
|
| 134 |
+
{
|
| 135 |
+
"title": doc["title"],
|
| 136 |
+
"text": doc["text"][0] if isinstance(doc["text"], list) else doc["text"],
|
| 137 |
+
'docid': f'{tag}-{i}-{random.randint(*RANGE)}'
|
| 138 |
+
} for i, doc in enumerate(ctxs)
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
_id = f"{_id}-{random.randint(*RANGE)}"
|
| 142 |
+
# import pdb; pdb.set_trace()
|
| 143 |
return _id, {
|
| 144 |
"query_id": _id,
|
| 145 |
"query": data["question"],
|
| 146 |
"answers": data.get("answers", []),
|
| 147 |
+
"positive_passages": process_ctx(positive_ctxs, "pos"),
|
| 148 |
+
"negative_passages": process_ctx(hard_negative_ctxs, "neg"),
|
| 149 |
}
|
| 150 |
|
| 151 |
def process_dev_test_entry(data):
|
|
|
|
| 159 |
|
| 160 |
assert len(files) == 1
|
| 161 |
filepath = files[0]
|
| 162 |
+
try:
|
| 163 |
+
with open(filepath, encoding="utf-8") as f:
|
| 164 |
+
all_data = json.load(f)
|
| 165 |
+
for i, data in enumerate(all_data):
|
| 166 |
+
yield process_train_entry(data, i)
|
| 167 |
+
|
| 168 |
+
# if filepath.endswith(".jsonl"): <-- doesn't work
|
| 169 |
+
except Exception as e:
|
| 170 |
with open(filepath, encoding="utf-8") as f:
|
| 171 |
for line in f:
|
| 172 |
data = json.loads(line)
|
| 173 |
|
| 174 |
if "id" in data and "query_id" not in data:
|
| 175 |
yield process_dev_test_entry(data)
|
| 176 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|