Mofe commited on
Commit
8a9f0f8
1 Parent(s): 4996988

Added loading script

Browse files
Files changed (1) hide show
  1. ciral.py +148 -0
ciral.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ import csv
17
+ import json
18
+ import os
19
+
20
+ import datasets
21
+ from collections import defaultdict
22
+
23
+
24
+ _CITATION = ""
25
+
26
+ languages = ['hausa', 'yoruba']
27
+
28
+ # TODO: Add description of the dataset here
29
+ # You can copy an official description
30
+ _DESCRIPTION = """\
31
+ This dataset consists of the queries and relevance judgements in the CIRAL test collection.
32
+ """
33
+
34
+ _HOMEPAGE = ""
35
+
36
+ _LICENSE = ""
37
+
38
+ _URLS = {
39
+ lang: {
40
+ 'train': [
41
+ f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/qrels/qrels.ciral-{lang}-train.txt',
42
+ f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/topics/topics.ciral-{lang}-train.txt',
43
+ ],
44
+ 'test':[]
45
+ } for lang in languages
46
+ }
47
+
48
+
49
+ def load_queries(_file):
50
+ if _file is None:
51
+ return None
52
+
53
+ queries = {}
54
+ with open(_file, encoding="utf-8") as query_file:
55
+ for line in query_file:
56
+ id, query = line.strip().split('\t')
57
+ queries[id] = query
58
+ return queries
59
+
60
+ def load_qrels(_file):
61
+ if _file is None:
62
+ return None
63
+
64
+ qrels = defaultdict(dict)
65
+ with open(_file, encoding="utf-8") as qrel_file:
66
+ for line in qrel_file:
67
+ qid, _, docid, rel = line.strip().split('\t')
68
+ qrels[qid][docid] = int(rel)
69
+ return qrels
70
+
71
+
72
+
73
+ class CIRAL(datasets.GeneratorBasedBuilder):
74
+ VERSION = datasets.Version("1.1.0")
75
+
76
+ BUILDER_CONFIGS = [
77
+ datasets.BuilderConfig(
78
+ name=lang,
79
+ version=VERSION,
80
+ description=f"CIRAL data for {lang}.") for lang in languages
81
+ ]
82
+
83
+ def _info(self):
84
+ features = datasets.Features(
85
+ {
86
+ "query id": datasets.Value("string"),
87
+ "query": datasets.Value("string"),
88
+ "positive passages": [{
89
+ "docid": datasets.Value("string"),
90
+ "title": datasets.Value("string"),
91
+ "text": datasets.Value("string")}],
92
+ "negative passages": datasets.features.Sequence({
93
+ "docid": datasets.Value("string"),
94
+ "title": datasets.Value("string"),
95
+ "text": datasets.Value("string")})
96
+ }
97
+ )
98
+
99
+ return datasets.DatasetInfo(
100
+ # This is the description that will appear on the datasets page.
101
+ description=_DESCRIPTION,
102
+ # This defines the different columns of the dataset and their types
103
+ features=features,
104
+ supervised_keys=None,
105
+ homepage=_HOMEPAGE,
106
+ license=_LICENSE,
107
+ citation=_CITATION,
108
+ )
109
+
110
+ def _split_generators(self, dl_manager):
111
+
112
+ lang = self.config.name
113
+ downloaded_files = dl_manager.download_and_extract(_URLS[lang])
114
+ return [
115
+ datasets.SplitGenerator(
116
+ name='train',
117
+ gen_kwargs={
118
+ 'filepaths': downloaded_files['train'],
119
+ },
120
+ ),
121
+ datasets.SplitGenerator(
122
+ name='test',
123
+ gen_kwargs={
124
+ 'filepaths': downloaded_files['test'],
125
+ },
126
+ ),
127
+ ]
128
+
129
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
130
+ def _generate_examples(self, filepaths, split):
131
+ lang = self.config.name
132
+ corpus = datasets.load_dataset('ciral/ciral-corpus', lang)
133
+ docid2doc = {doc['docid']: (doc['title'], doc['text']) for doc in corpus}
134
+
135
+ query_file, qrel_file = (filepaths) if len(filepaths) == 2 else (filepaths[0], None)
136
+ queries = load_queries(query_file)
137
+ qrels = load_qrels(qrel_file)
138
+ for query_id in queries:
139
+ data = {}
140
+ data['Query Id'] = query_id
141
+ data['Query'] = queries[query]
142
+ data['Judgements'] = [{
143
+ 'docid': docid,
144
+ 'Judgement': judgement,
145
+ 'text': docid2doc[docid]
146
+ } for docid, judgement in qrels[query_id].items()]
147
+
148
+ yield query_id, data