This repository was archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmarco_dataset.py
executable file
·56 lines (44 loc) · 1.73 KB
/
marco_dataset.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
from typing import List, Union, Optional
from haystack.schema import Document
from haystack.nodes.other import Dataset
import time, ray
import pandas as pd
import modin.pandas as modin_pd
import os
import json
os.environ["__MODIN_AUTOIMPORT_PANDAS__"] = "1"
def _generate_documents(batch: pd.DataFrame) -> List[Document]:
documents = []
for _, data in batch.iterrows():
if isinstance(data['answers'], list) == False:
continue
data['answers'] = data['answers'][0]
if len(str(data['wellFormedAnswers'])) > 2:
if isinstance(data['wellFormedAnswers'], list) :
data['answers'] = data['wellFormedAnswers'][0]
elif "No Answer Present." in data['answers']:
data['answers'] = data['passages']
if len(str(data['answers'])) == 0:
print("no answers, drop the document!")
continue
doc = {'content': str(data['query']), 'meta': {'answer': str(data['answers']), 'question_id': str(data['query_id']), 'question_type': str(data['query_type'])}}
documents.append(Document.from_dict(doc))
return documents
class MarcoDataset(Dataset):
"""
This Node is used to convert MS Marco dataset into ray.data.Dataset of Haystack Document format.
"""
outgoing_edges = 1
def __init__(self,
file: str,
batch_size: Optional[int] = 4096,
) :
super().__init__(batch_size=batch_size)
self.file = file
def convert(self) -> ray.data.Dataset:
dataset = modin_pd.read_json(self.file)
dataset = ray.data.from_modin(dataset)
start = time.time()
dataset = dataset.map_batches(_generate_documents)
cost = time.time() - start
return dataset