Datasets:
Tasks:
Token Classification
Modalities:
Text
Sub-tasks:
named-entity-recognition
Languages:
English
Size:
10K - 100K
ArXiv:
| # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """CrossNER is a cross-domain dataset for named entity recognition""" | |
| import json | |
| import os | |
| import datasets | |
| _CITATION = """\ | |
| @article{liu2020crossner, | |
| title={CrossNER: Evaluating Cross-Domain Named Entity Recognition}, | |
| author={Zihan Liu and Yan Xu and Tiezheng Yu and Wenliang Dai and Ziwei Ji and Samuel Cahyawijaya and Andrea Madotto and Pascale Fung}, | |
| year={2020}, | |
| eprint={2012.04373}, | |
| archivePrefix={arXiv}, | |
| primaryClass={cs.CL} | |
| } | |
| """ | |
| _DESCRIPTION = """\ | |
| CrossNER is a fully-labeled collected of named entity recognition (NER) data spanning over five diverse domains | |
| (Politics, Natural Science, Music, Literature, and Artificial Intelligence) with specialized entity categories for | |
| different domains. Additionally, CrossNER also includes unlabeled domain-related corpora for the corresponding five | |
| domains. | |
| For details, see the paper: | |
| [CrossNER: Evaluating Cross-Domain Named Entity Recognition](https://arxiv.org/abs/2012.04373) | |
| """ | |
| _HOMEPAGE = "https://github.com/zliucr/CrossNER" | |
| # TODO: Add the licence for the dataset here if you can find it | |
| _LICENSE = "" | |
| # The HuggingFace Datasets library doesn't host the datasets but only points to the original files. | |
| # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method) | |
| _URLS = { | |
| "conll2003": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/conll2003/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/conll2003/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/conll2003/test.txt", | |
| }, | |
| "politics": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/politics/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/politics/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/politics/test.txt", | |
| }, | |
| "science": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/science/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/science/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/science/test.txt", | |
| }, | |
| "music": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/music/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/music/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/music/test.txt", | |
| }, | |
| "literature": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/literature/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/literature/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/literature/test.txt", | |
| }, | |
| "ai": { | |
| "train": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/ai/train.txt", | |
| "validation": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/ai/dev.txt", | |
| "test": "https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data/ai/test.txt", | |
| }, | |
| } | |
| _CLASS_LABELS = [ | |
| "O", | |
| "B-academicjournal", "I-academicjournal", | |
| "B-album", "I-album", | |
| "B-algorithm", "I-algorithm", | |
| "B-astronomicalobject", "I-astronomicalobject", | |
| "B-award", "I-award", | |
| "B-band", "I-band", | |
| "B-book", "I-book", | |
| "B-chemicalcompound", "I-chemicalcompound", | |
| "B-chemicalelement", "I-chemicalelement", | |
| "B-conference", "I-conference", | |
| "B-country", "I-country", | |
| "B-discipline", "I-discipline", | |
| "B-election", "I-election", | |
| "B-enzyme", "I-enzyme", | |
| "B-event", "I-event", | |
| "B-field", "I-field", | |
| "B-literarygenre", "I-literarygenre", | |
| "B-location", "I-location", | |
| "B-magazine", "I-magazine", | |
| "B-metrics", "I-metrics", | |
| "B-misc", "I-misc", | |
| "B-musicalartist", "I-musicalartist", | |
| "B-musicalinstrument", "I-musicalinstrument", | |
| "B-musicgenre", "I-musicgenre", | |
| "B-organisation", "I-organisation", | |
| "B-person", "I-person", | |
| "B-poem", "I-poem", | |
| "B-politicalparty", "I-politicalparty", | |
| "B-politician", "I-politician", | |
| "B-product", "I-product", | |
| "B-programlang", "I-programlang", | |
| "B-protein", "I-protein", | |
| "B-researcher", "I-researcher", | |
| "B-scientist", "I-scientist", | |
| "B-song", "I-song", | |
| "B-task", "I-task", | |
| "B-theory", "I-theory", | |
| "B-university", "I-university", | |
| "B-writer", "I-writer", | |
| ] | |
| class CrossNER(datasets.GeneratorBasedBuilder): | |
| """CrossNER is a cross-domain dataset for named entity recognition""" | |
| VERSION = datasets.Version("1.1.0") | |
| # This is an example of a dataset with multiple configurations. | |
| # If you don't want/need to define several sub-sets in your dataset, | |
| # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes. | |
| # If you need to make complex sub-parts in the datasets with configurable options | |
| # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig | |
| # BUILDER_CONFIG_CLASS = MyBuilderConfig | |
| # You will be able to load one or the other configurations in the following list with | |
| # data = datasets.load_dataset('my_dataset', 'first_domain') | |
| # data = datasets.load_dataset('my_dataset', 'second_domain') | |
| BUILDER_CONFIGS = [ | |
| datasets.BuilderConfig(name="conll2003", version=VERSION, | |
| description="This part of CrossNER covers data from the news domain"), | |
| datasets.BuilderConfig(name="politics", version=VERSION, | |
| description="This part of CrossNER covers data from the politics domain"), | |
| datasets.BuilderConfig(name="science", version=VERSION, | |
| description="This part of CrossNER covers data from the science domain"), | |
| datasets.BuilderConfig(name="music", version=VERSION, | |
| description="This part of CrossNER covers data from the music domain"), | |
| datasets.BuilderConfig(name="literature", version=VERSION, | |
| description="This part of CrossNER covers data from the literature domain"), | |
| datasets.BuilderConfig(name="ai", version=VERSION, | |
| description="This part of CrossNER covers data from the AI domain"), | |
| ] | |
| def _info(self): | |
| features = datasets.Features( | |
| { | |
| "id": datasets.Value("string"), | |
| "tokens": datasets.Sequence(datasets.Value("string")), | |
| "ner_tags": datasets.Sequence(datasets.features.ClassLabel(names=_CLASS_LABELS)), | |
| } | |
| ) | |
| return datasets.DatasetInfo( | |
| # This is the description that will appear on the datasets page. | |
| description=_DESCRIPTION, | |
| # This defines the different columns of the dataset and their types | |
| features=features, # Here we define them above because they are different between the two configurations | |
| # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and | |
| # specify them. They'll be used if as_supervised=True in builder.as_dataset. | |
| # supervised_keys=("sentence", "label"), | |
| # Homepage of the dataset for documentation | |
| homepage=_HOMEPAGE, | |
| # License for the dataset if available | |
| license=_LICENSE, | |
| # Citation for the dataset | |
| citation=_CITATION, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name | |
| # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS | |
| # 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. | |
| # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive | |
| urls = _URLS[self.config.name] | |
| downloaded_files = dl_manager.download_and_extract(urls) | |
| return [datasets.SplitGenerator(name=i, gen_kwargs={"filepath": downloaded_files[str(i)]}) | |
| for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]] | |
| # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` | |
| def _generate_examples(self, filepath): | |
| # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example. | |
| with open(filepath, encoding="utf-8") as f: | |
| guid = 0 | |
| tokens = [] | |
| ner_tags = [] | |
| for line in f: | |
| if line == "" or line == "\n": | |
| if tokens: | |
| yield guid, { | |
| "id": str(guid), | |
| "tokens": tokens, | |
| "ner_tags": ner_tags, | |
| } | |
| guid += 1 | |
| tokens = [] | |
| ner_tags = [] | |
| else: | |
| splits = line.split("\t") | |
| tokens.append(splits[0]) | |
| ner_tags.append(splits[1].rstrip()) | |
| # last example | |
| if tokens: | |
| yield guid, { | |
| "id": str(guid), | |
| "tokens": tokens, | |
| "ner_tags": ner_tags, | |
| } | |