Stern5497 commited on
Commit
d5ebf35
1 Parent(s): 9b8609d

Create swiss_legislation.py

Browse files
Files changed (1) hide show
  1. swiss_legislation.py +115 -0
swiss_legislation.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Dataset for the doc2doc information retrieval task."""
15
+
16
+ import json
17
+ import lzma
18
+ import os
19
+
20
+ import datasets
21
+ try:
22
+ import lzma as xz
23
+ except ImportError:
24
+ import pylzma as xz
25
+
26
+
27
+ # TODO: Add BibTeX citation
28
+ # Find for instance the citation on arxiv or on the dataset repo/website
29
+ _CITATION = """\
30
+ @InProceedings{huggingface:dataset,
31
+ title = {A great new dataset},
32
+ author={huggingface, Inc.
33
+ },
34
+ year={2020}
35
+ }
36
+ """
37
+
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ This dataset contains Swiss law articles
41
+ """
42
+
43
+ _URLS = {
44
+ "all": "https://huggingface.co/datasets/rcds/doc2doc/resolve/main/lexfind_v2.jsonl.xz",
45
+ }
46
+
47
+
48
+ class SwissLegilation(datasets.GeneratorBasedBuilder):
49
+ """This dataset contains court decision for doc2doc information retrieval task."""
50
+
51
+
52
+ BUILDER_CONFIGS = [
53
+ datasets.BuilderConfig(name="full", description="This part covers the whole dataset"),
54
+ ]
55
+
56
+ DEFAULT_CONFIG_NAME = "full" # It's not mandatory to have a default configuration. Just use one if it make sense.
57
+
58
+ def _info(self):
59
+ if self.config.name == "full" or self.config.name == "origin": # This is the name of the configuration selected in BUILDER_CONFIGS above
60
+ features = datasets.Features(
61
+ {
62
+ "canton": datasets.Value("string"),
63
+ "language": datasets.Value("string"),
64
+ "uuid": datasets.Value("string"),
65
+ "title": datasets.Value("string"),
66
+ "short": datasets.Value("string"),
67
+ "abbreviation": datasets.Value("string"),
68
+ "sr_number": datasets.Value("string"),
69
+ "pdf_content": datasets.Value("string")
70
+ }
71
+ )
72
+ return datasets.DatasetInfo(
73
+ description=_DESCRIPTION,
74
+ features=features, # Here we define them above because they are different between the two configurations
75
+ )
76
+
77
+ def _split_generators(self, dl_manager):
78
+ urls = _URLS[self.config.name]
79
+ filepath = dl_manager.download(os.path.join(urls, " lexfind_v2.jsonl.xz"))
80
+ return [
81
+ datasets.SplitGenerator(
82
+ name=datasets.Split.TRAIN,
83
+ # These kwargs will be passed to _generate_examples
84
+ gen_kwargs={
85
+ "filepath": filepath_train,
86
+ "split": "train",
87
+ },
88
+ )
89
+ ]
90
+
91
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
92
+ def _generate_examples(self, filepath, split):
93
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
94
+ line_counter = 0
95
+ try:
96
+ with xz.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
97
+ for id, line in enumerate(f):
98
+ line_counter += 1
99
+ if line:
100
+ data = json.loads(line)
101
+ if self.config.name == "full":
102
+ yield id, {
103
+ "canton": data["canton"],
104
+ "language": data["language"],
105
+ "uuid": data["uuid"],
106
+ "short": data["short"],
107
+ "title": data["title"],
108
+ "abbreviation": data["abbreviation"],
109
+ "sr_number": data["sr_number"],
110
+ "pdf_content": data["pdf_content"],
111
+ }
112
+ except lzma.LZMAError as e:
113
+ print(split, e)
114
+ if line_counter == 0:
115
+ raise e