Peter Henderson commited on
Commit
43c5ebb
1 Parent(s): 4714437

add dataset loading script

Browse files
Files changed (1) hide show
  1. pile_of_law.py +87 -0
pile_of_law.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """C4 dataset based on Common Crawl."""
2
+
3
+
4
+ import gzip
5
+ import json
6
+
7
+ import datasets
8
+ try:
9
+ import lzma as xz
10
+ except ImportError:
11
+ import pylzma as xz
12
+
13
+
14
+ logger = datasets.logging.get_logger(__name__)
15
+
16
+
17
+ _DESCRIPTION = """\
18
+ A living legal dataset.
19
+ """
20
+
21
+ _CITATION = """
22
+ TODO
23
+ """
24
+
25
+ _URL = ""
26
+
27
+ _VARIANTS = ["all", "r_legaladvice"]
28
+
29
+ _DATA_URL = {
30
+ "r_legaladvice" :
31
+ {
32
+ "train" : "https://huggingface.co/datasets/pile-of-law/pile-of-law/resolve/main/data/train.r_legaldvice.jsonl.xz",
33
+ "validation" : "https://huggingface.co/datasets/pile-of-law/pile-of-law/resolve/main/data/train.r_legaldvice.jsonl.xz"
34
+ }
35
+ }
36
+
37
+
38
+ class PileOfLaw(datasets.GeneratorBasedBuilder):
39
+ """TODO"""
40
+
41
+ BUILDER_CONFIGS = [datasets.BuilderConfig(name) for name in _VARIANTS]
42
+
43
+ def _info(self):
44
+ return datasets.DatasetInfo(
45
+ description=_DESCRIPTION,
46
+ features=datasets.Features(
47
+ {
48
+ "text": datasets.Value("string"),
49
+ "timestamp": datasets.Value("string"),
50
+ "url": datasets.Value("string"),
51
+ }
52
+ ),
53
+ supervised_keys=None,
54
+ homepage=_URL,
55
+ citation=_CITATION,
56
+ )
57
+
58
+ def _split_generators(self, dl_manager):
59
+ data_urls = {}
60
+ if self.config.name == "all":
61
+ data_sources = list(_DATA_URL.keys())
62
+ else:
63
+ data_sources = [self.config.name]
64
+ for split in ["train", "validation"]:
65
+ data_urls[split] = [
66
+ _DATA_URL[source][split] for source in data_sources
67
+ ]
68
+ train_downloaded_files = dl_manager.download(data_urls["train"])
69
+ validation_downloaded_files = dl_manager.download(data_urls["validation"])
70
+ return [
71
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_downloaded_files}),
72
+ datasets.SplitGenerator(
73
+ name=datasets.Split.VALIDATION, gen_kwargs={"filepaths": validation_downloaded_files}
74
+ ),
75
+ ]
76
+
77
+ def _generate_examples(self, filepaths):
78
+ """This function returns the examples in the raw (text) form by iterating on all the files."""
79
+ id_ = 0
80
+ for filepath in filepaths:
81
+ logger.info("generating examples from = %s", filepath)
82
+ with xz.open(filepath, "rt", encoding="utf-8") as f:
83
+ for line in f:
84
+ if line:
85
+ example = json.loads(line)
86
+ yield id_, example
87
+ id_ += 1