Intro

I have been interested in Machine Learning for some time, but I’ve been unable to think of a project to work on that I could apply Machine Learning to.

The National Library of Wales has a number of large data sets that I could apply Machine Learning on, including the Welsh Newspapers Online.

Recently I’ve been thinking about all the possibilities that you could apply Machine learning to with the Newspaper data, until my colleague, Hazel Thomas, was demonstrating the Welsh Newspapers Online website to people at the National Eisteddfod. Being a self described ‘Foodie’ she was demonstrating example search queries on the website, which included the terms ‘recipe’ and ‘baking’.

The problem

Although the website does contain a number of recipes for cooking, the full-text search nature of the website doesn’t always return intended results.

Newspaper clipping titled Dangerous Recipe about a fatal paraffin hair-oil recipe
Dangerous Recipe

This gave me the idea to use Machine Learning to classify articles in the Newspaper database and create a cookbook from the results.

Gathering the data

The first stage in classifying articles was to gather a corpus of documents to classify, the documents would come from the search results of three keywords, Cooking, Baking and Recipe, which generated 198,966 articles, after an analysis of the search results, many articles relating to cooking came from articles with the title Homely Hints and Dainty Dishes which generated 10,105 articles. Combining the search results gave me a total of 205,131 articles to classify.

Algorithms and Tools

Tools

The programming languages and packages used in the classification were Python, Scikit-learn and nltk.

Algorithm

The algorithm I chose for classification of articles was the Naive Bayes classifier, which is also used to detect spam emails, otherwise known as Ham or Spam.

Inspired by a Youtube video on NLTK by sentdex, I used the following classifiers on the Newspaper data set:

Each Newspaper article would be classified against each of these classifiers and the results (positive or negative) from the classifiers were placed in a voting system, if an article had a higher percentage of positive results it would be marked as a recipe otherwise the article would be ignored.

While training the data, the Bernoulli classifier proved to be the least accurate algorithm, it was not included in the voting process to give an odd number in the voting process.

Training Data

To classify an article as ‘Ham’ or ‘Spam’, I needed to train the classifiers with example articles that contain cooking recipes and articles that do not.

To achieve this; I first wrote a bash script to retrieve 500 random articles from our 205,131 articles, and ingest them into a MySQL database, I then wrote a small PHP application that would allow me to view the articles and choose whether an article was a recipe (positive) or not (negative).

The articles that contained cooking recipes were then exported to ‘positive.txt’ file and the articles marked as negative were exported to ‘negative.txt’ file.

Training the classifiers

Once the positive and negative training data was compiled, it was time to train the classifiers.

Although, my first attempt at running the training data against the classifiers didn’t produce the results I was expecting at first…

We don’t make mistakes, just happy little accidents” — Bob Ross

Python code opening negative and positive training files using write mode
Spot the obvious error.

Once the ‘happy little accident’ was resolved, I then ran the training data against the classifiers twice, the second time increasing the number of items in the training and testing set. Each run would print out the accuracy of each of the classifiers and the top keywords in the Naive Bayes algorithm.

First run

Word cloud of the 30 most prominent terms in the first feature set
Top 30 words from feature set — feature set size 14,767

View top 500 words.

Classifier results

  • Naive Bayes accuracy — 91.10%
  • MNB_classifier accuracy — 94.65%
  • BernoulliNB_classifier accuracy — 90.68%
  • LogisticRegression_classifier accuracy — 93.22%
  • LinearSVC_classifier accuracy — 93.24%
  • SGDClassifier accuracy — 93.30%

Second run

Word cloud of the 30 most prominent terms in the second feature set
Top 30 words from feature set — feature set size 18,010

View top 500 words.

Classifier results

  • Naive Bayes accuracy — 91.16%
  • MNB_classifier accuracy — 95.11%
  • BernoulliNB_classifier accuracy — 90.98%
  • LogisticRegression_classifier accuracy — 93.74%
  • LinearSVC_classifier accuracy — 93.98%
  • SGDClassifier accuracy — 94.20%

Classifying the data

Once the classifiers had been trained, I ran the trained classifiers against the full data set of 205,131 articles, this classified 6,029 articles as being recipes relating to cooking.

Results

A sample of recipes can be found in my new cook book. Please let me know if you actually cook anything from here :).

Visualisations

Number of articles over time.

Line chart showing newspaper articles containing recipes increasing over time
Number of articles that contain recipes over time

There is an anomaly in the graph above; where the number of articles drops down to zero, I’ve not looked into the reasons behind this yet, but I imagine it is an issue with the data as opposed to a lack of published articles.

PublisherNumber of articles
Evening Express3,343
Weekly Mail286
The Cardiff Times193
Flintshire Observer Mining Journal...141
South Wales Daily News131
Rhyl Record and Advertiser111
The Aberystwith Observer107
County Observer and Monmouthshire...94
Denbighshire Free Press85
The Cambrian News and Merionethshire Standard81
Abergavenny Chronicle71
Monmouthshire Merlin57
The Cambrian55
The North Wales Express53
The Western Mail53
South Wales Echo46
The Cardigan Observer and General Advertiser...46
The Pembrokeshire Herald and General Advertiser46
Carnarvon and Denbigh Herald and North and South Wales...42
The North Wales Chronicle and Advertiser...42
The Rhondda Leader40
The Brecon County Times Neath Gazette...39
Barry Dock News36
Barry Herald33
The Aberdare Times31
The Carmarthen Journal and South Wales Weekly Advertiser30

View all publisher details.

Stacked time-series chart of popular recipe ingredients
Popular ingredients

Taking the six most popular ingredients from our classifier results and performing a frequency analysis on the classified documents.

Stacked chart of popular recipe ingredients between 1890 and 1910
Popular Ingredients 1890–1910

The same frequency analysis, but with a focus on the most active period, 1890–1910.

The increase in the number of articles for recipes and increased mention of popular ingredients does match the trend found in other online articles.

Britain’s annual per capita consumption of sugar was 4lbs in 1704, 18lbs in 1800, 90lbs in 1901 — a 22-fold increase to the point where Britons had the highest sugar intake in Europe. — https://www.theguardian.com/uk/2007/oct/13/lifeandhealth.britishidentity

Charts showing nineteenth-century sugar imports and their countries of origin
Sugar Imports — http://tradingconsequences.blogs.edina.ac.uk/2014/04/08/a-quick-exploration-of-ten-nineteenth-century-british-imports/
Charts showing nineteenth-century butter imports and their countries of origin
Butter Imports — http://tradingconsequences.blogs.edina.ac.uk/2014/04/08/a-quick-exploration-of-ten-nineteenth-century-british-imports/

Future work

If I were to start this project again; I would probably look into removing stop words and stemming words, to see if there would be an improvement in the classification of articles.