import os import datasets logger = datasets.logging.get_logger(__name__) # TODO: Add BibTeX citation # Find for instance the citation on arxiv or on the dataset repo/website _CITATION = """\ @InProceedings{huggingface:dataset, title = {A great new dataset}, author={huggingface, Inc. }, year={2020} } """ # TODO: Add description of the dataset here # You can copy an official description _DESCRIPTION = """\ SemEval 2023 Task 2: MultiCoNER II Multilingual Complex Named Entity Recognition """ # TODO: Add a link to an official homepage for the dataset here _HOMEPAGE = "https://multiconer.github.io/" # TODO: Add the licence for the dataset here if you can find it _LICENSE = "" # TODO: Add link to the official dataset URLs here # 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 = "" class Multiconer2Config(datasets.BuilderConfig): """BuilderConfig for Multiconer2""" def __init__(self, **kwargs): """BuilderConfig for Multiconer2. Args: **kwargs: keyword arguments forwarded to super. """ super(Multiconer2Config, self).__init__(**kwargs) class Multiconer2(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.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 = [ Multiconer2Config(name="bn", version=VERSION), Multiconer2Config(name="de", version=VERSION), Multiconer2Config(name="en", version=VERSION), Multiconer2Config(name="es", version=VERSION), Multiconer2Config(name="fa", version=VERSION), Multiconer2Config(name="fr", version=VERSION), Multiconer2Config(name="hi", version=VERSION), Multiconer2Config(name="it", version=VERSION), Multiconer2Config(name="pt", version=VERSION), Multiconer2Config(name="sv", version=VERSION), Multiconer2Config(name="uk", version=VERSION), Multiconer2Config(name="zh", version=VERSION), ] DEFAULT_CONFIG_NAME = "en" micro_to_macro_ner_mapping = { 'O': "O", "B-AerospaceManufacturer": "B-Group", 'I-AerospaceManufacturer': 'I-Group', 'B-AnatomicalStructure': "B-Medical", 'I-AnatomicalStructure': "I-Medical", 'B-ArtWork': "B-CreativeWork", 'I-ArtWork': "I-CreativeWork", 'B-Artist': "B-Person", 'I-Artist': "I-Person", 'B-Athlete': "B-Person", 'I-Athlete': "I-Person", 'B-CarManufacturer': "B-Group", 'I-CarManufacturer': "I-Group", 'B-Cleric': "B-Person", 'I-Cleric': "I-Person", 'B-Clothing': "B-Product", 'I-Clothing': "I-Product", 'B-Disease': "B-Medical", 'I-Disease': "I-Medical", 'B-Drink': "B-Product", 'I-Drink': "I-Product", 'B-Facility': "B-Location", 'I-Facility': "I-Location", 'B-Food': "B-Product", 'I-Food': "I-Product", 'B-HumanSettlement': "B-Location", 'I-HumanSettlement': "I-Location", 'B-MedicalProcedure': "B-Medical", 'I-MedicalProcedure': "I-Medical", 'B-Medication/Vaccine': "B-Medical", 'I-Medication/Vaccine': "I-Medical", 'B-MusicalGRP': "B-Group", 'I-MusicalGRP': "I-Group", 'B-MusicalWork': "B-CreativeWork", 'I-MusicalWork': "I-CreativeWork", 'B-ORG': "B-Group", 'I-ORG': "I-Group", 'B-OtherLOC': "B-Location", 'I-OtherLOC': "I-Location", 'B-OtherPER': "B-Person", 'I-OtherPER': "I-Person", 'B-OtherPROD': "B-Product", 'I-OtherPROD': "I-Product", 'B-Politician': "B-Person", 'I-Politician': "I-Person", 'B-PrivateCorp': "B-Group", 'I-PrivateCorp': "I-Group", 'B-PublicCorp': "B-Group", 'I-PublicCorp': "I-Group", 'B-Scientist': "B-Person", 'I-Scientist': "I-Person", 'B-Software': "B-CreativeWork", 'I-Software': "I-CreativeWork", 'B-SportsGRP': "B-Group", 'I-SportsGRP': "I-Group", 'B-SportsManager': "B-Person", 'I-SportsManager': "I-Person", 'B-Station': 'B-Location', 'I-Station': 'I-Location', 'B-Symptom': "B-Medical", 'I-Symptom': "I-Medical", 'B-Vehicle': "B-Product", 'I-Vehicle': "I-Product", 'B-VisualWork': "B-CreativeWork", 'I-VisualWork': "I-CreativeWork", 'B-WrittenWork': "B-CreativeWork", 'I-WrittenWork': "I-CreativeWork", } def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "id": datasets.Value("string"), "tokens": datasets.Sequence(datasets.Value("string")), "ner_tags": datasets.Sequence( datasets.features.ClassLabel( names=['O', "B-AerospaceManufacturer", 'I-AerospaceManufacturer', 'B-AnatomicalStructure', 'I-AnatomicalStructure', 'B-ArtWork', 'I-ArtWork', 'B-Artist', 'I-Artist', 'B-Athlete', 'I-Athlete', 'B-CarManufacturer', 'I-CarManufacturer', 'B-Cleric', 'I-Cleric', 'B-Clothing', 'I-Clothing', 'B-Disease', 'I-Disease', 'B-Drink', 'I-Drink', 'B-Facility', 'I-Facility', 'B-Food', 'I-Food', 'B-HumanSettlement', 'I-HumanSettlement', 'B-MedicalProcedure', 'I-MedicalProcedure', 'B-Medication/Vaccine', 'I-Medication/Vaccine', 'B-MusicalGRP', 'I-MusicalGRP', 'B-MusicalWork', 'I-MusicalWork', 'B-ORG', 'I-ORG', 'B-OtherLOC', 'I-OtherLOC', 'B-OtherPER', 'I-OtherPER', 'B-OtherPROD', 'I-OtherPROD', 'B-Politician', 'I-Politician', 'B-PrivateCorp', 'I-PrivateCorp', 'B-PublicCorp', 'I-PublicCorp', 'B-Scientist', 'I-Scientist', 'B-Software', 'I-Software', 'B-SportsGRP', 'I-SportsGRP', 'B-SportsManager', 'I-SportsManager', 'B-Station', 'I-Station', 'B-Symptom', 'I-Symptom', 'B-Vehicle', 'I-Vehicle', 'B-VisualWork', 'I-VisualWork', 'B-WrittenWork', 'I-WrittenWork'] ) ), "ner_macro_tags": datasets.Sequence( datasets.features.ClassLabel( names=['O', "B-Location", "I-Location", "B-CreativeWork", "I-CreativeWork", "B-Group", "I-Group", "B-Person", "I-Person", "B-Product", "I-Product", "B-Medical", "I-Medical", ] ) ), } ), supervised_keys=None, homepage="https://www.aclweb.org/anthology/W03-0419/", citation=_CITATION, ) def _split_generators(self, dl_manager: datasets.DownloadManager): """Returns SplitGenerators.""" downloaded_files = dl_manager.download_and_extract({ "train": f"{self.config.name}-train.conll", "dev": f"{self.config.name}-dev.conll", }) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}), datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}), ] def _generate_examples(self, filepath): logger.info("⏳ Generating examples from = %s", filepath) with open(filepath, encoding="utf-8") as f: guid = 0 tokens = [] ner_tags = [] ner_macro_tags = [] for line in f: if line.startswith("#") or line == "" or line == "\n": if tokens: yield guid, { "id": str(guid), "tokens": tokens, "ner_tags": ner_tags, "ner_macro_tags": ner_macro_tags, } guid += 1 tokens = [] ner_tags = [] ner_macro_tags = [] else: # conll2003 tokens are space separated splits = line.split(" _ _ ") tokens.append(splits[0]) ner_tags.append(splits[1].rstrip()) ner_macro_tags.append(self.micro_to_macro_ner_mapping[splits[1].rstrip()]) # last example if tokens: yield guid, { "id": str(guid), "tokens": tokens, "ner_tags": ner_tags, "ner_macro_tags": ner_macro_tags, }