us_patent / us_patent_generator.py
bstds's picture
Rename us_patent.py to us_patent_generator.py
adca745
raw
history blame
2.8 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import os
from functools import partial
from typing import Dict, Iterable, Optional
import datasets
import numpy as np
from datasets import DatasetDict, DownloadManager, load_dataset
VERSION = datasets.Version("0.0.1")
AVAILABLE_DATASETS = {
'train': "data/train.csv.zip",
'test': "data/test.csv",
}
class UsPatentDataset(datasets.GeneratorBasedBuilder):
"""UsPatentDataset dataset."""
@staticmethod
def load(data_name_config: Optional[str] = None) -> DatasetDict:
if data_name_config is not None:
ds = load_dataset(__file__, data_name_config)
else:
ds = load_dataset(__file__)
return ds
def _info(self) -> datasets.DatasetInfo:
return datasets.DatasetInfo(
description="",
features=datasets.Features(
{
"id": datasets.Value("string"),
"anchor": datasets.Value("string"),
"target": datasets.Value("string"),
"context": datasets.Value("string"),
"score": datasets.Value("float32"),
}
),
supervised_keys=None,
homepage="https://www.kaggle.com/competitions/us-patent-phrase-to-phrase-matching",
citation="",
)
def _split_generators(
self, dl_manager: DownloadManager
) -> Iterable[datasets.SplitGenerator]:
downloader = partial(
lambda split: dl_manager.download_and_extract(AVAILABLE_DATASETS[split])
)
# There is no predefined train/val/test split for this dataset.
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"split": "train",
"filepath": os.path.join(downloader("train"), "train.csv"),
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"split": "test",
"filepath": downloader("test"),
},
),
]
def _generate_examples(self, split: str, filepath: str) -> Iterable[Dict]:
with open(filepath, encoding="utf-8") as f_in:
csv_reader = csv.reader(
f_in,
delimiter=",",
)
for idx, row in enumerate(csv_reader):
if idx == 0:
continue
yield idx, {
"id": row[0],
"anchor": row[1],
"target": row[2],
"context": row[3],
"score": None if split=="test" else row[4],
}