added display for missing ingred per recipe

This commit is contained in:
Askill 2020-05-13 11:04:21 +02:00
parent 3834c1bf3e
commit f8f04410ad
5 changed files with 57 additions and 20 deletions

View File

@ -27,12 +27,10 @@ class RecipeList(Resource):
end = time.time()
print("get recipes",end - start, "\n")
#start = time.time()
recs = search.getRecDict2(indx, ingreds)
end = time.time()
print("calc overlay",end - start, "\n")
g.session.commit()
g.session.close()
return flask.make_response(flask.jsonify({'data': recs}), 200)

View File

@ -8,6 +8,7 @@ import heapq
from collections import Counter
import background.migrate
def search2(inputArr):
indx = {}
dbSession = db.Session()
@ -32,6 +33,7 @@ def stemInput(inputArr):
return inputArr2
#
def getRecDict2(indx, inputArr):
dbSession = db.Session()
@ -42,30 +44,41 @@ def getRecDict2(indx, inputArr):
indx = k.most_common(1000)
indx = dict(indx)
ingred = [x for x in dbSession.query(db.Recipe.recipe_id, db.IngredTrunk.trunk_name, db.IngredTrunk.ingredient_name ).filter(db.Recipe.recipe_id.in_(indx.keys())).join(db.RecIngred).join(db.Ingredient).join(db.IngredTrunk).all()]
ingred = [x for x in dbSession.query(db.Recipe.recipe_id, db.IngredTrunk.trunk_name, db.IngredTrunk.ingredient_name).filter(
db.Recipe.recipe_id.in_(indx.keys())).join(db.RecIngred).join(db.Ingredient).join(db.IngredTrunk).all()]
ingredDict = {}
for k,v, i in ingred:
# RezeptID, stemmed Ingred, full ingred Name
# Dict spiegelt DB wieder, key, full ingred, stemmed
for k, v, i in ingred:
if k not in ingredDict:
ingredDict[k] = {}
if i not in ingredDict[k]:
ingredDict[k][i] = []
ingredDict[k][i].append(v)
inputArr += defaultArr
# checks overlay per recipeID
# itareate over ingreds and checks per stemmed ingred
# returns accurate percentage of overlay
# since overlay scare is the key of dict it is reduced by insignificant number to preserve all values
for key, value in ingredDict.items():
overlay = calcOverlay2(inputArr, value)
overlay, missing = calcOverlay2(inputArr, value)
while overlay in outDict.keys():
overlay -= 0.0001
outDict[overlay] = int(key)
outDict[overlay] = (int(key), missing)
# return Dict with 20 highest value keys
outDict2 = {}
for key in heapq.nlargest(20, outDict.keys()):
key2 = outDict[key]
rec = dbSession.query(db.Recipe).filter(db.Recipe.recipe_id == key2).first()
key2 = outDict[key][0]
missing = outDict[key][1]
rec = dbSession.query(db.Recipe).filter(
db.Recipe.recipe_id == key2).first()
outDict2[key] = (key2, rec.name, rec.url, [r[0] + ": " + r[1] for r in dbSession.query(db.Ingredient.name,
db.RecIngred.ingredient_amount).join(db.RecIngred).join(db.Recipe).filter(db.Recipe.recipe_id == key2).all()])
db.RecIngred.ingredient_amount).join(db.RecIngred).join(db.Recipe).filter(db.Recipe.recipe_id == key2).all()], missing)
return outDict2
def stem(l1):
snowball = nltk.SnowballStemmer(language='german')
stopset = set(stopwords.words('german'))
@ -73,17 +86,24 @@ def stem(l1):
l1 = [snowball.stem(l) for l in l1]
return l1
def calcOverlay2(l1, l2):
'''Calculates overlay and returns missing ingredients, [score (float), missing([])]'''
counter = 0
for ll in l2.values():
notIn = []
for key, ll in l2.items():
missing = True
for l in ll:
if l in l1:
counter += 1
missing = False
break
if missing:
notIn.append(key)
counter = counter / len(l2)
return counter
return counter, notIn
# it is assumed that everyone has this
defaultArr = ["Wasser", "salz", "pfeffer"]

View File

@ -32,6 +32,7 @@
}
.recipe-name{
margin: 1rem 0;
color: whitesmoke !important;
}
.recipe-instructions{
display: grid;
@ -48,7 +49,9 @@
text-align: left;
}
.missing{
color: rgb(220, 100, 120);
}
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */

View File

@ -51,24 +51,32 @@ function renderRecipeList(data){
function (key) {
data1 = data[key]
ingredString = ""
missingString = ""
data1[3].forEach(
function(ingred){
ingredString += `${ingred}<br>`
}
)
data1[4].forEach(
function(ingred){
missingString += `${ingred}<br>`
}
)
recString = `
<a href="${data1[2]}" target="_blank">
<div class="card text-white bg-primary mb-3" style="max-width: 100%">
<div class="card-body recipe-container">
<div class="row">
<div class="col-lg-5 col-sm-5 col">
<img class="recipe-img" src="/api/v1/images/${data1[0]}">
<a href="${data1[2]}" target="_blank">
<img class="recipe-img" src="/api/v1/images/${data1[0]}">
</a>
</div>
<div class="col-lg col-sm col">
<div class="row">
<div class="col">
<span><h4 class="recipe-name">${data1[1]}</h4></span>
<a href="${data1[2]}" target="_blank">
<span><h4 class="recipe-name">${data1[1]}</h4></span>
</a>
</div>
</div>
<div class="row">
@ -76,6 +84,14 @@ function renderRecipeList(data){
<div class="recipe-ingredients">${ingredString}</div>
</div>
</div>
<div class="row">
<div class="col">
<br>Fehlt:<br>
<div class="recipe-ingredients missing">
${missingString}
</div>
</div>
</div>
</div>
<div class="col-lg-1 col-sm-2 col-2">
@ -84,7 +100,7 @@ function renderRecipeList(data){
</div>
</div>
</div>
</a>
`
rl.innerHTML += recString