nyt_ingredients / nyt-ingredients.py
napsternxg's picture
Create nyt-ingredients.py
3a7e6d8
raw
history blame
No virus
4.03 kB
"""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"
import json
class NYTIngedientsConfig(datasets.BuilderConfig):
"""The NYTIngedients Dataset."""
def __init__(self, **kwargs):
"""BuilderConfig for NYTIngedients.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(NYTIngedientsConfig, self).__init__(**kwargs)
class NYTIngedients(datasets.GeneratorBasedBuilder):
"""The WNUT 17 Emerging Entities Dataset."""
BUILDER_CONFIGS = [
NYTIngedientsConfig(
name="nyt-ingredients", version=datasets.Version("1.0.0"), description="The NYTIngedients 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.Value("string")),
"insideParenthesis": datasets.Sequence(datasets.Value("string")),
"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."""
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": "nyt-ingredients.crf.jsonl"}),
]
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)