File size: 14,676 Bytes
8d1b163 ec08338 49066ef 8d1b163 49066ef 36cb928 be9ca74 8d1b163 5b4ddef 2c87312 a752839 a90d318 5094fd8 8d1b163 e3bbe00 8d1b163 36cb928 8d1b163 36cb928 8d1b163 49066ef 36cb928 49066ef 36cb928 49066ef ec08338 49066ef ec08338 49066ef 39ddb78 49066ef 39ddb78 49066ef 39ddb78 49066ef a2e43c2 49066ef a2e43c2 49066ef 8d1b163 49066ef a2e43c2 49066ef |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import os
import pandas as pd
import datasets
def _read_table_from_file(table_name: str, root_dir: str)-> dict:
df_ = pd.read_json(os.path.join(root_dir, table_name), compression='gzip', lines=True)
df_ = df_.astype(str)
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_content = {}
table_content["header"] = col
table_content["rows"] = row
table_content["name"] = table_name
return table_content
class SOTAB(datasets.GeneratorBasedBuilder):
"""The SOTAB dataset"""
def _info(self):
features = datasets.Features(
{
# "id": datasets.Value("int32"),
"column_index": datasets.Value("string"),
"label": 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"),
},
}
)
# datasets.value -- single value
# datasets.features.Sequence -- list
return datasets.DatasetInfo(
features=features
)
def _split_generators(self, dl_manager):
train_url = "https://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Training.zip"
dev_url = "https://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Validation.zip"
test_url = "https://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Test.zip"
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
CTA_Training = os.path.join(dl_manager.download_and_extract(train_url))
CTA_Validation = os.path.join(dl_manager.download_and_extract(dev_url))
CTA_Test = os.path.join(dl_manager.download_and_extract(test_url))
train_file = "CTA_training_gt.csv"
test_file = "CTA_test_gt.csv"
dev_file = "CTA_validation_gt.csv"
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={"main_filepath": os.path.join(CTA_Training, train_file), "root_dir": os.path.join(CTA_Training, "Train")},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={"main_filepath": os.path.join(CTA_Validation, test_file), "root_dir": os.path.join(CTA_Validation, "Test")},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={"main_filepath": os.path.join(CTA_Test, dev_file), "root_dir": os.path.join(CTA_Test, "Validation")},
),
]
def _generate_examples(self, main_filepath, root_dir):
with open(main_filepath, encoding="utf-8") as f:
for idx, line in enumerate(f):
# skip the header
if idx == 0:
continue
table_name, column_index, label = line.strip("\n").split(",")
print(table_name, column_index, label)
yield idx, {
"column_index": column_index,
"label": label,
"table": _read_table_from_file(table_name, root_dir),
}
# # 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
# import gzip
# import pandas as pd
# from ast import literal_eval
# # 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 SOTAB(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("int32"),
# "column_index": datasets.Value("string"),
# "label": 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"),
# },
# }
# )
# # datasets.value -- single value
# # datasets.features.Sequence -- list
# 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://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Training.zip"
# dev_url = "https://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Validation.zip"
# test_url = "https://data.dws.informatik.uni-mannheim.de/structureddata/sotab/CTA_Test.zip"
# # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
# CTA_Training = os.path.join(dl_manager.download_and_extract(train_url))
# CTA_Validation = os.path.join(dl_manager.download_and_extract(dev_url))
# CTA_Test = os.path.join(dl_manager.download_and_extract(test_url))
# train_file = "CTA_training_gt.csv"
# test_file = "CTA_test_gt.csv"
# dev_file = "CTA_validation_gt.csv"
# return [
# datasets.SplitGenerator(
# name=datasets.Split.TRAIN,
# # These kwargs will be passed to _generate_examples
# gen_kwargs={"main_filepath": os.path.join(CTA_Training, train_file), "root_dir": os.path.join(CTA_Training, "Train")},
# ),
# datasets.SplitGenerator(
# name=datasets.Split.TEST,
# # These kwargs will be passed to _generate_examples
# gen_kwargs={"main_filepath": os.path.join(CTA_Test, test_file), "root_dir": os.path.join(CTA_Test, "Test")},
# ),
# datasets.SplitGenerator(
# name=datasets.Split.VALIDATION,
# # These kwargs will be passed to _generate_examples
# gen_kwargs={"main_filepath": os.path.join(CTA_Validation, dev_file), "root_dir": os.path.join(CTA_Validation, "Validation")},
# ),
# ]
# def _read_table_from_file(self, table_name: str, root_dir: str):
# # fn = os.path.join(root_dir, table_name)
# # with gzip.open(fn, 'rt', encoding='UTF-8') as zipfile:
# # data = [literal_eval(v.strip()) for v in zipfile]
# # # create the dataframe
# # df_ = pd.DataFrame(data)
# df_ = pd.read_json(os.path.join(root_dir, table_name), compression='gzip', lines=True)
# df_ = df_.astype(str)
# 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_content = {}
# table_content["header"] = col
# table_content["rows"] = row
# table_content["name"] = table_name
# return table_content
# # 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")
# df = df.astype({'table_name':'string','column_index':'string', 'label':'string'})
# 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)
# yield ind, {"column_index": column_index, "label": label, "table": table_content}
# # yield {"id": example_id, "column_index": column_index, "label": label, "table": table_content}
|