Inverse-Rezeptsuche/test.py

76 lines
2.0 KiB
Python
Raw Normal View History

2020-04-10 21:34:58 +00:00
2020-04-11 20:49:43 +00:00
from application.db import Session, Recipe, Ingredient, Link, Trunk
import nltk as nltk
from nltk.corpus import stopwords
2020-04-10 21:34:58 +00:00
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)
2020-04-11 20:49:43 +00:00
#
def stemIngred():
stopset = set(stopwords.words('german'))
stopset |= set("(),")
count = dbSession.query(Ingredient).count()
for x in dbSession.query(Ingredient).all():
snowball = nltk.SnowballStemmer(language='german')
for token in nltk.word_tokenize(x.name):
if token in stopset or len(token) < 3:
continue
stemmed = snowball.stem(token)
x.trunks.append(Trunk(name=stemmed))
dbSession.commit()
print(x.ingredient_id/count)
#faster()
#slow()
print(dbSession.query(Trunk.name).all())