ArneBinder commited on
Commit
e52c8ee
1 Parent(s): ba05b31

Create imdb.py

Browse files
Files changed (1) hide show
  1. imdb.py +55 -0
imdb.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+
3
+ import pytorch_ie.data.builder
4
+ from pytorch_ie.annotations import Label
5
+ from pytorch_ie.core import AnnotationList, annotation_field
6
+ from pytorch_ie.documents import TextDocument
7
+
8
+ import datasets
9
+
10
+
11
+ class ImdbConfig(datasets.BuilderConfig):
12
+ """BuilderConfig for IMDB"""
13
+
14
+ def __init__(self, **kwargs):
15
+ """BuilderConfig for IMDB.
16
+ Args:
17
+ **kwargs: keyword arguments forwarded to super.
18
+ """
19
+ super().__init__(**kwargs)
20
+
21
+
22
+ @dataclass
23
+ class ImdbDocument(TextDocument):
24
+ label: AnnotationList[Label] = annotation_field()
25
+
26
+
27
+ class Imdb(pytorch_ie.data.builder.GeneratorBasedBuilder):
28
+ DOCUMENT_TYPE = ImdbDocument
29
+
30
+ BASE_DATASET_PATH = "imdb"
31
+
32
+ BUILDER_CONFIGS = [
33
+ ImdbConfig(
34
+ name="plain_text",
35
+ version=datasets.Version("1.0.0"),
36
+ description="IMDB sentiment classification dataset",
37
+ ),
38
+ ]
39
+
40
+ def _generate_document_kwargs(self, dataset):
41
+ return {"int2str": dataset.features["label"].int2str}
42
+
43
+ def _generate_document(self, example, int2str):
44
+
45
+ text = example["text"]
46
+ document = ImdbDocument(text=text)
47
+ label_id = example["label"]
48
+ if label_id < 0:
49
+ return document
50
+
51
+ label = int2str(label_id)
52
+ label_annotation = Label(label=label)
53
+ document.label.append(label_annotation)
54
+
55
+ return document