File size: 2,924 Bytes
8d20d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
import dataclasses
from typing import Any, Dict, List

import datasets
from pytorch_ie.core import (
    Annotation,
    AnnotationLayer,
    AnnotationList,
    annotation_field,
)
from pytorch_ie.documents import TextBasedDocument

from pie_datasets import GeneratorBasedBuilder


@dataclasses.dataclass(eq=True, frozen=True)
class AbstractiveSummary(Annotation):
    """A question about a context."""

    text: str

    def __str__(self) -> str:
        return self.text


@dataclasses.dataclass(eq=True, frozen=True)
class SectionName(Annotation):
    """A question about a context."""

    text: str

    def __str__(self) -> str:
        return self.text


@dataclasses.dataclass
class ScientificPapersDocument(TextBasedDocument):
    """A PIE document for scientific papers dataset."""

    abstract: AnnotationLayer[AbstractiveSummary] = annotation_field()
    section_names: AnnotationList[SectionName] = annotation_field()


def example_to_document(
    example: Dict[str, Any],
) -> ScientificPapersDocument:
    """Convert a Huggingface Scientific Papers example to a PIE document."""
    document = ScientificPapersDocument(
        text=example["article"],
    )
    document.abstract.append(AbstractiveSummary(text=example["abstract"]))
    document.section_names.extend(
        [SectionName(text=section_name) for section_name in example["section_names"].split("\n")]
    )

    return document


def document_to_example(doc: ScientificPapersDocument) -> Dict[str, Any]:
    """Convert a PIE document to a Huggingface Scientific Papers example."""
    example = {
        "article": doc.text,
        "abstract": doc.abstract[0].text,
        "section_names": "\n".join([section_name.text for section_name in doc.section_names]),
    }
    return example


class ScientificPapersConfig(datasets.BuilderConfig):
    """BuilderConfig for Scientific Papers."""

    def __init__(self, **kwargs):
        """BuilderConfig for Scientific Papers.

        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(**kwargs)


class ScientificPapers(GeneratorBasedBuilder):
    DOCUMENT_TYPE = ScientificPapersDocument

    BASE_DATASET_PATH = "scientific_papers"
    BASE_DATASET_REVISION = "14c5296f2d707630f5835c9da59dcaddeea19b20"

    BUILDER_CONFIGS = [
        ScientificPapersConfig(
            name="arxiv",
            version=datasets.Version("1.1.1"),
            description="Scientific Papers dataset - ArXiv variant",
        ),
        ScientificPapersConfig(
            name="pubmed",
            version=datasets.Version("1.1.1"),
            description="Scientific Papers dataset - PubMed variant",
        ),
    ]

    DEFAULT_CONFIG_NAME = "arxiv"

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

    def _generate_example(self, document, **kwargs):
        return document_to_example(document)