| import csv |
|
|
| import datasets |
| from datasets import Dataset, DatasetInfo, Features, ClassLabel, Value, Sequence, DatasetDict |
| import json |
| from io import StringIO |
| from pathlib import Path |
| import pandas as pd |
|
|
| _CITATION = """""" |
| _DESCRIPTION = """""" |
| _HOMEPAGE = "" |
| _URLS = { |
| "questions": "data/questions.json.zip", |
| "questions_aux": "data/questions_aux.json.zip", |
| "statutes": "data/statutes.tsv.zip", |
| } |
|
|
| _CONFIGS = {} |
|
|
| _CONFIGS["questions"] = { |
| "description": "Questions about housing law.", |
| "features" : Features({ |
| 'idx': Value('int32'), |
| 'state': Value('string'), |
| 'question': Value('string'), |
| 'answer': Value('string'), |
| 'question_group': Value('int32'), |
| 'statutes': [{ |
| 'statute_idx': Value('int32'), |
| 'citation': Value('string'), |
| 'excerpt': Value('string'), |
| }], |
| 'original_question': Value('string'), |
| 'caveats': Sequence(Value('string')), |
| }), |
| "license": None, |
| } |
|
|
| _CONFIGS["questions_aux"] = { |
| "description": "An auxilliary set of larger questions about housing law, without statutory annotations.", |
| "features" : Features({ |
| 'idx': Value('int32'), |
| 'state': Value('string'), |
| 'question': Value('string'), |
| 'answer': Value('string'), |
| 'question_group': Value('int32'), |
| 'statutes': Sequence({ |
| 'citation': Value('string'), |
| 'excerpt': Value('string'), |
| }), |
| 'original_question': Value('string'), |
| 'caveats': Sequence(Value('string')), |
| }), |
| "license": None, |
| } |
|
|
| _CONFIGS["statutes"] = { |
| "description": "Corpus of statutes", |
| "features": Features({ |
| "citation": datasets.Value("string"), |
| "path": datasets.Value("string"), |
| "state": datasets.Value("string"), |
| "text": datasets.Value("string"), |
| "idx": datasets.Value("int32"), |
| }), |
| "license": None, |
| } |
|
|
|
|
| class HousingQA(datasets.GeneratorBasedBuilder): |
| """TODO""" |
|
|
| BUILDER_CONFIGS = [ |
| datasets.BuilderConfig( |
| name=task, version=datasets.Version("1.0.0"), description=task, |
| ) |
| for task in _CONFIGS |
| ] |
|
|
| def _info(self): |
| features = _CONFIGS[self.config.name]["features"] |
| return datasets.DatasetInfo( |
| description=_DESCRIPTION, |
| features=features, |
| homepage=_HOMEPAGE, |
| citation=_CITATION, |
| license=_CONFIGS[self.config.name]["license"], |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Returns SplitGenerators.""" |
| downloaded_file_dir = Path(dl_manager.download_and_extract(_URLS[self.config.name])) |
| return [ |
| datasets.SplitGenerator( |
| name="corpus" if self.config.name == "statutes" else "test", |
| gen_kwargs={ |
| "downloaded_file_dir": downloaded_file_dir, |
| "name": self.config.name, |
| }, |
| ), |
| ] |
|
|
| def _generate_examples(self, downloaded_file_dir, name): |
| """Yields examples as (key, example) tuples.""" |
| |
| if name in ["questions", "questions_aux"]: |
| fpath = downloaded_file_dir / f"{name}.json" |
| data = json.loads(fpath.read_text()) |
| for id_line, data in enumerate(data): |
| yield id_line, data |
|
|
| if name in ["statutes"]: |
| fpath = downloaded_file_dir / f"{name}.tsv" |
| data = pd.read_csv(fpath, sep="\t", dtype={'index': 'int32'}) |
| data = data.to_dict(orient="records") |
| for id_line, data in enumerate(data): |
| yield id_line, data |