File size: 6,743 Bytes
2a822d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fcdce5
2a822d8
 
 
 
 
 
3404ff9
 
 
 
 
 
 
 
 
 
2a822d8
5761450
2a822d8
72259ac
2a822d8
 
 
5761450
2a822d8
 
 
 
 
 
5761450
2a822d8
5761450
2a822d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5761450
 
 
 
2a822d8
 
 
 
 
 
 
7c489e1
 
2a822d8
 
 
 
c21dc67
2a822d8
 
 
 
 
76b7219
 
2a822d8
 
 
 
7c489e1
2a822d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc8402c
 
2a822d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c489e1
 
2a822d8
 
 
 
 
 
 
 
 
 
 
76b7219
 
2a822d8
 
 
 
5761450
2a822d8
5761450
2a822d8
 
 
 
 
 
 
 
5761450
2a822d8
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
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# 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.

# Lint as: python3
# ok
import os
import json

import datasets


_DESCRIPTION = """MQA is a multilingual corpus of questions and answers parsed from the Common Crawl. Questions are divided between Frequently Asked Questions (FAQ) pages and Community Question Answering (CQA) pages."""
_HOMEPAGE_URL = "https://huggingface.co/datasets/clips/mqa"
_CITATION = """
@misc{debruyn2021mfaq,
      title={MFAQ: a Multilingual FAQ Dataset}, 
      author={Maxime {De Bruyn} and Ehsan Lotfi and Jeska Buhmann and Walter Daelemans},
      year={2021},
      booktitle={MRQA@EMNLP2021},
}
"""

_VERSION = "0.1"
_BASE_NAME = ""
_BASE_URL = "data/data.{}.{}.json.gz"


_LANGUAGES = [
    "ca", "en", "de", "es", "fr",
    "ru", "ja", "it", "zh", "pt",
    "nl", "tr", "pl", "vi", "ar",
    "id", "uk", "ro", "no", "th",
    "sv", "el", "fi", "he", "da",
    "cs", "ko", "fa", "hi", "hu",
    "sk", "lt", "et", "hr", "is",
    "lv", "ms", "bg", "sr", 
]
_SCOPES = ["faq", "cqa"]
_LEVELS = ["domain", "page", "question"]


class MQAConfig(datasets.BuilderConfig):
    def __init__(self, *args, language="en", scope="all", level="question", **kwargs):
        super().__init__(
            *args,
            name=f"{language}-{scope}-{level}",
            **kwargs,
        )
        self.language = language
        self.scope = scope
        self.level = level


class MQA(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = []
    for language in _LANGUAGES:
        for scope in _SCOPES:
            for level in _LEVELS:
                BUILDER_CONFIGS.append(MQAConfig(language=language, scope=scope, level=level))
    for language in _LANGUAGES:
        BUILDER_CONFIGS.append(MQAConfig(language=language, scope="all", level=level))
    for scope in _SCOPES:
        BUILDER_CONFIGS.append(MQAConfig(language="all", scope=scope, level=level))
    BUILDER_CONFIG_CLASS = MQAConfig

    def _info(self):
        question = {
            "id": datasets.Value("string"),
            "text": datasets.Value("string"),
            "name": datasets.Value("string"),
            "domain": datasets.Value("string"),
            "bucket": datasets.Value("string"),
            "answers": [{
                "text": datasets.Value("string"),
                "name": datasets.Value("string"),
                "is_accepted": datasets.Value("bool"),
            }]                      
        }
        page = {
            "id": datasets.Value("string"),
            "bucket": datasets.Value("string"),
            "domain": datasets.Value("string"),
            # "description": datasets.Value("string"),
            # "title": datasets.Value("string"),
            "questions": [question]                        
        }
        domain = {
            "domain": datasets.Value("string"),
            "pages": [page]
        }
        if self.config.level == "question":
            features = question
        elif self.config.level == "page":
            features = page
        elif self.config.level == "domain":
            features = domain
        else:
            raise NotImplementedError()
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(features),
            supervised_keys=None,
            homepage=_HOMEPAGE_URL,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        filenames = []
        languages = _LANGUAGES if self.config.language == "all" else [self.config.language]
        scopes = _SCOPES if self.config.scope == "all" else [self.config.scope]
        for language in languages:
            for scope in scopes:
                path = dl_manager.download_and_extract(_BASE_URL.format(language, scope))
                filenames.append(path)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filenames": filenames},
            )
        ]

    def _generate_examples(self, filenames):

        def default(e, key, default_value=""):
            if e[key] is None:
                return default_value
            return e[key]

        for filename in filenames:
            with open(filename, "r") as f:
                domain = []
                previous_domain = ''
                for line in f:
                    page = json.loads(line)
                    questions = [{
                        "text": default(question, "text"),
                        "name": default(question, "name"),
                        "domain": page["domain"],
                        "bucket": page["bucket"],
                        "id": question["hash"],
                        "answers": [{
                            "text": default(answer, "text"),
                            "name": default(answer, "name"),
                            "is_accepted": answer["is_accepted"]
                        } for answer in question["answers"]]
                    } for question in page["questions"]]
                    page = {
                        "id": page["page_hash"],
                        "domain": page["domain"],
                        "bucket": page["bucket"],
                        # "title": default(page, "title"),
                        # "description": default(page, "description"),
                        "questions": questions
                    }
                    if self.config.level == "question":
                        for question in questions:
                            yield question["id"], question
                    if self.config.level == "page":
                        yield page["id"], page
                    if self.config.level == "domain":
                        if page["domain"] == previous_domain or previous_domain == "":
                            domain.append(page)
                        else:
                            yield previous_domain, {
                                "domain": previous_domain,
                                "pages": domain
                            }
                            domain = []
                        previous_domain = page["domain"]