add loading script
Browse files- bernice-pretrain-data.py +121 -0
bernice-pretrain-data.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
15 |
+
"""Bernice pretrain data"""
|
16 |
+
|
17 |
+
|
18 |
+
import csv
|
19 |
+
import json
|
20 |
+
import os
|
21 |
+
import gzip
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
# TODO: Add BibTeX citation
|
26 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
27 |
+
_CITATION = """\
|
28 |
+
Alexandra DeLucia, Shijie Wu, Aaron Mueller, Carlos Aguirre, Philip Resnik, and Mark Dredze. 2022.
|
29 |
+
Bernice: A Multilingual Pre-trained Encoder for Twitter. In Proceedings of the 2022 Conference on
|
30 |
+
Empirical Methods in Natural Language Processing, pages 6191–6205, Abu Dhabi, United Arab Emirates.
|
31 |
+
Association for Computational Linguistics.
|
32 |
+
"""
|
33 |
+
|
34 |
+
# You can copy an official description
|
35 |
+
_DESCRIPTION = """\
|
36 |
+
Tweet IDs for the 2.5 billion multilingual tweets used to train Bernice, a Twitter encoder.
|
37 |
+
The tweets are from the public 1% Twitter API stream from January 2016 to December 2021.
|
38 |
+
Twitter-provided language metadata is provided with the tweet ID. The data contains 66 unique languages,
|
39 |
+
as identified by ISO 639 language codes, including `und` for undefined languages.
|
40 |
+
Tweets need to be re-gathered via the Twitter API.
|
41 |
+
"""
|
42 |
+
|
43 |
+
_HOMEPAGE = "https://preview.aclanthology.org/emnlp-22-ingestion/2022.emnlp-main.415"
|
44 |
+
|
45 |
+
# TODO: Add the licence for the dataset here if you can find it
|
46 |
+
_LICENSE = ""
|
47 |
+
|
48 |
+
# TODO: Add link to the official dataset URLs here
|
49 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
50 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
51 |
+
# If the data files live in the same folder or repository of the dataset script,
|
52 |
+
# you can just pass the relative paths to the files instead of URLs.
|
53 |
+
# Only train data, validation split not provided
|
54 |
+
_URLS = {
|
55 |
+
"train": "data"
|
56 |
+
}
|
57 |
+
|
58 |
+
|
59 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
60 |
+
class BernicePretrainData(datasets.GeneratorBasedBuilder):
|
61 |
+
"""Tweet IDs for the 2.5 billion multilingual tweets used to train Bernice, a Twitter encoder."""
|
62 |
+
VERSION = datasets.Version("1.1.0")
|
63 |
+
|
64 |
+
def _info(self):
|
65 |
+
return datasets.DatasetInfo(
|
66 |
+
# This is the description that will appear on the datasets page.
|
67 |
+
description=_DESCRIPTION,
|
68 |
+
# This defines the different columns of the dataset and their types
|
69 |
+
# Here we define them above because they are different between the two configurations
|
70 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
71 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
72 |
+
# supervised_keys=("sentence", "label"),
|
73 |
+
# Homepage of the dataset for documentation
|
74 |
+
features=datasets.Features(
|
75 |
+
{
|
76 |
+
"tweet_id": datasets.Value("string"),
|
77 |
+
"lang": datasets.Value("string"),
|
78 |
+
"year": datasets.Value("string")
|
79 |
+
}
|
80 |
+
),
|
81 |
+
homepage=_HOMEPAGE,
|
82 |
+
# License for the dataset if available
|
83 |
+
license=_LICENSE,
|
84 |
+
# Citation for the dataset
|
85 |
+
citation=_CITATION,
|
86 |
+
)
|
87 |
+
|
88 |
+
def _split_generators(self, dl_manager):
|
89 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
90 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
91 |
+
|
92 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
93 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
94 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
95 |
+
dir_url = self._URLS["train"]
|
96 |
+
urls_to_download = [f"{dir_url}/{f}" for f in os.listdir(dir_url)]
|
97 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
98 |
+
return [
|
99 |
+
datasets.SplitGenerator(
|
100 |
+
name=datasets.Split.TRAIN,
|
101 |
+
# These kwargs will be passed to _generate_examples
|
102 |
+
gen_kwargs={
|
103 |
+
"filepaths": downloaded_files,
|
104 |
+
"split": "train",
|
105 |
+
},
|
106 |
+
)
|
107 |
+
]
|
108 |
+
|
109 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
110 |
+
def _generate_examples(self, filepaths, split):
|
111 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
112 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
113 |
+
for filepath in filepaths:
|
114 |
+
with gzip.open(filepath, "rb") as f:
|
115 |
+
for line_number, instance in enumerate(f):
|
116 |
+
tweet_id, lang, year = instance.strip().split("\t")
|
117 |
+
yield tweet_id, {
|
118 |
+
"tweet_id": tweet_id,
|
119 |
+
"lang": lang,
|
120 |
+
"year": year
|
121 |
+
}
|