import csv import sys import datasets import pandas as pd 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/" _PARAGRAPHS_URL = "https://huggingface.co/datasets/GroNLP/ik-nlp-22_slp/raw/main/slp3ed.csv" _QUESTIONS_URL = "https://huggingface.co/datasets/GroNLP/ik-nlp-22_slp/raw/main/slp_questions.csv" class IkNlp22SlpConfig(datasets.BuilderConfig): """BuilderConfig for IK NLP '22 Speech and Language Processing.""" def __init__( self, features, **kwargs, ): """ Args: features: `list[string]`, list of the features that will appear in the feature dict. Should not include "label". **kwargs: keyword arguments forwarded to super. """ super().__init__(version=datasets.Version("1.0.0"), **kwargs) 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"], ), IkNlp22SlpConfig( name="questions", features=["chapter", "section", "subsection", "question", "paragraph", "answer"], ), ] 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.""" if self.config.name == "paragraphs": paragraphs_file = dl_manager.download_and_extract(_PARAGRAPHS_URL) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "filepath": paragraphs_file, "features": self.config.features, }, ), ] else: pairs_file = dl_manager.download_and_extract(_QUESTIONS_URL) return [ datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={ "filepath": pairs_file, "features": self.config.features, }, ), ] def _generate_examples(self, filepath: str, features: List[str]): """Yields examples as (key, example) tuples.""" data = pd.read_csv(filepath) for id_, row in data.iterrows(): yield id_, row.to_dict()