File size: 4,752 Bytes
820660d
 
 
 
 
 
 
 
 
 
 
 
 
946738b
820660d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c33dc8
 
 
 
 
820660d
 
946738b
673a510
 
820660d
 
673a510
820660d
 
 
1962b7c
820660d
 
673a510
 
820660d
 
673a510
098daae
946738b
673a510
820660d
 
 
 
 
 
 
 
 
 
 
 
 
2fd0c64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820660d
 
 
946738b
 
 
 
 
 
 
 
 
 
 
820660d
 
 
 
 
 
 
 
 
 
 
 
39c7f3b
820660d
946738b
 
39c7f3b
946738b
820660d
 
 
 
 
 
946738b
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""New York Times Ingredient Phrase Tagger Dataset"""


import datasets


logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@misc{nytimesTaggedIngredients,
	author = {Erica Greene and Adam Mckaig},
	title = {{O}ur {T}agged {I}ngredients {D}ata is {N}ow on {G}it{H}ub --- archive.nytimes.com},
	howpublished = {\\url{https://archive.nytimes.com/open.blogs.nytimes.com/2016/04/27/structured-ingredients-data-tagging/}},
	year = {},
	note = {[Accessed 03-10-2023]},
}
"""


_DESCRIPTION = """\
New York Times Ingredient Phrase Tagger Dataset
We use a conditional random field model (CRF) to extract tags from labelled training data, which was tagged by human news assistants. 
e wrote about our approach on the [New York Times Open blog](http://open.blogs.nytimes.com/2015/04/09/extracting-structured-data-from-recipes-using-conditional-random-fields/).
This repo contains scripts to extract the Quantity, Unit, Name, and Comments from unstructured ingredient phrases. 
We use it on Cooking to format incoming recipes. Given the following input:

```
1 pound carrots, young ones if possible
Kosher salt, to taste
2 tablespoons sherry vinegar
2 tablespoons honey
2 tablespoons extra-virgin olive oil
1 medium-size shallot, peeled and finely diced
1/2 teaspoon fresh thyme leaves, finely chopped
Black pepper, to taste
```
"""

_URL = "https://github.com/nytimes/ingredient-phrase-tagger"


_URLS = {
    "train": "https://huggingface.co/datasets/napsternxg/nyt_ingredients/resolve/main/nyt-ingredients.crf.jsonl"
}

import json


class NYTIngredientsConfig(datasets.BuilderConfig):
    """The NYT Ingredients Dataset."""

    def __init__(self, **kwargs):
        """BuilderConfig for NYT Ingredients.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(NYTIngredientsConfig, self).__init__(**kwargs)


class NYTIngredients(datasets.GeneratorBasedBuilder):
    """The NYT Ingredients Dataset."""

    BUILDER_CONFIGS = [
        NYTIngredientsConfig(
            name="nyt_ingredients",
            version=datasets.Version("1.0.0"),
            description="The NYT Ingredients Dataset",
        ),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "input": datasets.Value("string"),
                    "display_input": datasets.Value("string"),
                    "tokens": datasets.Sequence(datasets.Value("string")),
                    "index": datasets.Sequence(datasets.Value("string")),
                    "lengthGroup": datasets.Sequence(datasets.Value("string")),
                    "isCapitalized": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                "NoCAP",
                                "YesCAP"
                            ]
                        )
                    ),
                    "insideParenthesis": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                "NoPAREN",
                                "YesPAREN",
                            ]
                        )
                    ),
                    "label": datasets.Sequence(
                        datasets.features.ClassLabel(
                            names=[
                                "O",
                                "B-COMMENT",
                                "I-COMMENT",
                                "B-NAME",
                                "I-NAME",
                                "B-RANGE_END",
                                "I-RANGE_END",
                                "B-QTY",
                                "I-QTY",
                                "B-UNIT",
                                "I-UNIT",
                            ]
                        )
                    ),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/nytimes/ingredient-phrase-tagger",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        downloaded_files = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": downloaded_files["train"]},
            ),
        ]

    def _generate_examples(self, filepath):
        logger.info("⏳ Generating examples from = %s", filepath)
        with open(filepath, encoding="utf-8") as fp:
            for i, line in enumerate(fp):
                yield i, json.loads(line)