added display for missing ingred per recipe
This commit is contained in:
parent
3834c1bf3e
commit
f8f04410ad
|
|
@ -27,12 +27,10 @@ class RecipeList(Resource):
|
||||||
end = time.time()
|
end = time.time()
|
||||||
print("get recipes",end - start, "\n")
|
print("get recipes",end - start, "\n")
|
||||||
|
|
||||||
#start = time.time()
|
|
||||||
recs = search.getRecDict2(indx, ingreds)
|
recs = search.getRecDict2(indx, ingreds)
|
||||||
end = time.time()
|
end = time.time()
|
||||||
print("calc overlay",end - start, "\n")
|
print("calc overlay",end - start, "\n")
|
||||||
|
|
||||||
g.session.commit()
|
|
||||||
g.session.close()
|
g.session.close()
|
||||||
return flask.make_response(flask.jsonify({'data': recs}), 200)
|
return flask.make_response(flask.jsonify({'data': recs}), 200)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import heapq
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
import background.migrate
|
import background.migrate
|
||||||
|
|
||||||
|
|
||||||
def search2(inputArr):
|
def search2(inputArr):
|
||||||
indx = {}
|
indx = {}
|
||||||
dbSession = db.Session()
|
dbSession = db.Session()
|
||||||
|
|
@ -32,6 +33,7 @@ def stemInput(inputArr):
|
||||||
return inputArr2
|
return inputArr2
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
def getRecDict2(indx, inputArr):
|
def getRecDict2(indx, inputArr):
|
||||||
dbSession = db.Session()
|
dbSession = db.Session()
|
||||||
|
|
||||||
|
|
@ -42,30 +44,41 @@ def getRecDict2(indx, inputArr):
|
||||||
indx = k.most_common(1000)
|
indx = k.most_common(1000)
|
||||||
indx = dict(indx)
|
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 = {}
|
ingredDict = {}
|
||||||
|
# RezeptID, stemmed Ingred, full ingred Name
|
||||||
|
# Dict spiegelt DB wieder, key, full ingred, stemmed
|
||||||
for k, v, i in ingred:
|
for k, v, i in ingred:
|
||||||
if k not in ingredDict:
|
if k not in ingredDict:
|
||||||
ingredDict[k] = {}
|
ingredDict[k] = {}
|
||||||
if i not in ingredDict[k]:
|
if i not in ingredDict[k]:
|
||||||
ingredDict[k][i] = []
|
ingredDict[k][i] = []
|
||||||
|
|
||||||
ingredDict[k][i].append(v)
|
ingredDict[k][i].append(v)
|
||||||
inputArr += defaultArr
|
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():
|
for key, value in ingredDict.items():
|
||||||
overlay = calcOverlay2(inputArr, value)
|
overlay, missing = calcOverlay2(inputArr, value)
|
||||||
while overlay in outDict.keys():
|
while overlay in outDict.keys():
|
||||||
overlay -= 0.0001
|
overlay -= 0.0001
|
||||||
outDict[overlay] = int(key)
|
outDict[overlay] = (int(key), missing)
|
||||||
|
|
||||||
|
# return Dict with 20 highest value keys
|
||||||
outDict2 = {}
|
outDict2 = {}
|
||||||
for key in heapq.nlargest(20, outDict.keys()):
|
for key in heapq.nlargest(20, outDict.keys()):
|
||||||
key2 = outDict[key]
|
key2 = outDict[key][0]
|
||||||
rec = dbSession.query(db.Recipe).filter(db.Recipe.recipe_id == key2).first()
|
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,
|
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
|
return outDict2
|
||||||
|
|
||||||
|
|
||||||
def stem(l1):
|
def stem(l1):
|
||||||
snowball = nltk.SnowballStemmer(language='german')
|
snowball = nltk.SnowballStemmer(language='german')
|
||||||
stopset = set(stopwords.words('german'))
|
stopset = set(stopwords.words('german'))
|
||||||
|
|
@ -73,17 +86,24 @@ def stem(l1):
|
||||||
l1 = [snowball.stem(l) for l in l1]
|
l1 = [snowball.stem(l) for l in l1]
|
||||||
return l1
|
return l1
|
||||||
|
|
||||||
|
|
||||||
def calcOverlay2(l1, l2):
|
def calcOverlay2(l1, l2):
|
||||||
|
'''Calculates overlay and returns missing ingredients, [score (float), missing([])]'''
|
||||||
counter = 0
|
counter = 0
|
||||||
for ll in l2.values():
|
notIn = []
|
||||||
|
for key, ll in l2.items():
|
||||||
|
missing = True
|
||||||
for l in ll:
|
for l in ll:
|
||||||
if l in l1:
|
if l in l1:
|
||||||
counter += 1
|
counter += 1
|
||||||
|
missing = False
|
||||||
break
|
break
|
||||||
|
if missing:
|
||||||
|
notIn.append(key)
|
||||||
|
|
||||||
counter = counter / len(l2)
|
counter = counter / len(l2)
|
||||||
return counter
|
return counter, notIn
|
||||||
|
|
||||||
|
|
||||||
# it is assumed that everyone has this
|
# it is assumed that everyone has this
|
||||||
defaultArr = ["Wasser", "salz", "pfeffer"]
|
defaultArr = ["Wasser", "salz", "pfeffer"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@
|
||||||
}
|
}
|
||||||
.recipe-name{
|
.recipe-name{
|
||||||
margin: 1rem 0;
|
margin: 1rem 0;
|
||||||
|
color: whitesmoke !important;
|
||||||
}
|
}
|
||||||
.recipe-instructions{
|
.recipe-instructions{
|
||||||
display: grid;
|
display: grid;
|
||||||
|
|
@ -48,7 +49,9 @@
|
||||||
text-align: left;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.missing{
|
||||||
|
color: rgb(220, 100, 120);
|
||||||
|
}
|
||||||
.loader {
|
.loader {
|
||||||
border: 16px solid #f3f3f3; /* Light grey */
|
border: 16px solid #f3f3f3; /* Light grey */
|
||||||
border-top: 16px solid #3498db; /* Blue */
|
border-top: 16px solid #3498db; /* Blue */
|
||||||
|
|
|
||||||
|
|
@ -51,24 +51,32 @@ function renderRecipeList(data){
|
||||||
function (key) {
|
function (key) {
|
||||||
data1 = data[key]
|
data1 = data[key]
|
||||||
ingredString = ""
|
ingredString = ""
|
||||||
|
missingString = ""
|
||||||
data1[3].forEach(
|
data1[3].forEach(
|
||||||
function(ingred){
|
function(ingred){
|
||||||
ingredString += `${ingred}<br>`
|
ingredString += `${ingred}<br>`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
data1[4].forEach(
|
||||||
|
function(ingred){
|
||||||
|
missingString += `${ingred}<br>`
|
||||||
|
}
|
||||||
|
)
|
||||||
recString = `
|
recString = `
|
||||||
<a href="${data1[2]}" target="_blank">
|
|
||||||
<div class="card text-white bg-primary mb-3" style="max-width: 100%">
|
<div class="card text-white bg-primary mb-3" style="max-width: 100%">
|
||||||
<div class="card-body recipe-container">
|
<div class="card-body recipe-container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-5 col-sm-5 col">
|
<div class="col-lg-5 col-sm-5 col">
|
||||||
|
<a href="${data1[2]}" target="_blank">
|
||||||
<img class="recipe-img" src="/api/v1/images/${data1[0]}">
|
<img class="recipe-img" src="/api/v1/images/${data1[0]}">
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-lg col-sm col">
|
<div class="col-lg col-sm col">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
|
<a href="${data1[2]}" target="_blank">
|
||||||
<span><h4 class="recipe-name">${data1[1]}</h4></span>
|
<span><h4 class="recipe-name">${data1[1]}</h4></span>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
@ -76,6 +84,14 @@ function renderRecipeList(data){
|
||||||
<div class="recipe-ingredients">${ingredString}</div>
|
<div class="recipe-ingredients">${ingredString}</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col">
|
||||||
|
<br>Fehlt:<br>
|
||||||
|
<div class="recipe-ingredients missing">
|
||||||
|
${missingString}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-lg-1 col-sm-2 col-2">
|
<div class="col-lg-1 col-sm-2 col-2">
|
||||||
|
|
@ -84,7 +100,7 @@ function renderRecipeList(data){
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
|
||||||
|
|
||||||
`
|
`
|
||||||
rl.innerHTML += recString
|
rl.innerHTML += recString
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue