Create querie.py
Browse files
querie.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import json
|
| 3 |
+
import lzma
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
import datasets
|
| 7 |
+
try:
|
| 8 |
+
import lzma as xz
|
| 9 |
+
except ImportError:
|
| 10 |
+
import pylzma as xz
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# TODO: Add BibTeX citation
|
| 14 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
| 15 |
+
_CITATION = """\
|
| 16 |
+
@InProceedings{huggingface:dataset,
|
| 17 |
+
title = {A great new dataset},
|
| 18 |
+
author={huggingface, Inc.
|
| 19 |
+
},
|
| 20 |
+
year={2020}
|
| 21 |
+
}
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
# You can copy an official description
|
| 25 |
+
_DESCRIPTION = """TODO"""
|
| 26 |
+
|
| 27 |
+
# TODO: Add a link to an official homepage for the dataset here
|
| 28 |
+
_HOMEPAGE = ""
|
| 29 |
+
|
| 30 |
+
# TODO: Add the licence for the dataset here if you can find it
|
| 31 |
+
_LICENSE = ""
|
| 32 |
+
|
| 33 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
| 34 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
| 35 |
+
_URLS = {
|
| 36 |
+
"german": "https://huggingface.co/datasets/Stern5497/querie/resolve/main/queries_de",
|
| 37 |
+
"italian": "https://huggingface.co/datasets/Stern5497/querie/resolve/main/queries_it",
|
| 38 |
+
"french": "https://huggingface.co/datasets/Stern5497/querie/resolve/main/queries_fr",
|
| 39 |
+
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
def get_url(config_name):
|
| 43 |
+
return _URLS[config_name]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class querie(datasets.GeneratorBasedBuilder):
|
| 47 |
+
"""This dataset contains court decision for law area prediction task."""
|
| 48 |
+
|
| 49 |
+
VERSION = datasets.Version("1.1.0")
|
| 50 |
+
|
| 51 |
+
BUILDER_CONFIGS = [
|
| 52 |
+
datasets.BuilderConfig(name="german", version=VERSION, description="This part of my dataset covers the whole dataset"),
|
| 53 |
+
datasets.BuilderConfig(name="italian", version=VERSION, description="This dataset is for predicting the sub law areas of the public law"),
|
| 54 |
+
datasets.BuilderConfig(name="french", version=VERSION, description="This dataset is for predicting the sub law areas of the civil law"),
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
DEFAULT_CONFIG_NAME = "german" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 58 |
+
|
| 59 |
+
def _info(self):
|
| 60 |
+
features = datasets.Features(
|
| 61 |
+
{
|
| 62 |
+
"id": datasets.Value("string"),
|
| 63 |
+
"text": datasets.Value("string"),
|
| 64 |
+
# These are the features of your dataset like images, labels ...
|
| 65 |
+
}
|
| 66 |
+
)
|
| 67 |
+
return datasets.DatasetInfo(
|
| 68 |
+
# This is the description that will appear on the datasets page.
|
| 69 |
+
description=_DESCRIPTION,
|
| 70 |
+
# This defines the different columns of the dataset and their types
|
| 71 |
+
features=features, # Here we define them above because they are different between the two configurations
|
| 72 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
| 73 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
| 74 |
+
# supervised_keys=("sentence", "label"),
|
| 75 |
+
# Homepage of the dataset for documentation
|
| 76 |
+
# homepage=_HOMEPAGE,
|
| 77 |
+
# License for the dataset if available
|
| 78 |
+
# license=_LICENSE,
|
| 79 |
+
# Citation for the dataset
|
| 80 |
+
# citation=_CITATION,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
def _split_generators(self, dl_manager):
|
| 84 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 85 |
+
|
| 86 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 87 |
+
# 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.
|
| 88 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 89 |
+
urls = get_url(self.config.name)
|
| 90 |
+
filepath = dl_manager.download(os.path.join(urls, ".jsonl"))
|
| 91 |
+
|
| 92 |
+
return [
|
| 93 |
+
datasets.SplitGenerator(
|
| 94 |
+
name=datasets.Split.TRAIN,
|
| 95 |
+
# These kwargs will be passed to _generate_examples
|
| 96 |
+
gen_kwargs={
|
| 97 |
+
"filepath": filepath,
|
| 98 |
+
"split": "train",
|
| 99 |
+
},
|
| 100 |
+
)
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
| 104 |
+
def _generate_examples(self, filepath, split):
|
| 105 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 106 |
+
line_counter = 0
|
| 107 |
+
try:
|
| 108 |
+
with xz.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
|
| 109 |
+
for id, line in enumerate(f):
|
| 110 |
+
line_counter += 1
|
| 111 |
+
if line:
|
| 112 |
+
data = json.loads(line)
|
| 113 |
+
if self.config.name == "german" or self.config.name == "italian" or self.config.name == "french":
|
| 114 |
+
yield id, {
|
| 115 |
+
"id": data["id"],
|
| 116 |
+
"text": data["text"],
|
| 117 |
+
}
|
| 118 |
+
except lzma.LZMAError as e:
|
| 119 |
+
print(split, e)
|
| 120 |
+
if line_counter == 0:
|
| 121 |
+
raise e
|