Inverse-Rezeptsuche/test.py

57 lines
1.4 KiB
Python
Raw Normal View History

2020-04-10 21:34:58 +00:00
from application.db import Session, Recipe, Ingredient, Link
dbSession = Session()
2020-04-10 22:42:37 +00:00
inputArr = ["kartoffeln", "zwiebel", "steak", "würfel"]
2020-04-10 21:34:58 +00:00
def slow():
recipes = dbSession.query(Recipe).all()
arr = {}
for recipe in recipes:
rec = recipe
recipe = recipe.ingredients()
2020-04-10 22:42:37 +00:00
if len(recipe) > len(inputArr) + 2:
continue
2020-04-10 21:34:58 +00:00
counter = 0
for i in inputArr:
for x in recipe:
if i in x:
counter += 1
continue
counter = str(counter)
if counter not in arr:
arr[counter] = []
arr[counter].append(rec.ingredients())
#print(rec.name)
2020-04-10 22:42:37 +00:00
print(arr)
2020-04-10 21:34:58 +00:00
def faster():
indx = {}
for inpu in inputArr:
ids = []
for x in dbSession.query(Ingredient).filter(Ingredient.name.contains(inpu)).all():
2020-04-10 22:42:37 +00:00
2020-04-10 21:34:58 +00:00
for y in x.recipe:
2020-04-10 22:42:37 +00:00
if dbSession.query(Link).filter(Link.recipe_id==y.recipe_id).count() > len(inputArr) + 5:
continue
if str(y.recipe_id) not in indx:
indx[str(y.recipe_id)] = 0
2020-04-10 21:34:58 +00:00
2020-04-10 22:42:37 +00:00
indx[str(y.recipe_id)] += 1
2020-04-10 21:34:58 +00:00
for key, value in indx.items():
if value >= len(inputArr):
print(dbSession.query(Recipe).filter(Recipe.recipe_id==key).first().ingredients())
#print(key)
faster()
2020-04-10 22:42:37 +00:00
slow()