File size: 7,296 Bytes
b0d91ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
959c055
b0d91ee
 
959c055
b0d91ee
 
 
 
 
 
 
 
 
959c055
 
 
 
 
 
 
b0d91ee
 
 
 
 
 
 
 
 
 
4c4fc29
 
b0d91ee
 
 
 
 
 
 
 
 
 
 
4c4fc29
b0d91ee
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import pickle

import datasets

logger = datasets.logging.get_logger(__name__)

# _SUBFIELD = "yg"

# _VERSION = "1.0.0"

# _DESCRIPTION = """\
#     DWY100k-yg is a large-scale monolingual dataset extracted from DBpedia and YAGO3. The suffix yg means DBpedia 
#     to YAGO3. And DWY100k-yg has 100,000 reference entity alignments.
# """

# _CITATION = """\
#     @inproceedings{sun2018bootstrapping,
#     title={Bootstrapping Entity Alignment with Knowledge Graph Embedding.},
#     author={Sun, Zequn and Hu, Wei and Zhang, Qingheng and Qu, Yuzhong},
#     booktitle={IJCAI},
#     volume={18},
#     pages={4396--4402},
#     year={2018}
# }
# """

# _URL = "https://dl.acm.org/doi/10.1145/3485447.3511945"

# _PREFIX = "https://huggingface.co/datasets/matchbench/selfkg-dwy100k-dbpyg"

# _URLS = {
#     "source": f"{_PREFIX}/resolve/main/selfkg-dwy100k-dbp{_SUBFIELD}-src.zip",
#     "target": f"{_PREFIX}/resolve/main/selfkg-dwy100k-dbp{_SUBFIELD}-tgt.zip",
#     "pairs": f"{_PREFIX}/resolve/main/selfkg-dwy100k-dbp{_SUBFIELD}-pairs.zip",
# }

class SelfkgDwy100kygConfig(datasets.BuilderConfig):
    """BuilderConfig for Selfkg-DWY100k."""

    def __init__(self, features, data_url, citation, url, label_classes=("False", "True"), **kwargs):
        """
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(SelfkgDwy100kygConfig, self).__init__(**kwargs)
        self.features = features
        self.label_classes = label_classes
        self.data_url = data_url
        self.citation = citation
        self.url = url

class DWY100kYg(datasets.GeneratorBasedBuilder):
    """DWY100k-yg: A Entity Alignment Dataset. From DBpedia to YAGO3."""

    BUILDER_CONFIGS = [
        SelfkgDwy100kygConfig(
            name="source",
            features=["column1", "column2", "column3"],
            citation="TODO",
            url="TODO",
            data_url="https://huggingface.co/datasets/matchbench/selfkg-dwy100k-dbpyg/resolve/main/selfkg-dwy100k-dbpyg.zip"
        ),
        SelfkgDwy100kygConfig(
            name="target",
            features=["column1", "column2", "column3"],
            citation="TODO",
            url="TODO",
            data_url="https://huggingface.co/datasets/matchbench/selfkg-dwy100k-dbpyg/resolve/main/selfkg-dwy100k-dbpyg.zip"
        ),
        SelfkgDwy100kygConfig(
            name="pairs",
            features=["left_id","right_id"],
            citation="TODO",
            url="TODO",
            data_url="https://huggingface.co/datasets/matchbench/selfkg-dwy100k-dbpyg/resolve/main/selfkg-dwy100k-dbpyg.zip"
        ),
    ]

    def _info(self) -> datasets.DatasetInfo:
        if self.config.name=="source":
            features = {feature: datasets.Value("string") for feature in self.config.features}
        elif self.config.name=="target":
            features = {feature: datasets.Value("string") for feature in self.config.features}
        elif self.config.name=="pairs":
            features = {feature: datasets.Value("int32") 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 ""
        if self.config.name == "source":
            return [
            datasets.SplitGenerator(
                name="ent_ids",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "id_ent_1"),
                    "split": "ent_ids",
                },
            ),
            datasets.SplitGenerator(
                name="rel_triples_id",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "triples_1"),
                    "split": "rel_triples_id",
                },
            ),
            datasets.SplitGenerator(
                name="LaBSE_emb",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "raw_LaBSE_emb_1.pkl"),
                    "split": "LaBSE_emb",
                },
            ),
            ]
        elif self.config.name == "target":
            return [
            datasets.SplitGenerator(
                name="ent_ids",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "id_ent_2"),
                    "split": "ent_ids",
                },
            ),
            datasets.SplitGenerator(
                name="rel_triples_id",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "triples_2"),
                    "split": "rel_triples_id",
                },
            ),
            datasets.SplitGenerator(
                name="LaBSE_emb",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "raw_LaBSE_emb_2.pkl"),
                    "split": "LaBSE_emb",
                },
            ),
            ]
        elif self.config.name == "pairs":
            return [
            datasets.SplitGenerator(
                name="train",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "ref_ent_ids"),
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name="valid",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "valid.ref"),
                    "split": "valid",
                },
            ),
            datasets.SplitGenerator(
                name="test",
                gen_kwargs={
                    "data_file": os.path.join(dl_dir, "ref_ent_ids"),
                    "split": "test",
                },
            ),
            ]
        
    def _generate_examples(self, data_file, split):
        if split in ["LaBSE_emb"]:
            des = pickle.load(open(data_file,"rb"))
            i = -1
            for ent_ids,ori_emb in des.items():
                i += 1
                yield i, {
                    "column1": ent_ids,
                    "column2": ori_emb,
                    "column3": None
                }
        else:    
            f = open(data_file,"r", encoding='utf-8')
            data = f.readlines()
            for i in range(len(data)):
                if self.config.name in ["source", "target"]:
                    if split in ["ent_ids"]:
                        row = data[i].strip('\n').split('\t')
                        yield i, {
                                    "column1": row[0],
                                    "column2": row[1],
                                    "column3": None
                                }
                    elif split in ["rel_triples_id"]:
                        row = data[i].strip('\n').split('\t')
                        yield i, {
                                    "column1": row[0],
                                    "column2": row[1],
                                    "column3": row[2]
                                }
                if self.config.name == "pairs":
                    row = data[i].strip('\n').split('\t')
                    yield i, {
                                "left_id": row[0],
                                "right_id": row[1]
                    }