Initialization done, Recipe to text

This commit is contained in:
Jonas Weinz 2020-01-02 18:14:02 +01:00
parent b4aae0ba02
commit 82507a2b89
6 changed files with 2743 additions and 2053 deletions

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -61,7 +61,8 @@
"import settings\n", "import settings\n",
"import AdjacencyMatrix\n", "import AdjacencyMatrix\n",
"from tqdm.autonotebook import tqdm\n", "from tqdm.autonotebook import tqdm\n",
"from Recipe import Ingredient" "from Recipe import Ingredient\n",
"from ActionGroups import groups"
] ]
}, },
{ {
@ -186,12 +187,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "2db5dd15c87740729f16f5c258707db2", "model_id": "6b079a15f1b94d9e9ebce8ad6eb82058",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"HBox(children=(IntProgress(value=0, max=741659), HTML(value='')))" "HBox(children=(FloatProgress(value=0.0, max=527544.0), HTML(value='')))"
] ]
}, },
"metadata": {}, "metadata": {},
@ -220,7 +221,7 @@
" \n", " \n",
" grouped_act = groups[act]\n", " grouped_act = groups[act]\n",
" \n", " \n",
" m_grouped_act.add_entry(grouped_act, grouped_ing.to_json(),1)\n", " m_grouped_act.add_entry(grouped_act, grouped_ing.to_json(),c[i_act, j_ing])\n",
" " " "
] ]
}, },
@ -232,12 +233,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "1d3f802944654954a6abbf8cb70f116e", "model_id": "b5b6c8ff52d1407495ef7fcd3ff409a8",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"HBox(children=(IntProgress(value=0, max=4472620), HTML(value='')))" "HBox(children=(FloatProgress(value=0.0, max=2123892.0), HTML(value='')))"
] ]
}, },
"metadata": {}, "metadata": {},
@ -268,7 +269,7 @@
" for a in ing_b._action_set:\n", " for a in ing_b._action_set:\n",
" grouped_ing_b.apply_action(groups[a])\n", " grouped_ing_b.apply_action(groups[a])\n",
" \n", " \n",
" m_grouped_mix.add_entry(grouped_ing_a.to_json(), grouped_ing_b.to_json(),1)" " m_grouped_mix.add_entry(grouped_ing_a.to_json(), grouped_ing_b.to_json(),c[i_ing, j_ing])"
] ]
}, },
{ {
@ -279,12 +280,12 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "92548999e3a6427bb33af20d3e53d9ef", "model_id": "d4064d730bd34f49946f54b845738585",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"HBox(children=(IntProgress(value=0, max=114804), HTML(value='')))" "HBox(children=(FloatProgress(value=0.0, max=78714.0), HTML(value='')))"
] ]
}, },
"metadata": {}, "metadata": {},
@ -309,7 +310,7 @@
" \n", " \n",
" grouped_act = groups[act]\n", " grouped_act = groups[act]\n",
" \n", " \n",
" m_grouped_base_act.add_entry(grouped_act, base_ing,1)" " m_grouped_base_act.add_entry(grouped_act, base_ing,c[i_act,j_ing])"
] ]
}, },
{ {

View File

@ -75,7 +75,7 @@
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"<db.database_connection.DatabaseConnection at 0x7f5d9469c550>" "<db.database_connection.DatabaseConnection at 0x7fbc4c131510>"
] ]
}, },
"execution_count": 2, "execution_count": 2,
@ -101,8 +101,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: user 8.17 s, sys: 632 ms, total: 8.8 s\n", "CPU times: user 8.68 s, sys: 778 ms, total: 9.46 s\n",
"Wall time: 8.84 s\n" "Wall time: 12.5 s\n"
] ]
} }
], ],
@ -181,18 +181,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "96abe66e3b4340ea8031c54333c28431", "model_id": "70396063ce4140b48105751d0ece88c9",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
"text/plain": [ "text/plain": [
"HBox(children=(IntProgress(value=0, max=100000), HTML(value='')))" "HBox(children=(FloatProgress(value=0.0, max=100000.0), HTML(value='')))"
] ]
}, },
"metadata": {}, "metadata": {},
@ -202,28 +202,10 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"warning: recipe 7a6657f1be has no ingredient! skipping it\n", "warning: recipe 0a14762efd has no ingredient! skipping it\n",
"warning: recipe 985bf99cb0 has no ingredient! skipping it\n", "warning: recipe 361b00dc50 has no ingredient! skipping it\n",
"warning: recipe 7b06b89a00 has no ingredient! skipping it\n", "warning: recipe e3b081a317 has no ingredient! skipping it\n",
"warning: recipe 5d99d73716 has no ingredient! skipping it\n", "an error occured : 'NoneType' object has no attribute 'lower'\n"
"warning: recipe 5ea97ce121 has no ingredient! skipping it\n",
"warning: recipe 219c14975a has no ingredient! skipping it\n",
"warning: recipe 312dab2c6e has no ingredient! skipping it\n",
"warning: recipe 9847df394d has no ingredient! skipping it\n",
"warning: recipe c456eca29f has no ingredient! skipping it\n",
"warning: recipe 63809adf7d has no ingredient! skipping it\n",
"warning: recipe d8097248d7 has no ingredient! skipping it\n",
"warning: recipe 4f6df45cbe has no ingredient! skipping it\n",
"warning: recipe fa8ffd2e9f has no ingredient! skipping it\n",
"warning: recipe 1274925e80 has no ingredient! skipping it\n",
"warning: recipe 780a3bfa7f has no ingredient! skipping it\n",
"warning: recipe 2f58557f49 has no ingredient! skipping it\n",
"warning: recipe 6e0201877c has no ingredient! skipping it\n",
"warning: recipe 4be587da96 has no ingredient! skipping it\n",
"warning: recipe b4fc8f359d has no ingredient! skipping it\n",
"warning: recipe 83dc617c2a has no ingredient! skipping it\n",
"warning: recipe fe406ab97d has no ingredient! skipping it\n",
"an error occured : 'NoneType' object is not subscriptable\n"
] ]
}, },
{ {
@ -244,27 +226,39 @@
" return [word2features(sent, i) for i in range(len(sent))]\n", " return [word2features(sent, i) for i in range(len(sent))]\n",
" File \"../Tagging/crf_data_generator.py\", line 87, in <listcomp>\n", " File \"../Tagging/crf_data_generator.py\", line 87, in <listcomp>\n",
" return [word2features(sent, i) for i in range(len(sent))]\n", " return [word2features(sent, i) for i in range(len(sent))]\n",
" File \"../Tagging/crf_data_generator.py\", line 19, in word2features\n", " File \"../Tagging/crf_data_generator.py\", line 54, in word2features\n",
" 'word[-3:]=' + word[-3:],\n", " '+1:word.lower=' + word1.lower(),\n",
"TypeError: 'NoneType' object is not subscriptable\n" "AttributeError: 'NoneType' object has no attribute 'lower'\n"
] ]
}, },
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"warning: recipe 83e51c3bc6 has no ingredient! skipping it\n", "warning: recipe 80dd4d10ab has no ingredient! skipping it\n",
"warning: recipe 4178a59023 has no ingredient! skipping it\n", "warning: recipe ac1ab87418 has no ingredient! skipping it\n",
"warning: recipe 37711ac6d0 has no ingredient! skipping it\n", "warning: recipe fa5b33cadc has no ingredient! skipping it\n",
"warning: recipe f93a2bfa73 has no ingredient! skipping it\n", "warning: recipe 80eb0385d0 has no ingredient! skipping it\n",
"warning: recipe 5f02077e4c has no ingredient! skipping it\n", "warning: recipe 2dfb05c466 has no ingredient! skipping it\n",
"warning: recipe 3cb76a27d1 has no ingredient! skipping it\n", "warning: recipe 7b372faa16 has no ingredient! skipping it\n",
"warning: recipe 4c786ec3d6 has no ingredient! skipping it\n", "warning: recipe f53fe916f2 has no ingredient! skipping it\n",
"warning: recipe 90d70c93c3 has no ingredient! skipping it\n", "warning: recipe b4ff30e1de has no ingredient! skipping it\n",
"warning: recipe a5f6838027 has no ingredient! skipping it\n", "warning: recipe cf2300c7ac has no ingredient! skipping it\n",
"warning: recipe d09e7e82f9 has no ingredient! skipping it\n",
"warning: recipe edbdc9c52f has no ingredient! skipping it\n",
"warning: recipe 4db0dadfce has no ingredient! skipping it\n",
"warning: recipe d9933872c6 has no ingredient! skipping it\n",
"warning: recipe 37af7ba84f has no ingredient! skipping it\n",
"warning: recipe cfc02f6bd7 has no ingredient! skipping it\n",
"warning: recipe fac985c58f has no ingredient! skipping it\n",
"warning: recipe 91308ac1b2 has no ingredient! skipping it\n",
"warning: recipe d3ff2bf3f9 has no ingredient! skipping it\n",
"warning: recipe ed9a0d0d51 has no ingredient! skipping it\n",
"warning: recipe 52f19fe220 has no ingredient! skipping it\n",
"warning: recipe 6fa7839c14 has no ingredient! skipping it\n",
"\n", "\n",
"CPU times: user 2h 14min 8s, sys: 42.9 s, total: 2h 14min 51s\n", "CPU times: user 1h 57min 25s, sys: 36.8 s, total: 1h 58min 2s\n",
"Wall time: 2h 15min 40s\n" "Wall time: 1h 58min 41s\n"
] ]
} }
], ],
@ -288,7 +282,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -297,7 +291,7 @@
"99999" "99999"
] ]
}, },
"execution_count": 9, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -308,7 +302,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -324,7 +318,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -336,7 +330,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -348,7 +342,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -360,7 +354,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 13,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -379,7 +373,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -398,10 +392,10 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"(65, 378992) (428558, 428558)\n", "(65, 272623) (293753, 293753)\n",
"43498 74724\n", "30381 42885\n",
"(65, 17533) (22178, 22178)\n", "(65, 11807) (13603, 13603)\n",
"15465 42881\n" "10725 19775\n"
] ]
} }
], ],
@ -414,15 +408,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 16,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"(65, 384445) (433582, 433582)\n", "(65, 272623) (293753, 293753)\n",
"749954 4526723\n" "30381 42885\n"
] ]
} }
], ],
@ -433,16 +427,16 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 18,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"43498" "30381"
] ]
}, },
"execution_count": 16, "execution_count": 18,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -453,26 +447,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 99, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"array([[1, 1, 0, ..., 0, 0, 0],\n",
" [0, 0, 1, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]], dtype=int64)"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [] "source": []
}, },
{ {

View File

@ -344,7 +344,7 @@
" def copy(self):\n", " def copy(self):\n",
" return RecipeState([ing.copy() for ing in self._ingredients])\n", " return RecipeState([ing.copy() for ing in self._ingredients])\n",
" \n", " \n",
" def apply_action(self, action: str, ing: Ingredient, instruction_number=0, sim_threshold = 0.6):\n", " def apply_action(self, action: str, ing: Ingredient, instruction_number=0, sim_threshold = 0.6, add_new_if_not_similar=True):\n",
" # find most similar ingredient to the given one and apply action on it\n", " # find most similar ingredient to the given one and apply action on it\n",
" sim_val, best_ing = ing.most_similar_ingredient(self._ingredients)\n", " sim_val, best_ing = ing.most_similar_ingredient(self._ingredients)\n",
" \n", " \n",
@ -360,7 +360,7 @@
" self._seen_applied_actions.add((action, best_ing.to_json()))\n", " self._seen_applied_actions.add((action, best_ing.to_json()))\n",
" best_ing.apply_action(action, instruction_number)\n", " best_ing.apply_action(action, instruction_number)\n",
" self._seen_ingredients.add(best_ing.to_json())\n", " self._seen_ingredients.add(best_ing.to_json())\n",
" else:\n", " elif add_new_if_not_similar:\n",
" self._ingredients.append(ing)\n", " self._ingredients.append(ing)\n",
" if action not in actions.stemmed_mixing_cooking_verbs:\n", " if action not in actions.stemmed_mixing_cooking_verbs:\n",
" self._seen_actions.add(action)\n", " self._seen_actions.add(action)\n",
@ -1284,7 +1284,7 @@
" actions_used.add(action['lemma'])\n", " actions_used.add(action['lemma'])\n",
" ingredients_used.add(ing_str)\n", " ingredients_used.add(ing_str)\n",
" # apply action on state\n", " # apply action on state\n",
" current_state.apply_action(action['lemma'], ing, instruction_number=instruction_number)\n", " current_state.apply_action(action['lemma'], ing, instruction_number=instruction_number, add_new_if_not_similar=False)\n",
" if debug:\n", " if debug:\n",
" print(f\"\\tapply {action['lemma']} on {ing}\")\n", " print(f\"\\tapply {action['lemma']} on {ing}\")\n",
" \n", " \n",
@ -1302,7 +1302,7 @@
" actions_used.add(act_token['lemma'])\n", " actions_used.add(act_token['lemma'])\n",
" ingredients_used.add(ing_str)\n", " ingredients_used.add(ing_str)\n",
" ing = Ingredient(ing_str)\n", " ing = Ingredient(ing_str)\n",
" current_state.apply_action(act_token['lemma'], ing, instruction_number=instruction_number)\n", " current_state.apply_action(act_token['lemma'], ing, instruction_number=instruction_number, add_new_if_not_similar=False)\n",
" if debug:\n", " if debug:\n",
" print(f\"\\tapply {act_token['lemma']} on {ing}\")\n", " print(f\"\\tapply {act_token['lemma']} on {ing}\")\n",
" break\n", " break\n",
@ -1339,7 +1339,7 @@
" print(\"mixing all ingredients in this instruction:\")\n", " print(\"mixing all ingredients in this instruction:\")\n",
" \n", " \n",
" for ing_str in clustered_ingredients:\n", " for ing_str in clustered_ingredients:\n",
" current_state.apply_action(\"mix\", Ingredient(ing_str), instruction_number=instruction_number)\n", " current_state.apply_action(\"mix\", Ingredient(ing_str), instruction_number=instruction_number, add_new_if_not_similar=False)\n",
" \n", " \n",
" for ing in current_state.get_combined_ingredients():\n", " for ing in current_state.get_combined_ingredients():\n",
" if debug:\n", " if debug:\n",

View File

@ -234,7 +234,7 @@ class RecipeState(object):
def copy(self): def copy(self):
return RecipeState([ing.copy() for ing in self._ingredients]) return RecipeState([ing.copy() for ing in self._ingredients])
def apply_action(self, action: str, ing: Ingredient, instruction_number=0, sim_threshold = 0.6): def apply_action(self, action: str, ing: Ingredient, instruction_number=0, sim_threshold = 0.6, add_new_if_not_similar=True):
# find most similar ingredient to the given one and apply action on it # find most similar ingredient to the given one and apply action on it
sim_val, best_ing = ing.most_similar_ingredient(self._ingredients) sim_val, best_ing = ing.most_similar_ingredient(self._ingredients)
@ -250,7 +250,7 @@ class RecipeState(object):
self._seen_applied_actions.add((action, best_ing.to_json())) self._seen_applied_actions.add((action, best_ing.to_json()))
best_ing.apply_action(action, instruction_number) best_ing.apply_action(action, instruction_number)
self._seen_ingredients.add(best_ing.to_json()) self._seen_ingredients.add(best_ing.to_json())
else: elif add_new_if_not_similar:
self._ingredients.append(ing) self._ingredients.append(ing)
if action not in actions.stemmed_mixing_cooking_verbs: if action not in actions.stemmed_mixing_cooking_verbs:
self._seen_actions.add(action) self._seen_actions.add(action)
@ -1150,7 +1150,7 @@ class Recipe(object):
actions_used.add(action['lemma']) actions_used.add(action['lemma'])
ingredients_used.add(ing_str) ingredients_used.add(ing_str)
# apply action on state # apply action on state
current_state.apply_action(action['lemma'], ing, instruction_number=instruction_number) current_state.apply_action(action['lemma'], ing, instruction_number=instruction_number, add_new_if_not_similar=False)
if debug: if debug:
print(f"\tapply {action['lemma']} on {ing}") print(f"\tapply {action['lemma']} on {ing}")
@ -1168,7 +1168,7 @@ class Recipe(object):
actions_used.add(act_token['lemma']) actions_used.add(act_token['lemma'])
ingredients_used.add(ing_str) ingredients_used.add(ing_str)
ing = Ingredient(ing_str) ing = Ingredient(ing_str)
current_state.apply_action(act_token['lemma'], ing, instruction_number=instruction_number) current_state.apply_action(act_token['lemma'], ing, instruction_number=instruction_number, add_new_if_not_similar=False)
if debug: if debug:
print(f"\tapply {act_token['lemma']} on {ing}") print(f"\tapply {act_token['lemma']} on {ing}")
break break
@ -1205,7 +1205,7 @@ class Recipe(object):
print("mixing all ingredients in this instruction:") print("mixing all ingredients in this instruction:")
for ing_str in clustered_ingredients: for ing_str in clustered_ingredients:
current_state.apply_action("mix", Ingredient(ing_str), instruction_number=instruction_number) current_state.apply_action("mix", Ingredient(ing_str), instruction_number=instruction_number, add_new_if_not_similar=False)
for ing in current_state.get_combined_ingredients(): for ing in current_state.get_combined_ingredients():
if debug: if debug:

File diff suppressed because one or more lines are too long