Gaëtan Caillaut commited on
Commit
a84d606
·
1 Parent(s): 6d66813

Initial commit with data

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. README.md +52 -0
  3. data.tar.gz +3 -0
  4. enwiki_el_dataset.py +286 -0
.gitattributes CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
35
  *.mp3 filter=lfs diff=lfs merge=lfs -text
36
  *.ogg filter=lfs diff=lfs merge=lfs -text
37
  *.wav filter=lfs diff=lfs merge=lfs -text
 
 
35
  *.mp3 filter=lfs diff=lfs merge=lfs -text
36
  *.ogg filter=lfs diff=lfs merge=lfs -text
37
  *.wav filter=lfs diff=lfs merge=lfs -text
38
+ data.tar.gz filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ annotations_creators:
3
+ - machine-generated
4
+ language_creators: []
5
+ languages:
6
+ - en-EN
7
+ licenses:
8
+ - wtfpl
9
+ multilinguality:
10
+ - monolingual
11
+ pretty_name: test
12
+ size_categories:
13
+ - unknown
14
+ source_datasets:
15
+ - original
16
+ task_categories:
17
+ - other
18
+ task_ids: []
19
+ ---
20
+
21
+ # Dataset Card for frwiki_good_pages_el
22
+
23
+ ## Dataset Description
24
+
25
+ - Repository: [enwiki_el](https://github.com/GaaH/enwiki_el)
26
+ - Point of Contact: [Gaëtan Caillaut](mailto://[email protected])
27
+
28
+ ### Dataset Summary
29
+
30
+ It is intended to be used to train Entity Linking (EL) systems. Links in Wikipedia articles are used to detect named entities.
31
+
32
+ ### Languages
33
+
34
+ - English
35
+
36
+ ## Dataset Structure
37
+
38
+ ```
39
+ {
40
+ "title": "Title of the page",
41
+ "qid": "QID of the corresponding Wikidata entity",
42
+ "words": ["tokens"],
43
+ "wikipedia": ["Wikipedia description of each entity"],
44
+ "labels": ["NER labels"],
45
+ "titles": ["Wikipedia title of each entity"],
46
+ "qids": ["QID of each entity"],
47
+ }
48
+ ```
49
+
50
+ The `words` field contains the article’s text splitted on white-spaces. The other fields are list with same length as `words` and contains data only when the respective token in `words` is the __start of an entity__. For instance, if the _i-th_ token in `words` is an entity, then the _i-th_ element of `wikipedia` contains a description, extracted from Wikipedia, of this entity. The same applies for the other fields. If the entity spans multiple words, then only the index of the first words contains data.
51
+
52
+ The only exception is the `labels` field, which is used to delimit entities. It uses the IOB encoding: if the token is not part of an entity, the label is `"O"`; if it is the first word of a multi-word entity, the label is `"B"`; otherwise the label is `"I"`.
data.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f9a687a7120f6c5993a8f24471cae5e4f9c4f60d597d941fb7d6f699778b54e6
3
+ size 7320271946
enwiki_el_dataset.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import pandas as pd
19
+ import re
20
+ import gzip
21
+ import json
22
+ import datasets
23
+ from pathlib import Path
24
+
25
+
26
+ def get_open_method(path):
27
+ path = Path(path)
28
+ ext = path.suffix
29
+
30
+ if ext == ".gz":
31
+ import gzip
32
+ open_func = gzip.open
33
+ elif ext == ".bz2":
34
+ import bz2
35
+ open_func = bz2.open
36
+ else:
37
+ open_func = open
38
+ return open_func
39
+
40
+
41
+ def read_file(path):
42
+ open_func = get_open_method(path)
43
+ with open_func(path, "rt", encoding="UTF-8") as f:
44
+ return f.read()
45
+
46
+
47
+ # TODO: Add BibTeX citation
48
+ # Find for instance the citation on arxiv or on the dataset repo/website
49
+ _CITATION = ""
50
+
51
+ _DESCRIPTION = """\
52
+ English Wikipedia dataset for Entity Linking
53
+ """
54
+
55
+ _HOMEPAGE = "https://github.com/GaaH/enwiki_el"
56
+
57
+ _LICENSE = ""
58
+
59
+ _URLs = {
60
+ "enwiki": "data.tar.gz",
61
+ "entities": "data.tar.gz",
62
+ }
63
+
64
+ _NER_CLASS_LABELS = [
65
+ "B",
66
+ "I",
67
+ "O",
68
+ ]
69
+
70
+
71
+ def text_to_el_features(doc_qid, doc_title, text, title2qid, title2wikipedia):
72
+ res = {
73
+ "title": doc_title.replace("_", " "),
74
+ "qid": doc_qid,
75
+ }
76
+ text_dict = {
77
+ "words": [],
78
+ "labels": [],
79
+ "qids": [],
80
+ "titles": [],
81
+ "wikipedia": [],
82
+ }
83
+ entity_pattern = r"\[E=(.+?)\](.+?)\[/E\]"
84
+
85
+ # start index of the previous text
86
+ i = 0
87
+ for m in re.finditer(entity_pattern, text):
88
+ mention_title = m.group(1)
89
+ mention = m.group(2)
90
+
91
+ mention_qid = title2qid.get(mention_title, "").replace("_", " ")
92
+ mention_wikipedia = title2wikipedia.get(mention_title, "")
93
+
94
+ # Removes entity tags in descriptions
95
+ mention_wikipedia = re.sub(entity_pattern, r"\2", mention_wikipedia)
96
+
97
+ # mention_qid = title2qid.get(mention_title, "YARIEN")
98
+ # mention_wikipedia = title2wikipedia.get(mention_title, "YARIEN")
99
+ # mention_wikidata = title2wikidata.get(mention_title, "YARIEN")
100
+
101
+ mention_words = mention.split()
102
+
103
+ j = m.start(0)
104
+ prev_text = text[i:j].split()
105
+ len_prev_text = len(prev_text)
106
+ text_dict["words"].extend(prev_text)
107
+ text_dict["labels"].extend(["O"] * len_prev_text)
108
+ text_dict["qids"].extend([None] * len_prev_text)
109
+ text_dict["titles"].extend([None] * len_prev_text)
110
+ text_dict["wikipedia"].extend([None] * len_prev_text)
111
+
112
+ text_dict["words"].extend(mention_words)
113
+
114
+ # If there is no description, learning can’t be done so we treat the mention as not en entity
115
+ if mention_wikipedia == "":
116
+ len_mention = len(mention_words)
117
+ text_dict["labels"].extend(["O"] * len_mention)
118
+ text_dict["qids"].extend([None] * len_mention)
119
+ text_dict["titles"].extend([None] * len_mention)
120
+ text_dict["wikipedia"].extend([None] * len_mention)
121
+ else:
122
+ len_mention_tail = len(mention_words) - 1
123
+ # wikipedia_words = mention_wikipedia.split()
124
+ # wikidata_words = mention_wikidata.split()
125
+ # title_words = mention_title.replace("_", " ").split()
126
+
127
+ text_dict["labels"].extend(["B"] + ["I"] * len_mention_tail)
128
+ text_dict["qids"].extend([mention_qid] + [None] * len_mention_tail)
129
+ text_dict["titles"].extend(
130
+ [mention_title] + [None] * len_mention_tail)
131
+ text_dict["wikipedia"].extend(
132
+ [mention_wikipedia] + [None] * len_mention_tail)
133
+
134
+ i = m.end(0)
135
+
136
+ tail = text[i:].split()
137
+ len_tail = len(tail)
138
+ text_dict["words"].extend(tail)
139
+ text_dict["labels"].extend(["O"] * len_tail)
140
+ text_dict["qids"].extend([None] * len_tail)
141
+ text_dict["titles"].extend([None] * len_tail)
142
+ text_dict["wikipedia"].extend([None] * len_tail)
143
+ res.update(text_dict)
144
+ return res
145
+
146
+
147
+ class EnWikiELDataset(datasets.GeneratorBasedBuilder):
148
+ """
149
+ """
150
+
151
+ VERSION = datasets.Version("0.1.0")
152
+
153
+ # This is an example of a dataset with multiple configurations.
154
+ # If you don't want/need to define several sub-sets in your dataset,
155
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
156
+
157
+ # If you need to make complex sub-parts in the datasets with configurable options
158
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
159
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
160
+
161
+ # You will be able to load one or the other configurations in the following list with
162
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
163
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
164
+ BUILDER_CONFIGS = [
165
+ datasets.BuilderConfig(name="enwiki", version=VERSION,
166
+ description="The enwiki dataset for Entity Linking"),
167
+ datasets.BuilderConfig(name="entities", version=VERSION,
168
+ description="Entities and their descriptions"),
169
+ ]
170
+
171
+ # It's not mandatory to have a default configuration. Just use one if it make sense.
172
+ DEFAULT_CONFIG_NAME = "enwiki"
173
+
174
+ def _info(self):
175
+ if self.config.name == "enwiki":
176
+ features = datasets.Features({
177
+ "title": datasets.Value("string"),
178
+ "qid": datasets.Value("string"),
179
+ "words": [datasets.Value("string")],
180
+ "wikipedia": [datasets.Value("string")],
181
+ "labels": [datasets.ClassLabel(names=_NER_CLASS_LABELS)],
182
+ "titles": [datasets.Value("string")],
183
+ "qids": [datasets.Value("string")],
184
+ })
185
+ elif self.config.name == "entities":
186
+ features = datasets.Features({
187
+ "qid": datasets.Value("string"),
188
+ "title": datasets.Value("string"),
189
+ "wikipedia": datasets.Value("string"),
190
+ })
191
+
192
+ return datasets.DatasetInfo(
193
+ # This is the description that will appear on the datasets page.
194
+ description=_DESCRIPTION,
195
+ # This defines the different columns of the dataset and their types
196
+ # Here we define them above because they are different between the two configurations
197
+ features=features,
198
+ # If there's a common (input, target) tuple from the features,
199
+ # specify them here. They'll be used if as_supervised=True in
200
+ # builder.as_dataset.
201
+ supervised_keys=None,
202
+ # Homepage of the dataset for documentation
203
+ homepage=_HOMEPAGE,
204
+ # License for the dataset if available
205
+ license=_LICENSE,
206
+ # Citation for the dataset
207
+ citation=_CITATION,
208
+ )
209
+
210
+ def _split_generators(self, dl_manager):
211
+ """Returns SplitGenerators."""
212
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
213
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
214
+
215
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
216
+ # 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.
217
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
218
+ my_urls = _URLs[self.config.name]
219
+ data_dir = dl_manager.download_and_extract(my_urls)
220
+ return [
221
+ datasets.SplitGenerator(
222
+ name=datasets.Split.TRAIN,
223
+ # These kwargs will be passed to _generate_examples
224
+ gen_kwargs={
225
+ "data_dir": Path(data_dir, "data"),
226
+ "split": "train"
227
+ }
228
+ )
229
+ ]
230
+
231
+ def _generate_examples(
232
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
233
+ self, data_dir, split
234
+ ):
235
+ """ Yields examples as (key, example) tuples. """
236
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
237
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
238
+
239
+ entities_path = Path(data_dir, "entities.jsonl.gz")
240
+ corpus_path = Path(data_dir, "corpus.jsonl.gz")
241
+
242
+ def _identiy(x):
243
+ return x
244
+
245
+ if self.config.name == "enwiki":
246
+ title2wikipedia = {}
247
+ title2qid = {}
248
+ with gzip.open(entities_path, "rt", encoding="UTF-8") as ent_file:
249
+ for line in ent_file:
250
+ item = json.loads(
251
+ line, parse_int=_identiy, parse_float=_identiy, parse_constant=_identiy)
252
+ title = item["title"]
253
+ title2wikipedia[title] = item["wikipedia_description"]
254
+ title2qid[title] = item["qid"]
255
+
256
+ with gzip.open(corpus_path, "rt", encoding="UTF-8") as crps_file:
257
+ for id, line in enumerate(crps_file):
258
+ item = json.loads(line, parse_int=lambda x: x,
259
+ parse_float=lambda x: x, parse_constant=lambda x: x)
260
+ qid = item["qid"]
261
+ title = item["title"]
262
+ text = item["text"]
263
+
264
+ features = text_to_el_features(
265
+ qid, title, text, title2qid, title2wikipedia)
266
+ yield id, features
267
+ elif self.config.name == "entities":
268
+ entity_pattern = r"\[E=(.+?)\](.+?)\[/E\]"
269
+ with gzip.open(entities_path, "rt", encoding="UTF-8") as ent_file:
270
+ for id, line in enumerate(ent_file):
271
+ item = json.loads(
272
+ line, parse_int=_identiy, parse_float=_identiy, parse_constant=_identiy)
273
+ try:
274
+ qid = item["qid"]
275
+ item["wikipedia"] = re.sub(
276
+ entity_pattern,
277
+ r"\2",
278
+ item.pop("wikipedia_description")
279
+ )
280
+ if qid is None or qid == "":
281
+ item["qid"] = ""
282
+ yield id, item
283
+ except:
284
+ import sys
285
+ print(item, file=sys.stderr)
286
+ return