PM-AI commited on
Commit
47bdded
1 Parent(s): 0412543

Update germandpr-beir.py

Browse files
Files changed (1) hide show
  1. germandpr-beir.py +36 -11
germandpr-beir.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import datasets
2
 
3
 
@@ -80,25 +82,48 @@ class GermanDPRBeir(datasets.GeneratorBasedBuilder):
80
  name = self.config.name
81
  if name == "qrels":
82
  dl_path = dl_manager.download([
83
- "https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/qrels/test.tsv",
84
- "https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/qrels/train.tsv"
85
  ])
 
 
 
 
86
  else:
87
  dl_path = dl_manager.download(f"https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/{name}.jsonl")
 
 
 
 
88
 
89
- return [
90
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": dl_path, "split": "train"}),
91
- datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": dl_path, "split": "test"})
92
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- def _generate_examples(self, filepath, split):
95
  """Yields examples."""
96
  name = self.config.name
97
  if name.startswith("queries"):
98
- yield 0, {"_id": "1", "text": "text"}
99
  elif name.startswith("corpus"):
100
- yield 0, {"_id": "1", "title": "title", "text": "text"}
101
  else:
102
  # name == qrels
103
- filepath = [x for x in filepath if x.endswith(f"{split}.tsv")]
104
- yield 0, {"query-id": "", "corpus-id": "", "score": 1}
 
1
+ import json
2
+
3
  import datasets
4
 
5
 
 
82
  name = self.config.name
83
  if name == "qrels":
84
  dl_path = dl_manager.download([
85
+ "https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/qrels/train.tsv",
86
+ "https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/qrels/test.tsv"
87
  ])
88
+ return [
89
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": dl_path[0]}),
90
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": dl_path[1]})
91
+ ]
92
  else:
93
  dl_path = dl_manager.download(f"https://huggingface.co/datasets/PM-AI/germandpr-beir/resolve/main/data/{name}.jsonl")
94
+ return [
95
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": dl_path}),
96
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": dl_path})
97
+ ]
98
 
99
+ def _generate_queries_data(self, filepath):
100
+ with open(filepath, "r", encoding="utf-8") as in_file:
101
+ for idx, line in enumerate(in_file):
102
+ data = json.loads(line)
103
+ yield idx, data
104
+
105
+ def _generate_corpus_data(self, filepath):
106
+ with open(filepath, "r", encoding="utf-8") as in_file:
107
+ for idx, line in enumerate(in_file):
108
+ data = json.loads(line)
109
+ if "metadata" in data:
110
+ del data["metadata"]
111
+ yield idx, data
112
+
113
+ def _generate_qrel_data(self, filepath):
114
+ with open(filepath, "r", encoding="utf-8") as in_file:
115
+ in_file.readline() # first line is header
116
+ for idx, line in enumerate(in_file):
117
+ qid, cid, score = line.rstrip().split("\t")
118
+ yield idx, {"query-id": qid, "corpus-id": cid, "score": score}
119
 
120
+ def _generate_examples(self, filepath):
121
  """Yields examples."""
122
  name = self.config.name
123
  if name.startswith("queries"):
124
+ return self._generate_queries_data(filepath)
125
  elif name.startswith("corpus"):
126
+ return self._generate_corpus_data(filepath)
127
  else:
128
  # name == qrels
129
+ return self._generate_qrel_data(filepath)