Fix streaming TAR archive
#2
by albertvillanova HF Staff - opened
28 unmodified lines29303132333435111 unmodified lines14714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923028 unmodified lines"""import jsonimport osfrom typing import Dict, List, Tupleimport datasets111 unmodified linesdef _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:"""Returns SplitGenerators."""urls = _URLS[_DATASETNAME]data_dir = dl_manager.download_and_extract(urls)return [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"filepath": os.path.join(data_dir, "scicite", "train.jsonl"),"split": "train",},),datasets.SplitGenerator(name=datasets.Split.TEST,gen_kwargs={"filepath": os.path.join(data_dir, "scicite", "test.jsonl"),"split": "test",},),datasets.SplitGenerator(name=datasets.Split.VALIDATION,gen_kwargs={"filepath": os.path.join(data_dir, "scicite", "dev.jsonl"),"split": "dev",},),]def _generate_examples(self, filepath, split: str) -> Tuple[int, Dict]:"""Yields examples as (key, example) tuples."""with open(filepath, "r") as data_file:examples = [json.loads(line) for line in data_file]# Preprocesses exampleskeys = set()for example in examples:# Fixes duplicate keysif example["unique_id"] in keys:example["unique_id"] = example["unique_id"] + "_duplicate"else:keys.add(example["unique_id"])if self.config.schema == "source":for example in examples:yield str(example["unique_id"]), {"string": example["string"],"label": str(example["label"]),"sectionName": str(example["sectionName"]),"citingPaperId": str(example["citingPaperId"]),"citedPaperId": str(example["citedPaperId"]),"excerpt_index": int(example["excerpt_index"]),"isKeyCitation": bool(example["isKeyCitation"]),"label2": str(example.get("label2", "none")),"citeEnd": _safe_int(example["citeEnd"]),"citeStart": _safe_int(example["citeStart"]),"source": str(example["source"]),"label_confidence": float(example.get("label_confidence", np.nan)),"label2_confidence": float(example.get("label2_confidence", np.nan)),"id": str(example["id"]),"unique_id": str(example["unique_id"]),}elif self.config.schema == "bigbio_text":for example in examples:if "label2" in example:labels = [example["label"], example["label2"]]else:labels = [example["label"]]yield str(example["unique_id"]), {"id": example["unique_id"],"document_id": example["citingPaperId"],"text": example["string"],"labels": labels,}def _safe_int(a):28 unmodified lines293031323334111 unmodified lines14614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323428 unmodified lines"""import jsonfrom typing import Dict, List, Tupleimport datasets111 unmodified linesdef _split_generators(self, dl_manager) -> List[datasets.SplitGenerator]:"""Returns SplitGenerators."""urls = _URLS[_DATASETNAME]data_dir = dl_manager.download(urls)return [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"archive": dl_manager.iter_archive(data_dir),"filepath": "scicite/train.jsonl","split": "train",},),datasets.SplitGenerator(name=datasets.Split.TEST,gen_kwargs={"archive": dl_manager.iter_archive(data_dir),"filepath": "scicite/test.jsonl","split": "test",},),datasets.SplitGenerator(name=datasets.Split.VALIDATION,gen_kwargs={"archive": dl_manager.iter_archive(data_dir),"filepath": "scicite/dev.jsonl","split": "dev",},),]def _generate_examples(self, archive, filepath, split: str) -> Tuple[int, Dict]:"""Yields examples as (key, example) tuples."""for path, file in archive:if path == filepath:examples = [json.loads(line) for line in file]break# Preprocesses exampleskeys = set()for example in examples:# Fixes duplicate keysif example["unique_id"] in keys:example["unique_id"] = example["unique_id"] + "_duplicate"else:keys.add(example["unique_id"])if self.config.schema == "source":for example in examples:yield str(example["unique_id"]), {"string": example["string"],"label": str(example["label"]),"sectionName": str(example["sectionName"]),"citingPaperId": str(example["citingPaperId"]),"citedPaperId": str(example["citedPaperId"]),"excerpt_index": int(example["excerpt_index"]),"isKeyCitation": bool(example["isKeyCitation"]),"label2": str(example.get("label2", "none")),"citeEnd": _safe_int(example["citeEnd"]),"citeStart": _safe_int(example["citeStart"]),"source": str(example["source"]),"label_confidence": float(example.get("label_confidence", np.nan)),"label2_confidence": float(example.get("label2_confidence", np.nan)),"id": str(example["id"]),"unique_id": str(example["unique_id"]),}elif self.config.schema == "bigbio_text":for example in examples:if "label2" in example:labels = [example["label"], example["label2"]]else:labels = [example["label"]]yield str(example["unique_id"]), {"id": example["unique_id"],"document_id": example["citingPaperId"],"text": example["string"],"labels": labels,}def _safe_int(a):
scicite.py+56-52