File size: 3,466 Bytes
3d71fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import os

import datasets
from datasets.tasks import TextClassification

_CITATION = None


_DESCRIPTION = """
 Arxiv Classification Dataset: a classification of Arxiv Papers (11 classes).
 It contains 11 slightly unbalanced classes, 33k Arxiv Papers divided into 3 splits: train (23k), val (5k) and test (5k).
 Copied from "Long Document Classification From Local Word Glimpses via Recurrent Attention Learning" by JUN HE LIQUN WANG LIU LIU, JIAO FENG AND HAO WU
 See: https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8675939
 See: https://github.com/LiqunW/Long-document-dataset
"""

_LABELS = [
    "math.AC",
    "cs.cv", 
    "cs.AI", 
    "cs.SY", 
    "math.GR", 
    "cs.CE", 
    "cs.PL", 
    "cs.IT", 
    "cs.DS", 
    "cs.NE", 
    "math.ST"
    ]


class ArxivClassificationConfig(datasets.BuilderConfig):
    """BuilderConfig for ArxivClassification."""

    def __init__(self, **kwargs):
        """BuilderConfig for ArxivClassification.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(ArxivClassificationConfig, self).__init__(**kwargs)


class ArxivClassificationDataset(datasets.GeneratorBasedBuilder):
    """ArxivClassification Dataset: classification of Arxiv Papers (11 classes)."""
    
    _DOWNLOAD_URL = "https://huggingface.co/datasets/ccdv/arxiv-classification/resolve/main/"
    _TRAIN_FILE = "train_data.txt"
    _VAL_FILE = "val_data.txt"
    _TEST_FILE = "test_data.txt"
    _LABELS_DICT = {label: i for i, label in enumerate(_LABELS)}

    BUILDER_CONFIGS = [

        ArxivClassificationConfig(
            name="arxiv",
            version=datasets.Version("1.0.0"),
            description="Arxiv Classification Dataset: A classification task of Arxiv Papers (11 classes)",
        ),
    ]

    DEFAULT_CONFIG_NAME = "arxiv"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "label": datasets.features.ClassLabel(names=_LABELS),
                }
            ),
            supervised_keys=None,
            citation=_CITATION,
            task_templates=[TextClassification(
                text_column="text", label_column="label")],
        )

    def _split_generators(self, dl_manager):
        train_path = dl_manager.download_and_extract(self._DOWNLOAD_URL + self._TRAIN_FILE)
        val_path = dl_manager.download_and_extract(self._DOWNLOAD_URL + self._VAL_FILE)
        test_path = dl_manager.download_and_extract(self._DOWNLOAD_URL + self._TEST_FILE)
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, gen_kwargs={"filepath": val_path}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}
            ),
        ]
    
    def _generate_examples(self, filepath):
        """Generate ArxivClassification examples."""
        with open(filepath, encoding="utf-8") as f:
            for id_, row in enumerate(f):
                data = json.loads(row)
                label = self._LABELS_DICT[data["label"]]
                text = data["text"]
                yield id_, {"text": text, "label": label}