Datasets:

Modalities:
Tabular
Text
Languages:
Chinese
ArXiv:
Libraries:
Datasets
License:
imryanxu commited on
Commit
bc46cd3
·
1 Parent(s): 28f4e7c

Create Hansel.py

Browse files
Files changed (1) hide show
  1. Hansel.py +171 -0
Hansel.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """Hansel: A Chinese Few-Shot and Zero-Shot Entity Linking Benchmark"""
4
+
5
+
6
+ import json
7
+ import os
8
+
9
+ import datasets
10
+
11
+
12
+
13
+ _HANSEL_CITATION = """\
14
+ @misc{xu2022hansel,
15
+ title = {Hansel: A Chinese Few-Shot and Zero-Shot Entity Linking Benchmark},
16
+ author = {Xu, Zhenran and Shan, Zifei and Li, Yuxin and Hu, Baotian and Qin, Bing},
17
+ publisher = {arXiv},
18
+ year = {2022},
19
+ url = {https://arxiv.org/abs/2207.13005}
20
+ }
21
+
22
+ """
23
+
24
+ _HANSEL_DESCRIPTION = """\
25
+ Hansel is a high-quality human-annotated Chinese entity linking (EL) dataset, used for testing Chinese EL systems' generalization ability to tail entities and emerging entities.
26
+ The test set contains Few-shot (FS) and zero-shot (ZS) slices, has 10K examples and uses Wikidata as the corresponding knowledge base.
27
+ The training and validation sets are from Wikipedia hyperlinks, useful for large-scale pretraining of Chinese EL systems.
28
+
29
+ """
30
+
31
+
32
+ _URLS = {
33
+ "train": "hansel-train.jsonl",
34
+ "val": "hansel-val.jsonl",
35
+ "hansel-fs": "hansel-few-shot-v1.jsonl",
36
+ "hansel-zs": "hansel-zero-shot-v1.jsonl",
37
+ }
38
+
39
+ logger = datasets.logging.get_logger(__name__)
40
+
41
+
42
+ class HanselConfig(datasets.BuilderConfig):
43
+ """BuilderConfig for HanselConfig."""
44
+
45
+ def __init__(self, features, data_url, citation, url, **kwargs):
46
+ """BuilderConfig for Hansel.
47
+
48
+ Args:
49
+ features: `list[string]`, list of the features that will appear in the
50
+ feature dict. Should not include "label".
51
+ data_url: `string`, url to download the zip file from.
52
+ citation: `string`, citation for the data set.
53
+ url: `string`, url for information about the data set.
54
+ **kwargs: keyword arguments forwarded to super.
55
+ """
56
+ super(HanselConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
57
+ self.features = features
58
+ self.data_url = data_url
59
+ self.citation = citation
60
+ self.url = url
61
+
62
+
63
+ class Hansel(datasets.GeneratorBasedBuilder):
64
+ """The Hansel benchmark."""
65
+
66
+ BUILDER_CONFIGS = [
67
+ HanselConfig(
68
+ name="wiki",
69
+ description=_HANSEL_DESCRIPTION,
70
+ features=["id", "text", "start", "end", "mention", "gold_id"],
71
+ data_url="https://huggingface.co/datasets/HIT-TMG/Hansel/blob/main/",
72
+ citation=_HANSEL_CITATION,
73
+ url="https://github.com/HITsz-TMG/Hansel",
74
+ )
75
+ HanselConfig(
76
+ name="hansel-few-shot",
77
+ description=_HANSEL_DESCRIPTION,
78
+ features=["id", "text", "start", "end", "mention", "gold_id", "source", "domain"],
79
+ data_url="https://huggingface.co/datasets/HIT-TMG/Hansel/blob/main/",
80
+ citation=_HANSEL_CITATION,
81
+ url="https://github.com/HITsz-TMG/Hansel",
82
+ )
83
+ HanselConfig(
84
+ name="hansel-zero-shot",
85
+ description=_HANSEL_DESCRIPTION,
86
+ features=["id", "text", "start", "end", "mention", "gold_id", "source", "domain"],
87
+ data_url="https://huggingface.co/datasets/HIT-TMG/Hansel/blob/main/",
88
+ citation=_HANSEL_CITATION,
89
+ url="https://github.com/HITsz-TMG/Hansel",
90
+ )
91
+ ]
92
+
93
+ def _info(self):
94
+ features = {feature: datasets.Value("string") for feature in self.config.features}
95
+ features["start"] = datasets.Value("int64")
96
+ features["end"] = datasets.Value("int64")
97
+
98
+ return datasets.DatasetInfo(
99
+ description=self.config.description,
100
+ features=datasets.Features(features),
101
+ homepage=self.config.url,
102
+ citation=self.config.citation
103
+ )
104
+
105
+
106
+ def _split_generators(self, dl_manager):
107
+
108
+ urls_to_download = self._URLS
109
+ downloaded_files = dl_manager.download_and_extract(urls_to_download)
110
+
111
+ if "hansel-few" in self.config.name:
112
+ return [
113
+ datasets.SplitGenerator(
114
+ name=datasets.Split.TEST,
115
+ gen_kwargs={
116
+ "data_file": downloaded_files["hansel-fs"]),
117
+ "split": datasets.Split.TEST,
118
+ },
119
+ ),
120
+ ]
121
+ if "hansel-zero" in self.config.name:
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TEST,
125
+ gen_kwargs={
126
+ "data_file": downloaded_files["hansel-zs"],
127
+ "split": datasets.Split.TEST,
128
+ },
129
+ ),
130
+ ]
131
+ return [
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TRAIN,
134
+ gen_kwargs={
135
+ "data_file": downloaded_files["train"],
136
+ "split": datasets.Split.TRAIN,
137
+ },
138
+ ),
139
+ datasets.SplitGenerator(
140
+ name=datasets.Split.VALIDATION,
141
+ gen_kwargs={
142
+ "data_file": downloaded_files["val"],
143
+ "split": datasets.Split.VALIDATION,
144
+ },
145
+ ),
146
+ ]
147
+
148
+ def _generate_examples(self, data_file, split):
149
+ logger.info("generating examples from = %s", data_file)
150
+ with open(data_file, encoding="utf-8") as f:
151
+ for line in f:
152
+ temDict = json.loads(line)
153
+ key = temDict["id"]
154
+ if "hansel" in self.config.name:
155
+ yield key, {
156
+ "text": temDict["text"],
157
+ "start": temDict["start"],
158
+ "end": temDict["end"],
159
+ "mention": temDict["mention"],
160
+ "gold_id": temDict["gold_id"],
161
+ "source": temDict["source"],
162
+ "domain": temDict["domain"],
163
+ }
164
+ else:
165
+ yield key, {
166
+ "text": temDict["text"],
167
+ "start": temDict["start"],
168
+ "end": temDict["end"],
169
+ "mention": temDict["mention"],
170
+ "gold_id": temDict["gold_id"],
171
+ }