Building a food graph is an interesting problem.

Such graphs can be used to mine similar recipes, analyse relationship between cuisines and food cultures etc.

This blog post from NYTimes about “Extracting Structured Data From Recipes Using Conditional Random Fields” could be an initial step towards building such graphs.

In an attempt to implement the idea shared in the blog post mentioned above, I’ve used CRFSuite to build a model that tags entities in ingredients list.

CRFSuite installation instruction here.

Note: For the impatient, Please checkout the TL;DR section at the end of the post.

3 steps to reach the goal.

Understanding data. Preparing data. Building model.

Step 1: Understanding data.

The basic assumption is to use the following 5 entities to tag ingredients of a recipe.

Quantity (QTY) Unit (UNIT) Comment (COM) Name (NAME) Others (OTHERS)

For example,

Ingredient Quantity Unit Comment Name Others 2 tablespoons of soya sauce 2 tablespoons NA soya, sauce of Onions sliced and fried brown 3 medium 3 NA sliced, brown, fried onions and 3 Finely chopped Green Chillies 3 NA finely, chopped, green chillies NA

Similarly most of the ingredients shared in recipes can be tagged with these 5 labels.

Step 2: Preparing data.

Preparing data involves the following steps

Collecting data POS tagging Labeling tokens Chunking

A simple script to politely scrape data from any recipe site will do the job. Checkout Scrapy.

I’ve collected data in the following format.

The actual input file is a JSON Lines file.

A three column tab separated file is required for chunking.

Column 1 – Token

Column 2 – POS tag

Column 3 – Label (done manually)

Each token in a ingredient list gets a line in the TSV file and a new line is left to separate ingredients.

The following script generates data in required format taking the JSON lines file mentioned above as input.

$ cat recipes.jl | python crf_input_generator.py > token_pos.tsv

Note that XXX is just a place holder, which will be replaced by the actual label (i.e. one of QTY, UNIT, COM, NAME, OTHERS).

I’ve manually labeled each token with the help of OpenRefine, Skip this step if you are tagging using a model that is already available.

In the end the file should look similar to table shown below.

Next task is chunking and it is explained well here.

The same POS and token position features discussed in the tutorial are used as features in this experiment as well,So using the util script provided in the CRFSuite repository we can generate chunks.

$ cat token_pos_tagged.tsv | python ~/workspace/crfsuite/example/chunking.py -s $'\t' > chunk.txt

After chunking the final output file should look similar to this.

Step 3: Building model

To train

$ crfsuite learn -m <model_name> <chunk_file>

To test

$ crfsuite tag -qt -m <model_name> <chunk_file>

To tag

$ crfsuite tag -m <model_name> <chunk_file>

I’ve collected 2000 recipes out of which 60% is used for training and 40% is used for testing.

Each ingredient is tokenized, POS tagged and manually labeled (hardest part).

Following are the input, intermediate and output files.

recipes.jl – a JSON lines file containing 2000 recipes. Input file

token_pos.tsv – Intermediate TSV file with token and its POS. (column with XXX is a place holder for next step)

token_pos_tagged.tsv – TSV file with token, pos and label columns, after tagging 3rd column manually.

train.txt – 60% of input, chunked, for training

test.txt – 40% of input, chunked, for testing

recipe.model – model output

$ cat recipes.jl | python crf_input_generator.py > token_pos.tsv

Intermediate step: Manually label tokens and generate token_pos_tagged.tsv

$ cat token_pos_tagged.tsv | python ~/workspace/crfsuite/example/chunking.py > chunk.txt

Intermediate step: split chunk.txt in 60/40 ratio to get train.txt and test.txt respectively

Training

$ crfsuite learn -m recipes.model train.txt

Testing

$ crfsuite tag -qt -m recipes.model test.txt Performance by label (#match, #model, #ref) (precision, recall, F1): QTY: (7307, 7334, 7338) (0.9963, 0.9958, 0.9960) UNIT: (3944, 4169, 4091) (0.9460, 0.9641, 0.9550) COM: (5014, 5281, 5505) (0.9494, 0.9108, 0.9297) NAME: (11943, 12760, 12221) (0.9360, 0.9773, 0.9562) OTHER: (6984, 7094, 7483) (0.9845, 0.9333, 0.9582) Macro-average precision, recall, F1: (0.962451, 0.956244, 0.959025) Item accuracy: 35192 / 36638 (0.9605) Instance accuracy: 6740 / 7854 (0.8582) Elapsed time: 0.328684 [sec] (23895.3 [instance/sec])

Note: -qt option will work only with labeled data.

Precision 96% Recall 95% F1 Measure 95%

Read more about precision, recall and F1 measure here

To tag ingredients that the model has never seen before, follow Step 2 and run the following command

Tagging

$ crfsuite tag -m recipes.model test.txt

code and data here

13.082680 80.270718