File size: 3,454 Bytes
5e078e2
 
 
 
3c4d251
5e078e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c4d251
dc27a26
f2b5438
 
5e078e2
 
5f92a8a
5e078e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2b5438
 
 
 
5e078e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2b5438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e078e2
f2b5438
5e078e2
3c4d251
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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()