File size: 3,173 Bytes
b4a7152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datasets
import pandas as pd
import six

def convert_to_unicode(text):
    """Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
    if six.PY3:
        if isinstance(text, str):
            return text
        elif isinstance(text, bytes):
            return text.decode("utf-8", "ignore")
        else:
            raise ValueError("Unsupported string type: %s" % (type(text)))
    elif six.PY2:
        if isinstance(text, str):
            return text.decode("utf-8", "ignore")
        elif isinstance(text, unicode):
            return text
        else:
            raise ValueError("Unsupported string type: %s" % (type(text)))
    else:
        raise ValueError("Not running on Python2 or Python 3?")


class AGPairDataset(datasets.GeneratorBasedBuilder):
    """A dataset script for loading AGNews(pair). Version 1.0.0"""
    VERSION = datasets.Version("1.0.0")

    _URL = "./"
    _URLS = {
        "train": _URL + "train.tsv",
        # "train_aug": _URL + "train_aug.tsv",
        "test": _URL + "test.tsv",
    }
    _NAMES = [
        "same",
        "different"
    ]

    def _info(self):
        # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
        features = datasets.Features(
            {
                "news1": datasets.Value("string"),
                "news2": datasets.Value("string"),
                "label": datasets.ClassLabel(names=self._NAMES)
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description="Pair of AGNews dataset, generated by AGNews",
            # This defines the different columns of the dataset and their types
            features=features
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        urls_to_download = self._URLS
        downloaded_files = dl_manager.download_and_extract(urls_to_download)

        return [
            datasets.SplitGenerator(name="train", gen_kwargs={"filepath": downloaded_files["train"], "split": "train"}),
            # datasets.SplitGenerator(name="train_aug", gen_kwargs={"filepath": downloaded_files["train_aug"], "split": "train_aug"}),
            datasets.SplitGenerator(name="validation", gen_kwargs={"filepath": downloaded_files["test"], "split": "test"}),
        ]
    
    def _generate_examples(self, filepath, split):

        # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        
        with open(filepath, encoding="utf-8") as f:
            for key,line in enumerate(f):
                if key == 0:
                    continue
                line = line.strip().split("\t")

                yield key, {
                        "news1": convert_to_unicode(line[1]+" - "+line[2]),
                        "news2": convert_to_unicode(line[3]+" - "+line[4]),
                        "label": convert_to_unicode(line[0])
                    }