File size: 3,974 Bytes
8d2e0ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""EurlexResources"""

import json

import datasets

try:
    import lzma as xz
except ImportError:
    import pylzma as xz

datasets.logging.set_verbosity_info()
logger = datasets.logging.get_logger(__name__)

_DESCRIPTION = """
"""

_CITATION = """
"""

_URL = "https://huggingface.co/datasets/joelito/eurlex_resources"
_DATA_URL = f"{_URL}/resolve/main/data"

_LANGUAGES = [
    "bg",
    "cs",
    "da",
    "de",
    "el",
    "en",
    "es",
    "et",
    "fi",
    "fr",
    "ga",
    "hr",
    "hu",
    "it",
    "lt",
    "lv",
    "mt",
    "nl",
    "pl",
    "pt",
    "ro",
    "sk",
    "sl",
    "sv",
]

_RESOURCE_TYPES = ["caselaw", "decision", "directive", "intagr", "proposal", "recommendation", "regulation"]


class EurlexResourcesConfig(datasets.BuilderConfig):
    """BuilderConfig for EurlexResources."""

    def __init__(self, name: str, **kwargs):
        """BuilderConfig for EurlexResources.
        Args:
            name: combination of language and resource_type with _
            language: One of bg,cs,da,de,el,en,es,et,fi,fr,ga,hr,hu,it,lt,lv,mt,nl,pl,pt,ro,sk,sl,sv or all
            resource_type: One of caselaw, decision, directive, intagr, proposal, recommendation, regulation
          **kwargs: keyword arguments forwarded to super.
        """
        super(EurlexResourcesConfig, self).__init__(**kwargs)
        self.name = name
        self.language = name.split("_")[0]
        self.resource_type = name.split("_")[1]


class EurlexResources(datasets.GeneratorBasedBuilder):
    """EurlexResources: A Corpus Covering the Largest EURLEX Resources"""

    BUILDER_CONFIGS = [EurlexResourcesConfig(f"{language}_{resource_type}")
                       for resource_type in _RESOURCE_TYPES + ["all"]
                       for language in _LANGUAGES + ["all"]]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "celex": datasets.Value("string"),
                    "date": datasets.Value("string"),
                    "title": datasets.Value("string"),
                    "text": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_URL,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_urls = []
        languages = _LANGUAGES if self.config.language == "all" else [self.config.language]
        resource_types = _RESOURCE_TYPES if self.config.resource_type == "all" else [self.config.resource_type]
        for language in languages:
            for resource_type in resource_types:
                data_urls.append(f"{_DATA_URL}/{language}/{resource_type}.jsonl.xz")

        downloaded_files = dl_manager.download(data_urls)
        return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": downloaded_files})]

    def _generate_examples(self, filepaths):
        """This function returns the examples in the raw (text) form by iterating on all the files."""
        id_ = 0
        for filepath in filepaths:
            logger.info("Generating examples from = %s", filepath)
            try:
                with xz.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
                    for line in f:
                        if line:
                            example = json.loads(line)
                            if example is not None and isinstance(example, dict):
                                yield id_, {
                                    "celex": example.get("celex", ""),
                                    "date": example.get("date", ""),
                                    "title": example.get("title", ""),
                                    "text": example.get("text", ""),
                                }
                                id_ += 1
            except:
                print("Error reading file:", filepath)