File size: 9,890 Bytes
8d1b163
 
 
 
 
 
 
 
 
 
 
 
 
2ee35ff
8d1b163
 
 
 
 
39ddb78
 
8d1b163
 
 
822fdb5
8d1b163
 
 
 
822fdb5
8d1b163
 
822fdb5
8d1b163
2ee35ff
8d1b163
 
2ee35ff
8d1b163
822fdb5
8d1b163
 
 
 
 
5094fd8
2ee35ff
8d1b163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5094fd8
 
 
 
 
 
 
 
a2e43c2
 
 
 
 
 
 
5094fd8
8d1b163
 
 
 
5094fd8
8d1b163
 
 
 
5094fd8
8d1b163
 
 
 
5094fd8
8d1b163
 
 
 
a2e43c2
39ddb78
 
a2e43c2
39ddb78
 
 
a2e43c2
39ddb78
 
 
a2e43c2
39ddb78
 
 
a2e43c2
 
 
8d1b163
 
 
a2e43c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The SOTAB dataset is a large-scale dataset for the task of column type annotation on semi-structured tables."""

import os

import datasets

import pandas as pd


# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@inproceedings{madoc63868, pages = {14--19}, booktitle = {SemTab 2022 : Proceedings of the Semantic Web Challenge on Tabular Data to Knowledge Graph Matching, co-located with the 21st International semantic Web Conference (ISWC 2022), virtual conference, October 23-27, 2022}, address = {Aachen, Germany}, editor = {Vasilis Efthymiou and Ernesto Jim{\'e}nez-Ruiz and Jiaoyan Chen and Vincenzo Cutrona and Oktie Hassanzadeh and Juan Sequeda and Kavitha Srinivas and Nora Abdelmageed and Madelon Hulsebos}, journal = {CEUR Workshop Proceedings}, year = {2022}, title = {SOTAB: The WDC Schema.org table annotation benchmark}, publisher = {RWTH Aachen}, language = {Englisch}, author = {Keti Korini and Ralph Peeters and Christian Bizer}, volume = {3320}, abstract = {Understanding the semantics of table elements is a prerequisite for many data integration and data discovery tasks. Table annotation is the task of labeling table elements with terms from a given vocabulary. This paper presents the WDC Schema.org Table Annotation Benchmark (SOTAB) for comparing the performance of table annotation systems. SOTAB covers the column type annotation (CTA) and columns property annotation (CPA) tasks. SOTAB provides {$\sim$}50,000 annotated tables for each of the tasks containing Schema.org data from different websites. The tables cover 17 different types of entities such as movie, event, local business, recipe, job posting, or product. The tables stem from the WDC Schema.org Table Corpus which was created by extracting Schema.org annotations from the Common Crawl. Consequently, the labels used for annotating columns in SOTAB are part of the Schema.org vocabulary. The benchmark covers 91 types for CTA and 176 properties for CPA distributed across textual, numerical and date/time columns. The tables are split into fixed training, validation and test sets. The test sets are further divided into subsets focusing on specific challenges, such as columns with missing values or different value formats, in order to allow a more fine-grained comparison of annotation systems. The evaluation of SOTAB using Doduo and TURL shows that the benchmark is difficult to solve for current state-of-the-art systems.}, url = {https://madoc.bib.uni-mannheim.de/63868/} }
"""

# You can copy an official description
_DESCRIPTION = """\
Understanding the semantics of table elements is a prerequisite for many data integration and data discovery tasks. Table annotation is the task of labeling table elements with terms from a given vocabulary. This paper presents the WDC Schema.org Table Annotation Benchmark (SOTAB) for comparing the performance of table annotation systems. SOTAB covers the column type annotation (CTA) and columns property annotation (CPA) tasks. SOTAB provides ∼50,000 annotated tables for each of the tasks containing Schema.org data from different websites. The tables cover 17 different types of entities such as movie, event, local business, recipe, job posting, or product. The tables stem from the WDC Schema.org Table Corpus which was created by extracting Schema.org annotations from the Common Crawl. Consequently, the labels used for annotating columns in SOTAB are part of the Schema.org vocabulary. The benchmark covers 91 types for CTA and 176 properties for CPA distributed across textual, numerical and date/time columns. The tables are split into fixed training, validation and test sets. The test sets are further divided into subsets focusing on specific challenges, such as columns with missing values or different value formats, in order to allow a more fine-grained comparison of annotation systems. The evaluation of SOTAB using Doduo and TURL shows that the benchmark is difficult to solve for current state-of-the-art systems.
"""

_HOMEPAGE = "https://webdatacommons.org/structureddata/sotab/"

_LICENSE = ""

class WikiTableQuestions(datasets.GeneratorBasedBuilder):
    """The SOTAB dataset is a large-scale dataset for the task of column type annotation on semi-structured tables."""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "id": datasets.Value("string"),
                "column_index": datasets.Value("int32"),
                "label": datasets.features.Sequence(datasets.Value("string")),
                "table": {
                    "header": datasets.features.Sequence(datasets.Value("string")),
                    "rows": datasets.features.Sequence(datasets.features.Sequence(datasets.Value("string"))),
                    "name": datasets.Value("string"),
                },
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            # supervised_keys=("sentence", "label"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        train_url = "https://huggingface.co/datasets/shivangibithel/SOTAB/resolve/main/CTA_Training.zip"
        dev_url = "https://huggingface.co/datasets/shivangibithel/SOTAB/resolve/main/CTA_Validation.zip"
        test_url = "https://huggingface.co/datasets/shivangibithel/SOTAB/resolve/main/CTA_Test.zip"
        
        CTA_Training = os.path.join(dl_manager.download_and_extract(train_url), "CTA_Training")
        CTA_Validation = os.path.join(dl_manager.download_and_extract(dev_url), "CTA_Validation")
        CTA_Test = os.path.join(dl_manager.download_and_extract(test_url), "CTA_Test")

        # train_file = "{}.json.gz".format(self.config.name)
        # test_file = "{}.json.gz".format(self.config.name)
        # dev_file = "{}.json.gz".format(self.config.name)

        train_file = "CTA_training_gt.csv".format(self.config.name)
        test_file = "CTA_test_gt.csv".format(self.config.name)
        dev_file = "CTA_validation_gt.csv".format(self.config.name)
        
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"main_filepath": os.path.join(root_dir, "Train", train_file), "root_dir": CTA_Training},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"main_filepath": os.path.join(root_dir, "Test", test_file), "root_dir": CTA_Test},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={"main_filepath": os.path.join(root_dir, "Validation", dev_file), "root_dir": CTA_Validation},
            ),
        ]

    def _read_table_from_file(self, table_name: str, root_dir: str):
        df_ = pd.read_json(os.path.join(root_dir, table_name), compression='gzip', lines=True)
        
        col = []
        for j in range(len(df_.loc[0])):
            col.append("col" + str(j+1))

        row = []
        for index in range(len(df_)):
            row.append(df.loc[index, :].values.tolist())

        # {"header": ["col1", "col2", "col3"], "rows": [["row11", "row12", "row13"], ["row21", "row22", "row23"]]}
            
        table_context = {}
        table_context["header"] = col
        table_context["rows"] = row
        table_context["name"] = table_name

        return table_context

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, main_filepath, root_dir):

        df = pd.read_csv(main_filepath,  encoding="utf8")
        for ind in df.index:
            example_id = ind
            table_name = df['table_name'][ind]
            column_index = df['column_index'][ind]
            label = df['label'][ind]
            table_content = self._read_table_from_file(table_name, root_dir)
            
        # for idx, line in enumerate(f):
        #     example_id, question, table_name, answer = line.strip("\n").split("\t")
        #     answer = answer.split("|")
        #     # must contain rows and header keys
        #     table_content = self._read_table_from_file(table_name, root_dir)

            yield idx, {"id": example_id, "column_index": column_index, "label": label, "table": table_content}