master-thesis/yummly_ingredients_list.ipynb

164 lines
3.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from json_buffered_reader import JSON_buffered_reader as JSON_br\n",
"\n",
"import settings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* load all ingredients from the yummly dataset and merge them into a huge set"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ingredients = []\n",
"ingredient_count = {}\n",
"\n",
"min_count = 100"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"for recipe in JSON_br(settings.yummly_train):\n",
" for ingredient in recipe['ingredients']:\n",
" if ingredient in ingredient_count:\n",
" ingredient_count[ingredient] += 1\n",
" if ingredient_count[ingredient] == min_count:\n",
" ingredients.append(ingredient)\n",
" else:\n",
" ingredient_count[ingredient] = 1\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"all_ingredients_set = set(ingredients)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"648"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(all_ingredients_set)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* save to python file:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"filename = 'cooking_ingredients.py'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"file_obj = open(filename,'w')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"file_obj.write(\"#!/usr/bin/env python3\\n\")\n",
"\n",
"file_obj.write(\"\\ningredients = [\\n\")\n",
"\n",
"\n",
"first_item = ingredients[0]\n",
"\n",
"file_obj.write(' \"' + first_item + '\"')\n",
"for ingredient in ingredients[1:]:\n",
" file_obj.write(',\\n \"' + ingredient + '\"')\n",
"\n",
"file_obj.write(\"\\n]\\n\")\n",
"\n",
"file_obj.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}