import csv import sys import datasets from typing import List csv.field_size_limit(sys.maxsize) _CITATION = """\ @book{slp3ed-iknlp2022, author = {Jurafsky, Daniel and Martin, James}, year = {2021}, month = {12}, pages = {1--235, 1--19}, title = {Speech and Language Processing: An Introduction to Natural Language Processing, Computational Linguistics, and Speech Recognition}, volume = {3} } """ _DESCRIPTION = """\ Paragraphs from the Speech and Language Processing book (3ed) by Jurafsky and Martin extracted semi-automatically from Chapters 2 to 11 of the original book draft. """ _HOMEPAGE = "https://www.rug.nl/masters/information-science/?lang=en" _LICENSE = "See https://web.stanford.edu/~jurafsky/slp3/" class IkNlp22SlpConfig(datasets.BuilderConfig): """BuilderConfig for ItaCoLA.""" def __init__( self, features, data_url, **kwargs, ): """ Args: features: `list[string]`, list of the features that will appear in the feature dict. Should not include "label". data_url: `string`, url to download the zip file from. **kwargs: keyword arguments forwarded to super. """ super().__init__(version=datasets.Version("1.0.0"), **kwargs) self.data_url = data_url self.features = features class IkNlp22Slp(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ IkNlp22SlpConfig( name="paragraphs", features=["n_chapter", "chapter", "n_section", "section", "n_subsection", "subsection", "text"], data_url="https://huggingface.co/datasets/GroNLP/ik-nlp-22_slp/resolve/main/slp3ed.tsv" ), ] DEFAULT_CONFIG_NAME = "paragraphs" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features({feature: datasets.Value("string") for feature in self.config.features}), homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" data_file = dl_manager.download_and_extract(self.config.data_url) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "filepath": data_file, "split": "train", "features": self.config.features, }, ), ] def _generate_examples(self, filepath: str, split: str, features: List[str]): """Yields examples as (key, example) tuples.""" with open(filepath, encoding="utf8") as f: for id_, row in enumerate(f): if id_ == 0: continue fields = row.strip().split("\t") yield id_, { k:v.strip() for k,v in zip(features, fields) }