File size: 1,926 Bytes
e52c8ee
93418a8
e52c8ee
93418a8
e52c8ee
93418a8
e52c8ee
93418a8
e52c8ee
 
93418a8
 
 
e52c8ee
 
93418a8
 
 
 
 
 
e52c8ee
93418a8
 
 
e52c8ee
93418a8
e52c8ee
93418a8
 
 
 
 
 
 
 
 
 
 
 
 
 
e52c8ee
 
 
93418a8
e52c8ee
 
93418a8
e52c8ee
 
 
 
 
 
93418a8
e52c8ee
93418a8
 
e52c8ee
93418a8
 
e52c8ee
93418a8
 
e52c8ee
93418a8
 
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
from dataclasses import dataclass
from typing import Any, Dict

import datasets
from pytorch_ie.annotations import Label
from pytorch_ie.documents import TextDocumentWithLabel

from pie_datasets import GeneratorBasedBuilder


@dataclass
class ImdbDocument(TextDocumentWithLabel):
    pass


def example_to_document(example: Dict[str, Any], labels: datasets.ClassLabel) -> ImdbDocument:
    text = example["text"]
    document = ImdbDocument(text=text)
    label_id = example["label"]
    if label_id < 0:
        return document

    label = labels.int2str(label_id)
    label_annotation = Label(label=label)
    document.label.append(label_annotation)

    return document


def document_to_example(document: ImdbDocument, labels: datasets.ClassLabel) -> Dict[str, Any]:
    if len(document.label) > 0:
        label_id = labels.str2int(document.label[0].label)
    else:
        label_id = -1

    return {
        "text": document.text,
        "label": label_id,
    }


class Imdb(GeneratorBasedBuilder):
    DOCUMENT_TYPE = ImdbDocument

    BASE_DATASET_PATH = "imdb"
    BASE_DATASET_REVISION = "9c6ede893febf99215a29cc7b72992bb1138b06b"

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="plain_text",
            version=datasets.Version("1.0.0"),
            description="IMDB sentiment classification dataset",
        ),
    ]

    DOCUMENT_CONVERTERS = {TextDocumentWithLabel: {}}

    def _generate_document_kwargs(self, dataset) -> Dict[str, Any]:
        return {"labels": dataset.features["label"]}

    def _generate_document(self, example, **kwargs) -> ImdbDocument:
        return example_to_document(example, **kwargs)

    def _generate_example_kwargs(self, dataset) -> Dict[str, Any]:
        return {"labels": dataset.features["label"]}

    def _generate_example(self, document: ImdbDocument, **kwargs) -> Dict[str, Any]:
        return document_to_example(document, **kwargs)