File size: 3,457 Bytes
04e1f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import os

import datasets

_CITATION = """
@misc{RecipeNLGLite, 
  author          = {Mehrdad Farahani},
  title           = {RecipeNLG: A Cooking Recipes Dataset for Semi-Structured Text Generation (Lite)},
  year            = 2021,
  publisher       = {GitHub},
  journal         = {GitHub repository},
  howpublished    = {url{https://github.com/m3hrdadfi/recipe-nlg-lite}},
} 
"""

_DESCRIPTION = """
RecipeNLG: A Cooking Recipes Dataset for Semi-Structured Text Generation - Lite version
The dataset we publish contains 7,198 cooking recipes (>7K). 
It's processed in more careful way and provides more samples than any other dataset in the area."""

_HOMEPAGE = "https://github.com/m3hrdadfi/recipe-nlg-lite"
_LICENSE = "MIT License"

_URLs = {
    "1.0.0": {
        "data": "https://drive.google.com/uc?id=1PGH5H_oW7wUvMw_5xaXvbEN7DFll-wDX",
        "features": [
            {"name": "uid", "type": datasets.Value("string")},
            {"name": "name", "type": datasets.Value("string")},
            {"name": "description", "type": datasets.Value("string")},
            {"name": "link", "type": datasets.Value("string")},
            {"name": "ner", "type": datasets.Value("string")},
            {"name": "ingredients", "type": datasets.Value("string")},
            {"name": "steps", "type": datasets.Value("string")},
        ],
    }
}


class RecipeNLGLiteConfig(datasets.BuilderConfig):
    """BuilderConfig for RecipeNLGLite."""

    def __init__(self, **kwargs):
        """BuilderConfig for RecipeNLGLite.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(RecipeNLGLiteConfig, self).__init__(**kwargs)


class RecipeNLGLite(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        RecipeNLGLiteConfig(
            name="1.0.0", version=datasets.Version("1.0.0"), description="The first version of recipe_nlg_lite"
        ),
    ]

    def _info(self):
        feature_names_types = _URLs[self.config.name]["features"]
        features = datasets.Features({f["name"]: f["type"] for f in feature_names_types})

        return datasets.DatasetInfo(
            description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, citation=_CITATION
        )

    def _split_generators(self, dl_manager):
        my_urls = _URLs[self.config.name]
        data_dir = dl_manager.download_and_extract(my_urls["data"])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "recipe_nlg_lite", "train.csv"),
                    "split": "train",
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepath": os.path.join(data_dir, "recipe_nlg_lite", "test.csv"),
                    "split": "test",
                },
            ),
        ]

    def _generate_examples(self, filepath, split):
        feature_names_types = _URLs[self.config.name]["features"]
        features = [f["name"] for f in feature_names_types]
        with open(filepath, encoding="utf-8") as csv_file:
            reader = csv.DictReader(csv_file, quotechar='"', delimiter="\t", quoting=csv.QUOTE_MINIMAL)

            for _id, row in enumerate(reader):
                if len(row) == len(features):
                    yield _id, {f: row[f] for f in features}