File size: 2,803 Bytes
47cc9fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195ed0c
47cc9fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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],
                }