File size: 4,926 Bytes
493e37d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import os
import datasets
from tqdm import tqdm


_ARTICLE_ID = "article_id"
_ARTICLE_WORDS = "article_words"
_ARTICLE_BBOXES = "article_bboxes"
_ARTICLE_NORM_BBOXES = "article_norm_bboxes"
_ABSTRACT = "abstract"
_ARTICLE_PDF_URL = "article_pdf_url"

def normalize_bbox(bbox, size):
    return [
        int(1000 * bbox[0] / size[0]),
        int(1000 * bbox[1] / size[1]),
        int(1000 * bbox[2] / size[0]),
        int(1000 * bbox[3] / size[1]),
    ]


class ArxivLaySummarizationConfig(datasets.BuilderConfig):
    """BuilderConfig for ArxivLaySummarization."""
    def __init__(self, **kwargs):
        """BuilderConfig for ArxivSummarization.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(ArxivLaySummarizationConfig, self).__init__(**kwargs)
        

class ArxivLaySummarizationDataset(datasets.GeneratorBasedBuilder):
    """ArxivLaySummarization Dataset."""

    _TRAIN_ARCHIVE = "train.zip"
    _VAL_ARCHIVE = "val.zip"
    _TEST_ARCHIVE = "test.zip"
    _TRAIN_ABSTRACTS = "train.txt"
    _VAL_ABSTRACTS = "validation.txt"
    _TEST_ABSTRACTS = "test.txt"
    
    BUILDER_CONFIGS = [
        ArxivLaySummarizationConfig(
            name="arxiv_lay",
            version=datasets.Version("1.0.0"),
            description="Layout-augmented Arxiv dataset for summarization",
        ),
    ]

    
    def _info(self):
        # Should return a datasets.DatasetInfo object
        return datasets.DatasetInfo(
            features=datasets.Features(
                {
                    _ARTICLE_ID: datasets.Value("string"),
                    _ARTICLE_WORDS: datasets.Sequence(datasets.Value("string")),
                    _ARTICLE_BBOXES: datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
                    _ARTICLE_NORM_BBOXES: datasets.Sequence(datasets.Sequence(datasets.Value("int64"))),
                    _ABSTRACT: datasets.Value("string"),
                    _ARTICLE_PDF_URL: datasets.Value("string"),
                }
            ),
            supervised_keys=None,
        )

    def _split_generators(self, dl_manager):

        train_dir = os.path.join(dl_manager.download_and_extract(self._TRAIN_ARCHIVE), "train")
        val_dir = os.path.join(dl_manager.download_and_extract(self._VAL_ARCHIVE), "val")
        test_dir = os.path.join(dl_manager.download_and_extract(self._TEST_ARCHIVE), "test")

        train_abstracts = dl_manager.download_and_extract(self._TRAIN_ABSTRACTS)
        val_abstracts = dl_manager.download_and_extract(self._VAL_ABSTRACTS)
        test_abstracts = dl_manager.download_and_extract(self._TEST_ABSTRACTS)
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, 
                gen_kwargs={"data_path": train_dir, "abstract_path": train_abstracts}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, 
                gen_kwargs={"data_path": val_dir, "abstract_path": val_abstracts}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, 
                gen_kwargs={"data_path": test_dir, "abstract_path": test_abstracts}
            ),
        ]

    def _generate_examples(self, data_path, abstract_path):
        """Generate ArxivLaySummarization examples."""
        filenames = sorted(os.listdir(data_path))

        guid = 0
        with open(abstract_path, 'r') as abstract_file:
            for line in tqdm(abstract_file, total=len(filenames), desc=f"Reading files in {data_path}"):
                guid += 1
                item = json.loads(line)
                fname = item["id"] + ".txt"
                filepath = os.path.join(data_path, fname)
                
                words  = []
                bboxes = []
                norm_bboxes = []

                with open(filepath, encoding="utf-8") as f:
                    for line in f:
                        splits = line.split("\t")
                        word = splits[0]
                        bbox = splits[1:5]
                        bbox = [int(b) for b in bbox]
                        page_width, page_height = int(splits[5]), int(splits[6])
                        norm_bbox = normalize_bbox(bbox, (page_width, page_height))

                        words.append(word)
                        bboxes.append(bbox)
                        norm_bboxes.append(norm_bbox)

                assert len(words) == len(bboxes)
                assert len(bboxes) == len(norm_bboxes)

                yield guid, {
                        _ARTICLE_ID: item["id"],
                        _ARTICLE_WORDS: words, 
                        _ARTICLE_BBOXES: bboxes, 
                        _ARTICLE_NORM_BBOXES: norm_bboxes,
                        _ABSTRACT: item["abstract"],
                        _ARTICLE_PDF_URL: item["pdf_url"],
                    }