On this page
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.

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:
- Naive Bayes
- MNB_classifier
- BernoulliNB_classifier
- LogisticRegression_classifier
- LinearSVC_classifier
- SGDClassifier
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

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

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

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.

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.
Popular Publishers
| Publisher | Number of articles |
|---|---|
| Evening Express | 3,343 |
| Weekly Mail | 286 |
| The Cardiff Times | 193 |
| Flintshire Observer Mining Journal... | 141 |
| South Wales Daily News | 131 |
| Rhyl Record and Advertiser | 111 |
| The Aberystwith Observer | 107 |
| County Observer and Monmouthshire... | 94 |
| Denbighshire Free Press | 85 |
| The Cambrian News and Merionethshire Standard | 81 |
| Abergavenny Chronicle | 71 |
| Monmouthshire Merlin | 57 |
| The Cambrian | 55 |
| The North Wales Express | 53 |
| The Western Mail | 53 |
| South Wales Echo | 46 |
| The Cardigan Observer and General Advertiser... | 46 |
| The Pembrokeshire Herald and General Advertiser | 46 |
| Carnarvon and Denbigh Herald and North and South Wales... | 42 |
| The North Wales Chronicle and Advertiser... | 42 |
| The Rhondda Leader | 40 |
| The Brecon County Times Neath Gazette... | 39 |
| Barry Dock News | 36 |
| Barry Herald | 33 |
| The Aberdare Times | 31 |
| The Carmarthen Journal and South Wales Weekly Advertiser | 30 |
Popular Ingredients over time

Taking the six most popular ingredients from our classifier results and performing a frequency analysis on the classified documents.
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


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.
