dbp15k-fr-en / dbp15k-fr-en.py
LisaWang0306's picture
Upload dbp15k-fr-en.py
5cd0931
raw
history blame
6.32 kB
import datasets
import os
class EntityAlignmentConfig(datasets.BuilderConfig):
"""BuilderConfig for SuperGLUE."""
def __init__(self, features, data_url, citation, url, label_classes=("False", "True"), **kwargs):
"""BuilderConfig for SuperGLUE.
Args:
features: `list[string]`, list of the features that will appear in the
feature dict. Should not include "label".
data_url: `string`, url to download the zip file from.
citation: `string`, citation for the data set.
url: `string`, url for information about the data set.
label_classes: `list[string]`, the list of classes for the label if the
label is present as a string. Non-string labels will be cast to either
'False' or 'True'.
**kwargs: keyword arguments forwarded to super.
"""
# Version history:
# 1.0.3: Fix not including entity position in ReCoRD.
# 1.0.2: Fixed non-nondeterminism in ReCoRD.
# 1.0.1: Change from the pre-release trial version of SuperGLUE (v1.9) to
# the full release (v2.0).
# 1.0.0: S3 (new shuffling, sharding and slicing mechanism).
# 0.0.2: Initial version.
super(EntityAlignmentConfig, self).__init__(version=datasets.Version("1.0.3"), **kwargs)
self.features = features
self.label_classes = label_classes
self.data_url = data_url
self.citation = citation
self.url = url
class EntityAlignment(datasets.GeneratorBasedBuilder):
"""The SuperGLUE benchmark."""
BUILDER_CONFIGS = [
EntityAlignmentConfig(
name="source",
features=["column1", "column2", "column3"],
data_url="https://www.dropbox.com/s/j55ly9i7w7t4tnn/dbp15k-fr-en-src.zip"
),
EntityAlignmentConfig(
name="target",
features=["column1", "column2", "column3"],
data_url="https://www.dropbox.com/s/eo2huntzhfti1p1/dbp15k-fr-en-tgt.zip"
),
EntityAlignmentConfig(
name="pairs",
features=["left_id", "right_id"],
data_url="https://www.dropbox.com/s/5lhkfka1imuum1o/dbp15k-fr-en-pairs.zip"
),
]
def _info(self):
features = {feature: datasets.Value("string") for feature in self.config.features}
return datasets.DatasetInfo(
features=datasets.Features(features)
)
def _split_generators(self, dl_manager):
dl_dir = dl_manager.download_and_extract(self.config.data_url) or ""
#task_name = _get_task_name_from_data_url(self.config.data_url)
#dl_dir = os.path.join(dl_dir, task_name)
if self.config.name == "source":
return [
datasets.SplitGenerator(
name="ent_ids",
gen_kwargs={
"data_file": os.path.join(dl_dir, "ent_ids_1"),
"split": "ent_ids",
},
),
datasets.SplitGenerator(
name="rel_ids",
gen_kwargs={
"data_file": os.path.join(dl_dir, "rel_ids_1"),
"split": "rel_ids",
},
),
datasets.SplitGenerator(
name="attr_triples",
gen_kwargs={
"data_file": os.path.join(dl_dir, "attr_triples_1"),
"split": "attr_triples",
},
),
datasets.SplitGenerator(
name="rel_triples",
gen_kwargs={
"data_file": os.path.join(dl_dir, "triples_1"),
"split": "rel_triples",
},
),
]
elif self.config.name == "target":
return [
datasets.SplitGenerator(
name="ent_ids",
gen_kwargs={
"data_file": os.path.join(dl_dir, "ent_ids_2"),
"split": "ent_ids",
},
),
datasets.SplitGenerator(
name="rel_ids",
gen_kwargs={
"data_file": os.path.join(dl_dir, "rel_ids_2"),
"split": "rel_ids",
},
),
datasets.SplitGenerator(
name="attr_triples",
gen_kwargs={
"data_file": os.path.join(dl_dir, "attr_triples_2"),
"split": "attr_triples",
},
),
datasets.SplitGenerator(
name="rel_triples",
gen_kwargs={
"data_file": os.path.join(dl_dir, "triples_2"),
"split": "rel_triples",
},
),
]
elif self.config.name == "pairs":
return [
datasets.SplitGenerator(
name="train",
gen_kwargs={
"data_file": os.path.join(dl_dir, "sup_pairs"),
"split": "train",
},
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"data_file": os.path.join(dl_dir, "ref_pairs"),
"split": "test",
},
),
]
def _generate_examples(self, data_file, split):
f = open(data_file,"r",encoding='utf-8')
data = f.readlines()
for i in range(len(data)):
row = data[i].strip('\n').split('\t')
if self.config.name in ["source", "target"]:
if split in ["ent_ids","rel_ids"]:
yield i, {
"column1": row[0],
"column2": row[1],
"column3": None
}
else:
yield i, {
"column1": row[0],
"column2": row[1],
"column3": row[2]
}
if self.config.name == "pairs":
yield i, {
"left_id": row[0],
"right_id": row[1]
}