File size: 5,408 Bytes
1318418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34e8621
 
 
1318418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
135
136
137
138
139
140
141
142
143
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 SciELOSummarizationConfig(datasets.BuilderConfig):
    """BuilderConfig for SciELOSummarization."""
    def __init__(self, lang, **kwargs):
        """BuilderConfig for ArxivSummarization.
        Args:
            lang: language (`es` for Spanish, `pt` for Portuguese)
          **kwargs: keyword arguments forwarded to super.
        """
        super(SciELOSummarizationConfig, self).__init__(**kwargs)
        self.lang = lang
        

class SciELOSummarizationDataset(datasets.GeneratorBasedBuilder):
    """SciELOSummarization Dataset."""
    
    BUILDER_CONFIGS = [
        SciELOSummarizationConfig(
            name="scielo_es",
            version=datasets.Version("1.0.0"),
            description="SciELO dataset for summarization (Spanish)",
            lang="es",
        ),
        SciELOSummarizationConfig(
            name="scielo_pt",
            version=datasets.Version("1.0.0"),
            description="SciELO dataset for summarization (Portuguese)",
            lang="pt",
        ),
    ]

    
    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_archive = self.config.lang + "_train.zip"
        val_archive = self.config.lang + "_val.zip"
        test_archive = self.config.lang + "_test.zip"
        train_abstracts = self.config.lang + "_train.txt"
        val_abstracts = self.config.lang + "_validation.txt"
        test_abstracts = self.config.lang + "_test.txt"

        train_dir = os.path.join(dl_manager.download_and_extract(train_archive), self.config.lang + "_train")
        val_dir = os.path.join(dl_manager.download_and_extract(val_archive), self.config.lang + "_val")
        test_dir = os.path.join(dl_manager.download_and_extract(test_archive), self.config.lang + "_test")

        train_abstracts = dl_manager.download_and_extract(train_abstracts)
        val_abstracts = dl_manager.download_and_extract(val_abstracts)
        test_abstracts = dl_manager.download_and_extract(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 SciELOSummarization 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"],
                    }