import csv from ast import literal_eval import datasets logger = datasets.logging.get_logger(__name__) _CITATION = """""" _DESCRIPTION = """""" _DOWNLOAD_URLS = { "train": "https://huggingface.co/datasets/mahdiyehebrahimi/nerutc/raw/main/nerutc_train.csv", "test": "https://huggingface.co/datasets/mahdiyehebrahimi/nerutc/raw/main/nerutc_test.csv", } class ParsTwiNERConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(ParsTwiNERConfig, self).__init__(**kwargs) class ParsTwiNER(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ ParsTwiNERConfig( name="nerutc", version=datasets.Version("1.1.1"), description=_DESCRIPTION, ), ] def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "tokens": datasets.Sequence(datasets.Value("string")), "ner_tags": datasets.Sequence( datasets.features.ClassLabel( names=[ "O", "B-UNI", "I-UNI", ] ) ), } ), homepage="https://huggingface.co/datasets/mahdiyehebrahimi/nerutc", citation=_CITATION, ) def _split_generators(self, dl_manager): """ Return SplitGenerators. """ train_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["train"]) test_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["test"]) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path} ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"filepath": test_path} ), ] def _generate_examples(self, filepath): logger.info("⏳ Generating examples from = %s", filepath) with open(filepath, encoding="utf-8") as csv_file: csv_reader = csv.reader(csv_file, quotechar='"', skipinitialspace=True) next(csv_reader, None) for id_, row in enumerate(csv_reader): tokens, ner_tags = row # Optional preprocessing here tokens = literal_eval(tokens) ner_tags = literal_eval(ner_tags) yield id_, {"tokens": tokens, "ner_tags": ner_tags}