Create norwegian-paws-x.py
Browse files- norwegian-paws-x.py +165 -0
norwegian-paws-x.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""PAWS-X, a multilingual version of PAWS for six languages."""
|
16 |
+
|
17 |
+
|
18 |
+
import json
|
19 |
+
|
20 |
+
import datasets
|
21 |
+
|
22 |
+
|
23 |
+
_CITATION = """\
|
24 |
+
@InProceedings{pawsx2019emnlp,
|
25 |
+
title = {{PAWS-X: A Cross-lingual Adversarial Dataset for Paraphrase Identification}},
|
26 |
+
author = {Yang, Yinfei and Zhang, Yuan and Tar, Chris and Baldridge, Jason},
|
27 |
+
booktitle = {Proc. of EMNLP},
|
28 |
+
year = {2019}
|
29 |
+
}
|
30 |
+
"""
|
31 |
+
|
32 |
+
_DESCRIPTION = """\
|
33 |
+
Norwegian PAWS-X, Bokmaal and Nynorsk machine-translated versions of PAWS-X.
|
34 |
+
|
35 |
+
PAWS-X, a multilingual version of PAWS (Paraphrase Adversaries from Word Scrambling) for six languages.
|
36 |
+
|
37 |
+
This dataset contains 23,659 human translated PAWS evaluation pairs and 296,406 machine
|
38 |
+
translated training pairs in six typologically distinct languages: French, Spanish, German,
|
39 |
+
Chinese, Japanese, and Korean. English language is available by default. All translated
|
40 |
+
pairs are sourced from examples in PAWS-Wiki.
|
41 |
+
|
42 |
+
For further details, see the accompanying paper: PAWS-X: A Cross-lingual Adversarial Dataset
|
43 |
+
for Paraphrase Identification (https://arxiv.org/abs/1908.11828)
|
44 |
+
|
45 |
+
NOTE: There might be some missing or wrong labels in the dataset and we have replaced them with -1.
|
46 |
+
"""
|
47 |
+
|
48 |
+
_HOMEPAGE = "https://github.com/google-research-datasets/paws/tree/master/pawsx"
|
49 |
+
|
50 |
+
# TODO: Add the licence for the dataset here if you can find it
|
51 |
+
_LICENSE = 'The dataset may be freely used for any purpose, although acknowledgement of Google LLC ("Google") as the data source would be appreciated. The dataset is provided "AS IS" without any warranty, express or implied. Google disclaims all liability for any damages, direct or indirect, resulting from the use of the dataset.'
|
52 |
+
|
53 |
+
# TODO: Add link to the official dataset URLs here
|
54 |
+
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
55 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
56 |
+
_DATA_OPTIONS = [
|
57 |
+
"nb",
|
58 |
+
"nn",
|
59 |
+
]
|
60 |
+
|
61 |
+
# _DATA_URL = "https://huggingface.co/datasets/NbAiLab/norwegian-paws-x/resolve/main/norwegian-x-final.tar.gz"
|
62 |
+
_DATA_URL = "gcs://nb-datasets/norwegian-paws-x/norwegian-x-final.tar.gz"
|
63 |
+
|
64 |
+
|
65 |
+
class NorwegianPAWSXConfig(datasets.BuilderConfig):
|
66 |
+
"""BuilderConfig for NorwegianPAWSX."""
|
67 |
+
|
68 |
+
def __init__(self, **kwargs):
|
69 |
+
"""Constructs a NorwegianPAWSXConfig.
|
70 |
+
Args:
|
71 |
+
**kwargs: keyword arguments forwarded to super.
|
72 |
+
"""
|
73 |
+
super(NorwegianPAWSXConfig, self).__init__(version=datasets.Version("1.1.0", ""), **kwargs),
|
74 |
+
|
75 |
+
|
76 |
+
class NorwegianPAWSX(datasets.GeneratorBasedBuilder):
|
77 |
+
"""Norwegian PAWS-X, Bokmaal and Nynorsk machine-translated versions of PAWS-X."""
|
78 |
+
|
79 |
+
VERSION = datasets.Version("1.1.0")
|
80 |
+
|
81 |
+
BUILDER_CONFIGS = [
|
82 |
+
NorwegianPAWSXConfig(
|
83 |
+
name=config_name,
|
84 |
+
description=(f"This config contains samples in {config_name}."),
|
85 |
+
)
|
86 |
+
for config_name in _DATA_OPTIONS
|
87 |
+
]
|
88 |
+
|
89 |
+
def _info(self):
|
90 |
+
features = datasets.Features(
|
91 |
+
{
|
92 |
+
"id": datasets.Value("int32"),
|
93 |
+
"sentence1": datasets.Value("string"),
|
94 |
+
"sentence2": datasets.Value("string"),
|
95 |
+
"label": datasets.features.ClassLabel(names=["0", "1"]),
|
96 |
+
}
|
97 |
+
)
|
98 |
+
return datasets.DatasetInfo(
|
99 |
+
# This is the description that will appear on the datasets page.
|
100 |
+
description=_DESCRIPTION,
|
101 |
+
# This defines the different columns of the dataset and their types
|
102 |
+
features=features, # Here we define them above because they are different between the two configurations
|
103 |
+
# If there's a common (input, target) tuple from the features,
|
104 |
+
# specify them here. They'll be used if as_supervised=True in
|
105 |
+
# builder.as_dataset.
|
106 |
+
supervised_keys=None,
|
107 |
+
# Homepage of the dataset for documentation
|
108 |
+
homepage=_HOMEPAGE,
|
109 |
+
# License for the dataset if available
|
110 |
+
license=_LICENSE,
|
111 |
+
# Citation for the dataset
|
112 |
+
citation=_CITATION,
|
113 |
+
)
|
114 |
+
|
115 |
+
def _split_generators(self, dl_manager):
|
116 |
+
"""Returns SplitGenerators."""
|
117 |
+
|
118 |
+
archive = dl_manager.download(_DATA_URL)
|
119 |
+
|
120 |
+
_TEST_FILE_NAME = f"x-final/{self.config.name}/translated_test_2k.json"
|
121 |
+
_VAL_FILE_NAME = f"x-final/{self.config.name}/translated_dev_2k.json"
|
122 |
+
_TRAIN_FILE_NAME = f"x-final/{self.config.name}/translated_train.json"
|
123 |
+
|
124 |
+
return [
|
125 |
+
datasets.SplitGenerator(
|
126 |
+
name=datasets.Split.TRAIN,
|
127 |
+
# These kwargs will be passed to _generate_examples
|
128 |
+
gen_kwargs={
|
129 |
+
"filepath": _TRAIN_FILE_NAME,
|
130 |
+
"files": dl_manager.iter_archive(archive),
|
131 |
+
},
|
132 |
+
),
|
133 |
+
datasets.SplitGenerator(
|
134 |
+
name=datasets.Split.TEST,
|
135 |
+
# These kwargs will be passed to _generate_examples
|
136 |
+
gen_kwargs={
|
137 |
+
"filepath": _TEST_FILE_NAME,
|
138 |
+
"files": dl_manager.iter_archive(archive),
|
139 |
+
},
|
140 |
+
),
|
141 |
+
datasets.SplitGenerator(
|
142 |
+
name=datasets.Split.VALIDATION,
|
143 |
+
# These kwargs will be passed to _generate_examples
|
144 |
+
gen_kwargs={
|
145 |
+
"filepath": _VAL_FILE_NAME,
|
146 |
+
"files": dl_manager.iter_archive(archive),
|
147 |
+
},
|
148 |
+
),
|
149 |
+
]
|
150 |
+
|
151 |
+
def _generate_examples(self, filepath, files):
|
152 |
+
"""Yields examples."""
|
153 |
+
|
154 |
+
for path, f in files:
|
155 |
+
if path == filepath:
|
156 |
+
lines = (line.decode("utf-8") for line in f)
|
157 |
+
for id_, data in enumerate(lines):
|
158 |
+
row = json.loads(data)
|
159 |
+
yield id_, {
|
160 |
+
"id": row["id"],
|
161 |
+
"sentence1": row["sentence1"],
|
162 |
+
"sentence2": row["sentence2"],
|
163 |
+
"label": row["label"],
|
164 |
+
}
|
165 |
+
break
|