File size: 7,480 Bytes
8a9f0f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b245d5
 
 
 
8a9f0f8
 
 
 
 
 
 
 
 
 
 
 
 
fc0e263
 
 
8a9f0f8
fc0e263
 
8e7c738
 
fc0e263
 
 
8e7c738
fe43834
1be4e98
8a9f0f8
 
 
 
 
e965e8b
8a9f0f8
 
 
 
70c12b2
a855f45
8a9f0f8
 
 
 
 
 
 
 
 
 
70c12b2
a855f45
8a9f0f8
70c12b2
8a9f0f8
 
 
 
 
015d35f
8a9f0f8
 
 
 
015d35f
1be4e98
8a9f0f8
 
 
 
 
9b9edf8
8a9f0f8
e67e171
 
 
 
 
 
 
 
 
0773459
 
 
 
 
 
 
 
 
8a9f0f8
9b9edf8
8a9f0f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc0e263
 
 
 
 
 
6ecbc21
8a9f0f8
6ecbc21
8a9f0f8
 
47c7204
6ecbc21
47c7204
0773459
47c7204
 
8a9f0f8
 
 
825b04e
8a9f0f8
0773459
c82f912
8a9f0f8
fc0e263
8a9f0f8
fc0e263
 
 
8a9f0f8
fc0e263
 
e67e171
fc0e263
 
e67e171
8a9f0f8
d51dd4b
36c4567
e67e171
 
 
 
 
8a9f0f8
 
e67e171
8a9f0f8
fc0e263
 
 
 
 
 
 
 
 
9b9edf8
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import csv
import json
import os

import datasets
from collections import defaultdict


_CITATION = ""

languages = {'yoruba':'yo', 
             'hausa':'ha',
             'swahili':'sw',
             'somali':'so'}

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
This dataset consists of the queries and relevance judgements in the CIRAL test collection. 
"""

_HOMEPAGE = ""

_LICENSE = ""

_URLS = {
    lang: {
        'dev': [
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/topics/topics.ciral-v1.0-{lang_code}-dev.tsv',
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/qrels/qrels.ciral-v1.0-{lang_code}-dev.tsv'
        ],
        'testA':[
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/topics/topics.ciral-v1.0-{lang_code}-test-a.tsv',
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/qrels/qrels.ciral-v1.0-{lang_code}-test-a.tsv',
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/qrels/qrels.ciral-v1.0-{lang_code}-test-a-pools.tsv',
        ],
        'testB':[
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/topics/topics.ciral-v1.0-{lang_code}-test-b.tsv',
            f'https://huggingface.co/datasets/CIRAL/ciral/resolve/main/ciral-{lang}/qrels/qrels.ciral-v1.0-{lang_code}-test-b.tsv',
        ]
    } for lang, lang_code in languages.items()
}


def load_queries(_file):
    if _file is None:
        return []

    queries = {}
    with open(_file, encoding="utf-8") as query_file:
        for line in query_file:
            line = line.strip()
            id, query = (line.split('\t')) if len(line.split('\t')) == 2 else ("", "")
            queries[id] = query
    return queries

def load_qrels(_file):
    if _file is None:
        return None

    qrels = defaultdict(dict)
    with open(_file, encoding="utf-8") as qrel_file:
        for line in qrel_file:
            line = line.strip()
            qid, _, docid, rel = (line.split('\t')) if len(line.split('\t')) == 4 else ("", "", "",False)
            qrels[qid][docid] = int(rel)
    #print(qrels)
    return qrels



class CIRAL(datasets.GeneratorBasedBuilder):
    #VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=lang,
            version=datasets.Version("1.1.0"), 
            description=f"CIRAL data for {lang}.")  for lang in languages.keys()
    ]

    def _info(self):
        features = datasets.Features(
            {
                "query_id": datasets.Value("string"),
                "query": datasets.Value("string"),
                # "judgements": [{
                #     "docid": datasets.Value("string"),
                #     "judgement": datasets.Value("string"),
                #     "text": datasets.Value("string")}]
                "positive_passages": [{
                    'docid': datasets.Value("string"),
                    'text': datasets.Value("string"),
                }],
                "negative_passages": [{
                    "docid": datasets.Value("string"),
                    "text": datasets.Value("string")
                }],
        
                "pools_positive_passages": [{
                    'docid': datasets.Value("string"),
                    'text': datasets.Value("string"),
                }],
                "pools_negative_passages": [{
                    "docid": datasets.Value("string"),
                    "text": datasets.Value("string")}]
            }
        )

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        
        lang = self.config.name
        downloaded_files = dl_manager.download_and_extract(_URLS[lang])
        return [
            datasets.SplitGenerator(
                name='dev',
                gen_kwargs={
                    'filepaths': downloaded_files['dev'],
                },
            ),
            datasets.SplitGenerator(
                name='testA',
                gen_kwargs={
                    'filepaths': downloaded_files['testA'],
                },
            ),
            datasets.SplitGenerator(
                name='testB',
                gen_kwargs={
                    'filepaths': downloaded_files['testB'],
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepaths):
        lang = self.config.name
        corpus = datasets.load_dataset('ciral/ciral-corpus', lang)['train']
        docid2doc = {doc['docid']: doc['text'] for doc in corpus}

        query_file, qrel_file, pools_file = (filepaths) if len(filepaths) == 3 else (filepaths[0], filepaths[1], None)
        queries = load_queries(query_file)
        shallow_qrels = load_qrels(qrel_file)
        pools_qrels = load_qrels(pools_file)
        
        for query_id in queries:
            positive_docids = [docid for docid, judgement in shallow_qrels[query_id].items() if judgement==1]
            negative_docids = [docid for docid, judgement in shallow_qrels[query_id].items() if judgement==0]

            pools_positive_docids = [docid for docid, judgement in pools_qrels[query_id].items() if judgement==1] if pools_qrels is not None else []
            pools_negative_docids = [docid for docid, judgement in pools_qrels[query_id].items() if judgement==0] if pools_qrels is not None else []

            data = {}
            data['query_id'] = query_id
            data['query'] = queries[query_id]
            data['positive_passages'] = [{
                'docid': docid,
                'text': docid2doc[docid]
            } for docid in positive_docids if docid in docid2doc]
            data['negative_passages'] = [{
                'docid': docid,
                'text': docid2doc[docid]
            } for docid in negative_docids if docid in docid2doc]

            data['pools_positive_passages'] = [{
                'docid': docid,
                'text': docid2doc[docid]
            } for docid in pools_positive_docids if docid in docid2doc]
            data['pools_negative_passages'] = [{
                'docid': docid,
                'text': docid2doc[docid]
            } for docid in pools_negative_docids if docid in docid2doc]

            yield query_id, data