|
|
|
|
|
|
| import datasets
|
| from pathlib import Path
|
| import logging
|
|
|
| _CITATION = """\
|
| @mastersthesis{meraner2019grasping,
|
| title={Grasping the Nettle: Neural Entity Recognition for Scientific and Vernacular Plant Names},
|
| author={Meraner, Isabel},
|
| year={2019},
|
| school={Institute of Computational Linguistics, University of Zurich},
|
| note={Available at: https://github.com/IsabelMeraner/BotanicalNER}
|
| }
|
| """
|
|
|
| _DESCRIPTION = """\
|
| BotanicalNER is a Named Entity Recognition dataset for scientific and vernacular plant names in German and English.
|
| The dataset was created for a master thesis project at the University of Zurich focusing on identifying and
|
| disambiguating plant names across multiple text genres to extract and preserve (ethno-)botanical knowledge.
|
| """
|
|
|
| _HOMEPAGE = "https://github.com/IsabelMeraner/BotanicalNER"
|
| _LICENSE = "GPL-3.0"
|
| _URL = "https://github.com/IsabelMeraner/BotanicalNER/archive/refs/heads/master.zip"
|
|
|
| _NER_TAGS = ["O", "B-Scientific", "I-Scientific", "B-Vernacular", "I-Vernacular"]
|
|
|
|
|
| _FILE_PATHS = {
|
| "de": {
|
| "train": [
|
| "RESOURCES/corpora/training corpora/de/plantblog_corpus_de.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/de/wiki_abstractcorpus_de.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/de/TextBerg_subcorpus_de.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/de/botlit_corpus_de.tok.pos.iob.txt",
|
| ],
|
| "test": ["RESOURCES/corpora/gold_standard/de/combined.test.fold1GOLD_de.txt"],
|
| "fungi": ["RESOURCES/corpora/gold_standard/de/test_fungi_de.tok.pos.iobGOLD.txt"],
|
| },
|
| "en": {
|
| "train": [
|
| "RESOURCES/corpora/training corpora/en/plantblog_corpus_en.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/en/wiki_abstractcorpus_en.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/en/TextBerg_subcorpus_en.tok.pos.iob.txt",
|
| "RESOURCES/corpora/training corpora/en/botlit_corpus_en.tok.pos.iob.txt",
|
| ],
|
| "test": ["RESOURCES/corpora/gold_standard/en/combined.test.fold1GOLD_en.txt"],
|
| "fungi": ["RESOURCES/corpora/gold_standard/en/test_fungi_en.tok.pos.iobGOLD.txt"],
|
| },
|
| }
|
|
|
| class BotanicalNERConfig(datasets.BuilderConfig):
|
| """BuilderConfig for BotanicalNER"""
|
| def __init__(self, language="de", **kwargs):
|
| super(BotanicalNERConfig, self).__init__(**kwargs)
|
| self.language = language
|
|
|
| class BotanicalNER(datasets.GeneratorBasedBuilder):
|
| """BotanicalNER dataset for plant name NER in German and English"""
|
|
|
| VERSION = datasets.Version("1.0.0")
|
|
|
| BUILDER_CONFIGS = [
|
| BotanicalNERConfig(name="de", language="de", version=VERSION, description="German BotanicalNER dataset"),
|
| BotanicalNERConfig(name="en", language="en", version=VERSION, description="English BotanicalNER dataset"),
|
| ]
|
|
|
| DEFAULT_CONFIG_NAME = "de"
|
|
|
| def _info(self):
|
| features = datasets.Features({
|
| "id": datasets.Value("string"),
|
| "tokens": datasets.Sequence(datasets.Value("string")),
|
| "pos_tags": datasets.Sequence(datasets.Value("string")),
|
| "ner_tags": datasets.Sequence(datasets.ClassLabel(names=_NER_TAGS)),
|
| })
|
| return datasets.DatasetInfo(
|
| description=_DESCRIPTION,
|
| features=features,
|
| supervised_keys=None,
|
| homepage=_HOMEPAGE,
|
| license=_LICENSE,
|
| citation=_CITATION,
|
| )
|
|
|
| def _split_generators(self, dl_manager):
|
| """Returns SplitGenerators."""
|
|
|
| data_dir = dl_manager.download_and_extract(_URL)
|
| base_path = Path(data_dir) / "BotanicalNER-master"
|
| language = self.config.language
|
|
|
| return [
|
| datasets.SplitGenerator(
|
| name=datasets.Split.TRAIN,
|
| gen_kwargs={"filepaths": [base_path / f for f in _FILE_PATHS[language]["train"]]},
|
| ),
|
| datasets.SplitGenerator(
|
| name=datasets.Split.TEST,
|
| gen_kwargs={"filepaths": [base_path / f for f in _FILE_PATHS[language]["test"]]},
|
| ),
|
| datasets.SplitGenerator(
|
| name="fungi",
|
| gen_kwargs={"filepaths": [base_path / f for f in _FILE_PATHS[language]["fungi"]]},
|
| ),
|
| ]
|
|
|
| def _generate_examples(self, filepaths):
|
| """Yields examples from the dataset files."""
|
| guid = 0
|
| for filepath in filepaths:
|
| logging.info(f"Generating examples from {filepath}")
|
| with open(filepath, encoding="utf-8") as f:
|
| tokens = []
|
| pos_tags = []
|
| ner_tags = []
|
| for line in f:
|
| line = line.strip()
|
| if not line or line.startswith("-DOCSTART-"):
|
| if tokens:
|
| yield guid, {
|
| "id": str(guid),
|
| "tokens": tokens,
|
| "pos_tags": pos_tags,
|
| "ner_tags": ner_tags,
|
| }
|
| guid += 1
|
| tokens = []
|
| pos_tags = []
|
| ner_tags = []
|
| else:
|
| parts = line.split("\t")
|
|
|
| if len(parts) == 3:
|
| tokens.append(parts[0])
|
| pos_tags.append(parts[1])
|
| ner_tags.append(parts[2])
|
| else:
|
| logging.warning(f"Skipping malformed line in {filepath}: '{line}'")
|
|
|
|
|
| if tokens:
|
| yield guid, {
|
| "id": str(guid),
|
| "tokens": tokens,
|
| "pos_tags": pos_tags,
|
| "ner_tags": ner_tags,
|
| }
|
| guid += 1 |