Files changed (3) hide show
  1. README.md +30 -0
  2. imdb.py +42 -29
  3. requirements.txt +1 -0
README.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PIE Dataset Card for "imdb"
2
+
3
+ This is a [PyTorch-IE](https://github.com/ChristophAlt/pytorch-ie) wrapper for the
4
+ [imdb Huggingface dataset loading script](https://huggingface.co/datasets/imdb).
5
+
6
+ ## Data Schema
7
+
8
+ The document type for this dataset is `ImdbDocument` which defines the following data fields:
9
+
10
+ - `text` (str)
11
+ - `id` (str, optional)
12
+ - `metadata` (dictionary, optional)
13
+
14
+ and the following annotation layers:
15
+
16
+ - `label` (annotation type: `Label`, target: `None`)
17
+
18
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/annotations.py) and
19
+ [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/annotations.py) for the annotation
20
+ type definitions.
21
+
22
+ ## Document Converters
23
+
24
+ The dataset provides predefined document converters for the following target document types:
25
+
26
+ - `pie_modules.documents.ExtractiveQADocument` (simple cast without any conversion)
27
+
28
+ See [here](https://github.com/ArneBinder/pie-modules/blob/main/src/pie_modules/documents.py) and
29
+ [here](https://github.com/ChristophAlt/pytorch-ie/blob/main/src/pytorch_ie/documents.py) for the document type
30
+ definitions.
imdb.py CHANGED
@@ -1,55 +1,68 @@
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
 
 
1
  from dataclasses import dataclass
2
+ from typing import Any, Dict
3
 
4
+ import datasets
5
  from pytorch_ie.annotations import Label
6
+ from pytorch_ie.documents import TextDocumentWithLabel
 
7
 
8
+ from pie_datasets import GeneratorBasedBuilder
9
 
10
 
11
+ @dataclass
12
+ class ImdbDocument(TextDocumentWithLabel):
13
+ pass
14
 
 
 
 
 
 
 
15
 
16
+ def example_to_document(example: Dict[str, Any], labels: datasets.ClassLabel) -> ImdbDocument:
17
+ text = example["text"]
18
+ document = ImdbDocument(text=text)
19
+ label_id = example["label"]
20
+ if label_id < 0:
21
+ return document
22
 
23
+ label = labels.int2str(label_id)
24
+ label_annotation = Label(label=label)
25
+ document.label.append(label_annotation)
26
 
27
+ return document
28
 
29
+
30
+ def document_to_example(document: ImdbDocument, labels: datasets.ClassLabel) -> Dict[str, Any]:
31
+ if len(document.label) > 0:
32
+ label_id = labels.str2int(document.label[0].label)
33
+ else:
34
+ label_id = -1
35
+
36
+ return {
37
+ "text": document.text,
38
+ "label": label_id,
39
+ }
40
+
41
+
42
+ class Imdb(GeneratorBasedBuilder):
43
  DOCUMENT_TYPE = ImdbDocument
44
 
45
  BASE_DATASET_PATH = "imdb"
46
+ BASE_DATASET_REVISION = "9c6ede893febf99215a29cc7b72992bb1138b06b"
47
 
48
  BUILDER_CONFIGS = [
49
+ datasets.BuilderConfig(
50
  name="plain_text",
51
  version=datasets.Version("1.0.0"),
52
  description="IMDB sentiment classification dataset",
53
  ),
54
  ]
55
 
56
+ DOCUMENT_CONVERTERS = {TextDocumentWithLabel: {}}
 
57
 
58
+ def _generate_document_kwargs(self, dataset) -> Dict[str, Any]:
59
+ return {"labels": dataset.features["label"]}
60
 
61
+ def _generate_document(self, example, **kwargs) -> ImdbDocument:
62
+ return example_to_document(example, **kwargs)
 
 
 
63
 
64
+ def _generate_example_kwargs(self, dataset) -> Dict[str, Any]:
65
+ return {"labels": dataset.features["label"]}
 
66
 
67
+ def _generate_example(self, document: ImdbDocument, **kwargs) -> Dict[str, Any]:
68
+ return document_to_example(document, **kwargs)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pie-datasets>=0.8.1,<0.9.0