ignore tags done
This commit is contained in:
parent
5e34db5494
commit
82a774e318
|
|
@ -14,22 +14,26 @@ class RecipeList(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
""" """
|
""" """
|
||||||
g.session = Session()
|
g.session = Session()
|
||||||
g.session = Session()
|
|
||||||
|
# get Input
|
||||||
parser = reqparse.RequestParser()
|
parser = reqparse.RequestParser()
|
||||||
parser.add_argument('ingred', type=str, action='append')
|
parser.add_argument('ingred', type=str, action='append')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
ingreds = args["ingred"]
|
ingreds = args["ingred"]
|
||||||
|
|
||||||
ingreds = [migrate.stem(ingred)[0] for ingred in ingreds]
|
# stem to find stems in db
|
||||||
|
stemmed = [migrate.stem(ingred)[0] for ingred in ingreds]
|
||||||
|
|
||||||
start = time.time()
|
start = time.time()
|
||||||
indx = search.search2(ingreds)
|
# returns ids of found recipes
|
||||||
end = time.time()
|
indx = search.search2(stemmed)
|
||||||
print("get recipes",end - start, "\n")
|
print("get recipes",time.time() - start, "\n")
|
||||||
|
|
||||||
recs = search.getRecDict2(indx, ingreds)
|
# returns dict with recipes, keys are the % of overlaps with recipes as values
|
||||||
end = time.time()
|
recs = search.getRecDict2(indx, stemmed)
|
||||||
print("calc overlay",end - start, "\n")
|
# ignored saved only the indices, has to be matched to the input before it's returned
|
||||||
|
recs["ignored"] = [ingreds[x] for x in recs["ignored"]]
|
||||||
|
print("calc overlay", time.time() - start, "\n")
|
||||||
|
|
||||||
g.session.close()
|
g.session.close()
|
||||||
return flask.make_response(flask.jsonify({'data': recs}), 200)
|
return flask.make_response(flask.jsonify({'data': recs}), 200)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import background.migrate
|
||||||
from sqlalchemy import exists
|
from sqlalchemy import exists
|
||||||
|
|
||||||
def search2(inputArr):
|
def search2(inputArr):
|
||||||
|
''' returns inputs with array of recipeID which use them '''
|
||||||
indx = {}
|
indx = {}
|
||||||
dbSession = db.Session()
|
dbSession = db.Session()
|
||||||
for inpu in inputArr:
|
for inpu in inputArr:
|
||||||
|
|
@ -22,6 +23,7 @@ def search2(inputArr):
|
||||||
|
|
||||||
|
|
||||||
def stemInput(inputArr):
|
def stemInput(inputArr):
|
||||||
|
''' returns array of stemmed input '''
|
||||||
inputArr2 = []
|
inputArr2 = []
|
||||||
|
|
||||||
snowball = nltk.SnowballStemmer(language='german')
|
snowball = nltk.SnowballStemmer(language='german')
|
||||||
|
|
@ -31,9 +33,8 @@ def stemInput(inputArr):
|
||||||
continue
|
continue
|
||||||
inputArr2.append(snowball.stem(word))
|
inputArr2.append(snowball.stem(word))
|
||||||
return inputArr2
|
return inputArr2
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: split into more functions
|
||||||
def getRecDict2(indx, inputArr):
|
def getRecDict2(indx, inputArr):
|
||||||
dbSession = db.Session()
|
dbSession = db.Session()
|
||||||
|
|
||||||
|
|
@ -47,8 +48,10 @@ def getRecDict2(indx, inputArr):
|
||||||
ingred = [x for x in dbSession.query(db.Recipe.recipe_id, db.IngredTrunk.trunk_name, db.IngredTrunk.ingredient_name).filter(
|
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()]
|
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
|
# RezeptID, stemmed Ingred, full ingred Name
|
||||||
# Dict spiegelt DB wieder, key, full ingred, stemmed
|
# Dict spiegelt DB wieder, key, full ingred, stemmed
|
||||||
|
# this structure makes calcOverlay() more efficient
|
||||||
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] = {}
|
||||||
|
|
@ -56,15 +59,16 @@ def getRecDict2(indx, inputArr):
|
||||||
ingredDict[k][i] = []
|
ingredDict[k][i] = []
|
||||||
ingredDict[k][i].append(v)
|
ingredDict[k][i].append(v)
|
||||||
|
|
||||||
|
# check if any input is not in db.Trunk
|
||||||
ignored = []
|
ignored = []
|
||||||
for x in inputArr:
|
for x in inputArr:
|
||||||
if not dbSession.query(exists().where(db.Trunk.name == x)).scalar():
|
if not dbSession.query(exists().where(db.Trunk.name == x)).scalar():
|
||||||
ignored.append(x)
|
ignored.append(inputArr.index(x))
|
||||||
|
|
||||||
inputArr += defaultArr
|
inputArr += defaultArr
|
||||||
|
|
||||||
# checks overlay per recipeID
|
# checks overlay per recipeID
|
||||||
# itareate over ingreds and checks per stemmed ingred
|
# iterate over ingreds and checks per stemmed ingred
|
||||||
# returns accurate percentage of overlay
|
# returns accurate percentage of overlay
|
||||||
# since overlay scare is the key of dict it is reduced by insignificant number to preserve all values
|
# 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():
|
||||||
|
|
@ -73,9 +77,8 @@ def getRecDict2(indx, inputArr):
|
||||||
overlay -= 0.0001
|
overlay -= 0.0001
|
||||||
outDict[overlay] = (int(key), missing)
|
outDict[overlay] = (int(key), missing)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# return Dict with 20 highest value keys
|
# return Dict with 20 highest value keys
|
||||||
|
# creates dict which is returned
|
||||||
outDict2 = {}
|
outDict2 = {}
|
||||||
for key in heapq.nlargest(20, outDict.keys()):
|
for key in heapq.nlargest(20, outDict.keys()):
|
||||||
key2 = outDict[key][0]
|
key2 = outDict[key][0]
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,6 @@ function checkforURLParam(){
|
||||||
params = url.split("?")[1]
|
params = url.split("?")[1]
|
||||||
if (params !== undefined){
|
if (params !== undefined){
|
||||||
// url param to string for search bar
|
// url param to string for search bar
|
||||||
paramString = params.split("&").join("").split("ingred=").join(", ").replace(", ", "")
|
|
||||||
|
|
||||||
document.getElementById("search-field").value = paramString
|
|
||||||
params = "?" + params
|
params = "?" + params
|
||||||
loadRecipes(params)
|
loadRecipes(params)
|
||||||
}
|
}
|
||||||
|
|
@ -24,7 +21,6 @@ function checkforURLParam(){
|
||||||
function loadData() {
|
function loadData() {
|
||||||
// make string of get params for request
|
// make string of get params for request
|
||||||
getParams = makeGetParamString()
|
getParams = makeGetParamString()
|
||||||
|
|
||||||
window.location = window.location.href.split("?")[0] + getParams
|
window.location = window.location.href.split("?")[0] + getParams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +35,6 @@ function loadRecipes(getParams){
|
||||||
ignored = data["data"]["ignored"]
|
ignored = data["data"]["ignored"]
|
||||||
data = data["data"]["ingred"] // remove wrapper
|
data = data["data"]["ingred"] // remove wrapper
|
||||||
|
|
||||||
|
|
||||||
renderRecipeList(data)
|
renderRecipeList(data)
|
||||||
renderIgnored(ignored)
|
renderIgnored(ignored)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue