# coding=utf-8 # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """The Legal Pile (Preprocessed)""" import datasets import json try: import lzma as xz except ImportError: import pylzma as xz logger = datasets.logging.get_logger(__name__) _DESCRIPTION = """The Legal Pile (Preprocessed)""" _HOMEPAGE = "https://iliaschalkidis.github.io/" _VERSION = "1.0.0" class LegalPileConfig(datasets.BuilderConfig): """BuilderConfig for Legal Pile Corpora""" def __init__(self, name, n_files, **kwargs): super(LegalPileConfig, self).__init__(**kwargs) self.name = name self.filepath = f'https://huggingface.co/datasets/lexlms/the_legal_pile_preprocessed/' \ f'resolve/main/data_files/{self.name}.jsonl.tar.xz' self.n_files = n_files class LegalPile(datasets.GeneratorBasedBuilder): """ The Legal Pile """ VERSION = datasets.Version(_VERSION) BUILDER_CONFIG_CLASS = LegalPileConfig BUILDER_CONFIGS = [ LegalPileConfig( name=f"eu-legislation", n_files=1, description="EU Legislation", ), LegalPileConfig( name=f"eu-court-cases", n_files=1, description="EU Court cases", ), LegalPileConfig( name=f"ecthr-cases", n_files=1, description="ECtHR cases", ), LegalPileConfig( name=f"uk-legislation", n_files=1, description="UK Legislation", ), LegalPileConfig( name=f"uk-court-cases", n_files=1, description="UK Court cases", ), LegalPileConfig( name=f"indian-court-cases", n_files=1, description="Indian Court cases", ), LegalPileConfig( name=f"canadian-legislation", n_files=1, description="Canadian Legislation", ), LegalPileConfig( name=f"canadian-court-cases", n_files=1, description="Canadian Legislation", ), LegalPileConfig( name=f"us-legislation", n_files=1, description="US Legislation", ), LegalPileConfig( name=f"us-contracts", n_files=7, description="US Contracts", ), LegalPileConfig( name=f"us-court-cases", n_files=16, description="US Court cases", ) ] def _info(self): features = datasets.Features( { "text": datasets.Value("string") } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=None, homepage=_HOMEPAGE, ) def _split_generators(self, dl_manager): train_data_dir = dl_manager.download([self.config.filepath.replace('.jsonl.tar.xz', f'_train_{idx+1}.jsonl.tar.xz') for idx in range(self.config.n_files)]) test_data_dir = dl_manager.download([self.config.filepath.replace('.jsonl.tar.xz', '_test_1.jsonl.tar.xz')]) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_data_dir, "split": 'train'}, ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"filepaths": test_data_dir, "split": 'test'}, ) ] def _generate_examples(self, filepaths, split): """ Reads line by line samples and generates examples. :param filepath: Path to jsonl files with line by line examples. """ id_ = 0 for filepath in filepaths: logger.info("⏳ Generating examples from = %s", filepath) with xz.open(open(filepath, "rb"), "rt", encoding='utf-8') as f: for row in f: if row: try: data = json.loads(row) yield id_, { "text": data["text"] } id_ += 1 except: continue