Datasets:

Modalities:
Text
Formats:
parquet
ArXiv:
Libraries:
Datasets
pandas
License:
lhoestq HF staff commited on
Commit
f21bf98
1 Parent(s): 3cbb74c

Delete loading script

Browse files
Files changed (1) hide show
  1. wikiann.py +0 -394
wikiann.py DELETED
@@ -1,394 +0,0 @@
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
-
16
- """The WikiANN dataset for multilingual named entity recognition"""
17
-
18
-
19
- import os
20
-
21
- import datasets
22
-
23
-
24
- _CITATION = """@inproceedings{pan-etal-2017-cross,
25
- title = "Cross-lingual Name Tagging and Linking for 282 Languages",
26
- author = "Pan, Xiaoman and
27
- Zhang, Boliang and
28
- May, Jonathan and
29
- Nothman, Joel and
30
- Knight, Kevin and
31
- Ji, Heng",
32
- booktitle = "Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
33
- month = jul,
34
- year = "2017",
35
- address = "Vancouver, Canada",
36
- publisher = "Association for Computational Linguistics",
37
- url = "https://www.aclweb.org/anthology/P17-1178",
38
- doi = "10.18653/v1/P17-1178",
39
- pages = "1946--1958",
40
- abstract = "The ambitious goal of this work is to develop a cross-lingual name tagging and linking framework for 282 languages that exist in Wikipedia. Given a document in any of these languages, our framework is able to identify name mentions, assign a coarse-grained or fine-grained type to each mention, and link it to an English Knowledge Base (KB) if it is linkable. We achieve this goal by performing a series of new KB mining methods: generating {``}silver-standard{''} annotations by transferring annotations from English to other languages through cross-lingual links and KB properties, refining annotations through self-training and topic selection, deriving language-specific morphology features from anchor links, and mining word translation pairs from cross-lingual links. Both name tagging and linking results for 282 languages are promising on Wikipedia data and on-Wikipedia data.",
41
- }"""
42
-
43
- _DESCRIPTION = """WikiANN (sometimes called PAN-X) is a multilingual named entity recognition dataset consisting of Wikipedia articles annotated with LOC (location), PER (person), and ORG (organisation) tags in the IOB2 format. This version corresponds to the balanced train, dev, and test splits of Rahimi et al. (2019), which supports 176 of the 282 languages from the original WikiANN corpus."""
44
-
45
- _DATA_URL = "https://s3.amazonaws.com/datasets.huggingface.co/wikiann/1.1.0/panx_dataset.zip"
46
- _HOMEPAGE = "https://github.com/afshinrahimi/mmner"
47
- _VERSION = "1.1.0"
48
- _LANGS = [
49
- "ace",
50
- "af",
51
- "als",
52
- "am",
53
- "an",
54
- "ang",
55
- "ar",
56
- "arc",
57
- "arz",
58
- "as",
59
- "ast",
60
- "ay",
61
- "az",
62
- "ba",
63
- "bar",
64
- "bat-smg",
65
- "be",
66
- "be-x-old",
67
- "bg",
68
- "bh",
69
- "bn",
70
- "bo",
71
- "br",
72
- "bs",
73
- "ca",
74
- "cbk-zam",
75
- "cdo",
76
- "ce",
77
- "ceb",
78
- "ckb",
79
- "co",
80
- "crh",
81
- "cs",
82
- "csb",
83
- "cv",
84
- "cy",
85
- "da",
86
- "de",
87
- "diq",
88
- "dv",
89
- "el",
90
- "eml",
91
- "en",
92
- "eo",
93
- "es",
94
- "et",
95
- "eu",
96
- "ext",
97
- "fa",
98
- "fi",
99
- "fiu-vro",
100
- "fo",
101
- "fr",
102
- "frr",
103
- "fur",
104
- "fy",
105
- "ga",
106
- "gan",
107
- "gd",
108
- "gl",
109
- "gn",
110
- "gu",
111
- "hak",
112
- "he",
113
- "hi",
114
- "hr",
115
- "hsb",
116
- "hu",
117
- "hy",
118
- "ia",
119
- "id",
120
- "ig",
121
- "ilo",
122
- "io",
123
- "is",
124
- "it",
125
- "ja",
126
- "jbo",
127
- "jv",
128
- "ka",
129
- "kk",
130
- "km",
131
- "kn",
132
- "ko",
133
- "ksh",
134
- "ku",
135
- "ky",
136
- "la",
137
- "lb",
138
- "li",
139
- "lij",
140
- "lmo",
141
- "ln",
142
- "lt",
143
- "lv",
144
- "map-bms",
145
- "mg",
146
- "mhr",
147
- "mi",
148
- "min",
149
- "mk",
150
- "ml",
151
- "mn",
152
- "mr",
153
- "ms",
154
- "mt",
155
- "mwl",
156
- "my",
157
- "mzn",
158
- "nap",
159
- "nds",
160
- "ne",
161
- "nl",
162
- "nn",
163
- "no",
164
- "nov",
165
- "oc",
166
- "or",
167
- "os",
168
- "pa",
169
- "pdc",
170
- "pl",
171
- "pms",
172
- "pnb",
173
- "ps",
174
- "pt",
175
- "qu",
176
- "rm",
177
- "ro",
178
- "ru",
179
- "rw",
180
- "sa",
181
- "sah",
182
- "scn",
183
- "sco",
184
- "sd",
185
- "sh",
186
- "si",
187
- "simple",
188
- "sk",
189
- "sl",
190
- "so",
191
- "sq",
192
- "sr",
193
- "su",
194
- "sv",
195
- "sw",
196
- "szl",
197
- "ta",
198
- "te",
199
- "tg",
200
- "th",
201
- "tk",
202
- "tl",
203
- "tr",
204
- "tt",
205
- "ug",
206
- "uk",
207
- "ur",
208
- "uz",
209
- "vec",
210
- "vep",
211
- "vi",
212
- "vls",
213
- "vo",
214
- "wa",
215
- "war",
216
- "wuu",
217
- "xmf",
218
- "yi",
219
- "yo",
220
- "zea",
221
- "zh",
222
- "zh-classical",
223
- "zh-min-nan",
224
- "zh-yue",
225
- ]
226
-
227
-
228
- class WikiannConfig(datasets.BuilderConfig):
229
- def __init__(self, **kwargs):
230
- super(WikiannConfig, self).__init__(version=datasets.Version(_VERSION, ""), **kwargs)
231
-
232
-
233
- class Wikiann(datasets.GeneratorBasedBuilder):
234
- """WikiANN is a multilingual named entity recognition dataset consisting of Wikipedia articles annotated with LOC, PER, and ORG tags"""
235
-
236
- VERSION = datasets.Version(_VERSION)
237
- # use two-letter ISO 639-1 language codes as the name for each corpus
238
- BUILDER_CONFIGS = [
239
- WikiannConfig(name=lang, description=f"WikiANN NER examples in language {lang}") for lang in _LANGS
240
- ]
241
-
242
- def _tags_to_spans(self, tags):
243
- """Convert tags to spans."""
244
- spans = set()
245
- span_start = 0
246
- span_end = 0
247
- active_conll_tag = None
248
- for index, string_tag in enumerate(tags):
249
- # Actual BIO tag.
250
- bio_tag = string_tag[0]
251
- assert bio_tag in ["B", "I", "O"], "Invalid Tag"
252
- conll_tag = string_tag[2:]
253
- if bio_tag == "O":
254
- # The span has ended.
255
- if active_conll_tag:
256
- spans.add((active_conll_tag, (span_start, span_end)))
257
- active_conll_tag = None
258
- # We don't care about tags we are
259
- # told to ignore, so we do nothing.
260
- continue
261
- elif bio_tag == "B":
262
- # We are entering a new span; reset indices and active tag to new span.
263
- if active_conll_tag:
264
- spans.add((active_conll_tag, (span_start, span_end)))
265
- active_conll_tag = conll_tag
266
- span_start = index
267
- span_end = index
268
- elif bio_tag == "I" and conll_tag == active_conll_tag:
269
- # We're inside a span.
270
- span_end += 1
271
- else:
272
- # This is the case the bio label is an "I", but either:
273
- # 1) the span hasn't started - i.e. an ill formed span.
274
- # 2) We have IOB1 tagging scheme.
275
- # We'll process the previous span if it exists, but also include this
276
- # span. This is important, because otherwise, a model may get a perfect
277
- # F1 score whilst still including false positive ill-formed spans.
278
- if active_conll_tag:
279
- spans.add((active_conll_tag, (span_start, span_end)))
280
- active_conll_tag = conll_tag
281
- span_start = index
282
- span_end = index
283
- # Last token might have been a part of a valid span.
284
- if active_conll_tag:
285
- spans.add((active_conll_tag, (span_start, span_end)))
286
- # Return sorted list of spans
287
- return sorted(list(spans), key=lambda x: x[1][0])
288
-
289
- def _get_spans(self, tokens, tags):
290
- """Convert tags to textspans."""
291
- spans = self._tags_to_spans(tags)
292
- text_spans = [x[0] + ": " + " ".join([tokens[i] for i in range(x[1][0], x[1][1] + 1)]) for x in spans]
293
- if not text_spans:
294
- text_spans = ["None"]
295
- return text_spans
296
-
297
- def _info(self):
298
- features = datasets.Features(
299
- {
300
- "tokens": datasets.Sequence(datasets.Value("string")),
301
- "ner_tags": datasets.Sequence(
302
- datasets.features.ClassLabel(
303
- names=[
304
- "O",
305
- "B-PER",
306
- "I-PER",
307
- "B-ORG",
308
- "I-ORG",
309
- "B-LOC",
310
- "I-LOC",
311
- ]
312
- )
313
- ),
314
- "langs": datasets.Sequence(datasets.Value("string")),
315
- "spans": datasets.Sequence(datasets.Value("string")),
316
- }
317
- )
318
- return datasets.DatasetInfo(
319
- description=_DESCRIPTION,
320
- features=features,
321
- supervised_keys=None,
322
- homepage=_HOMEPAGE,
323
- citation=_CITATION,
324
- )
325
-
326
- def _split_generators(self, dl_manager):
327
- wikiann_dl_dir = dl_manager.download_and_extract(_DATA_URL)
328
- lang = self.config.name
329
- lang_archive = os.path.join(wikiann_dl_dir, lang + ".tar.gz")
330
-
331
- return [
332
- datasets.SplitGenerator(
333
- name=datasets.Split.VALIDATION,
334
- gen_kwargs={"filepath": "dev", "files": dl_manager.iter_archive(lang_archive)},
335
- ),
336
- datasets.SplitGenerator(
337
- name=datasets.Split.TEST,
338
- gen_kwargs={"filepath": "test", "files": dl_manager.iter_archive(lang_archive)},
339
- ),
340
- datasets.SplitGenerator(
341
- name=datasets.Split.TRAIN,
342
- gen_kwargs={"filepath": "train", "files": dl_manager.iter_archive(lang_archive)},
343
- ),
344
- ]
345
-
346
- def _generate_examples(self, filepath, files):
347
- """Reads line by line format of the NER dataset and generates examples.
348
- Input Format:
349
- en:rick B-PER
350
- en:and O
351
- en:morty B-PER
352
- en:are O
353
- en:cool O
354
- en:. O
355
- Output Format:
356
- {
357
- 'tokens': ["rick", "and", "morty", "are", "cool", "."],
358
- 'ner_tags': ["B-PER", "O" , "B-PER", "O", "O", "O"],
359
- 'langs': ["en", "en", "en", "en", "en", "en"]
360
- 'spans': ["PER: rick", "PER: morty"]
361
- }
362
- Args:
363
- filepath: Path to file with line by line NER format.
364
- Returns:
365
- Examples with the format listed above.
366
- """
367
- guid_index = 1
368
- for path, f in files:
369
- if path == filepath:
370
- tokens = []
371
- ner_tags = []
372
- langs = []
373
- for line in f:
374
- line = line.decode("utf-8")
375
- if line == "" or line == "\n":
376
- if tokens:
377
- spans = self._get_spans(tokens, ner_tags)
378
- yield guid_index, {"tokens": tokens, "ner_tags": ner_tags, "langs": langs, "spans": spans}
379
- guid_index += 1
380
- tokens = []
381
- ner_tags = []
382
- langs = []
383
- else:
384
- # wikiann data is tab separated
385
- splits = line.split("\t")
386
- # strip out en: prefix
387
- langs.append(splits[0].split(":")[0])
388
- tokens.append(":".join(splits[0].split(":")[1:]))
389
- if len(splits) > 1:
390
- ner_tags.append(splits[-1].replace("\n", ""))
391
- else:
392
- # examples have no label in test set
393
- ner_tags.append("O")
394
- break