master-thesis/sandbox.ipynb

258 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import json \n",
"from pprint import pprint\n",
"import nltk\n",
"import word2vec\n",
"from gensim.test.utils import common_texts, get_tmpfile\n",
"from gensim.models import Word2Vec"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from json_buffered_reader import JSON_buffered_reader as JSON_br"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import settings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* read in objects:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"recipes = JSON_br(settings.one_million_recipes_file)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"\n",
" * experimenting a little bit with texts"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"ingredients = []\n",
"instructions = []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"filling with first n datasets"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"n = 50"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"i = 0\n",
"\n",
"for recipe in recipes:\n",
" ing = []\n",
" ins = []\n",
" for ingredient in recipe['ingredients']:\n",
" ing.append(ingredient['text'])\n",
" \n",
" for instruction in recipe['instructions']:\n",
" ins.append(instruction['text'])\n",
" \n",
" ingredients.append(ing)\n",
" instructions.append(ins)\n",
" \n",
" i += 1\n",
" if i >= n:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* filling in summarized datasets"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"summarized_ingredients = []\n",
"summarized_instructions = []"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"test = \"\".join([ins + ' ' for ins in instructions[0]])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Preheat the oven to 350 F. Butter or oil an 8-inch baking dish. Cook the penne 2 minutes less than package directions. (It will finish cooking in the oven.) Rinse the pasta in cold water and set aside. Combine the cooked pasta and the sauce in a medium bowl and mix carefully but thoroughly. Scrape the pasta into the prepared baking dish. Sprinkle the top with the cheeses and then the chili powder. Bake, uncovered, for 20 minutes. Let the mac and cheese sit for 5 minutes before serving. Melt the butter in a heavy-bottomed saucepan over medium heat and whisk in the flour. Continue whisking and cooking for 2 minutes. Slowly add the milk, whisking constantly. Cook until the sauce thickens, about 10 minutes, stirring frequently. Remove from the heat. Add the cheeses, salt, chili powder, and garlic powder. Stir until the cheese is melted and all ingredients are incorporated, about 3 minutes. Use immediately, or refrigerate for up to 3 days. This sauce reheats nicely on the stove in a saucepan over low heat. Stir frequently so the sauce doesnt scorch. This recipe can be assembled before baking and frozen for up to 3 monthsjust be sure to use a freezer-to-oven pan and increase the baking time to 50 minutes. One-half teaspoon of chipotle chili powder makes a spicy mac, so make sure your family and friends can handle it! The proportion of pasta to cheese sauce is crucial to the success of the dish. It will look like a lot of sauce for the pasta, but some of the liquid will be absorbed. '"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"token = nltk.word_tokenize(test)\n",
"pos_tags = nltk.pos_tag(token)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['6 ounces penne',\n",
" '2 cups Beechers Flagship Cheese Sauce (recipe follows)',\n",
" '1 ounce Cheddar, grated (1/4 cup)',\n",
" '1 ounce Gruyere cheese, grated (1/4 cup)',\n",
" '1/4 to 1/2 teaspoon chipotle chili powder (see Note)',\n",
" '1/4 cup (1/2 stick) unsalted butter',\n",
" '1/3 cup all-purpose flour',\n",
" '3 cups milk',\n",
" '14 ounces semihard cheese (page 23), grated (about 3 1/2 cups)',\n",
" '2 ounces semisoft cheese (page 23), grated (1/2 cup)',\n",
" '1/2 teaspoon kosher salt',\n",
" '1/4 to 1/2 teaspoon chipotle chili powder',\n",
" '1/8 teaspoon garlic powder',\n",
" '(makes about 4 cups)']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ingredients[0]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('1/4', 'CD'),\n",
" ('cup', 'NN'),\n",
" ('(', '('),\n",
" ('1/2', 'CD'),\n",
" ('stick', 'NN'),\n",
" (')', ')'),\n",
" ('unsalted', 'VBD'),\n",
" ('butter', 'NN')]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nltk.pos_tag(nltk.word_tokenize(ingredients[0][5]))"
]
},
{
"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
}