started adapting search for new db modell
This commit is contained in:
parent
2577c798c3
commit
d145ecee55
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -10,7 +10,7 @@ import time
|
|||
engine = db.create_engine('mysql+mysqldb://root@server/fs2?charset=utf8mb4', echo=False, encoding="utf8", pool_size=1000, max_overflow=0)
|
||||
|
||||
Base = declarative_base()
|
||||
Session = sessionmaker(bind=engine)
|
||||
Session = sessionmaker(bind=engine, autoflush=False)
|
||||
|
||||
# https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#association-object
|
||||
|
||||
|
|
@ -20,7 +20,7 @@ class Recipe(Base):
|
|||
name = Column('name', Text)
|
||||
instructions = Column('instructions', Text)
|
||||
url = Column('url', Text)
|
||||
img = Column('img', LargeBinary)
|
||||
img = Column('img', LargeBinary(length=(2**32)-1))
|
||||
imgURL = Column('imgURL', Text)
|
||||
ingredient = relationship("RecIngred", back_populates="recipe")
|
||||
|
||||
|
|
@ -30,25 +30,26 @@ class RecIngred(Base):
|
|||
ingredient_amount = Column('ingredient_amount', Text)
|
||||
ingredient_amount_mu = Column('ingredient_amount_mu', Text) # measurement unit
|
||||
|
||||
recipe_id = Column(Integer, ForeignKey('recipe.recipe_id'), primary_key=True)
|
||||
ingredient_name = Column(String(50), ForeignKey('ingredient.name'), primary_key=True)
|
||||
|
||||
recipe = relationship("Recipe", back_populates="ingredient")
|
||||
ingredient = relationship("Ingredient", back_populates="recipe")
|
||||
|
||||
recipe_id = Column(Integer, ForeignKey('recipe.recipe_id'), primary_key=True)
|
||||
ingredient_name = Column(String(200), ForeignKey('ingredient.name'), primary_key=True)
|
||||
|
||||
|
||||
class Ingredient(Base):
|
||||
__tablename__ = "ingredient"
|
||||
name = Column('name', String(50), primary_key=True)
|
||||
name = Column('name', String(200), primary_key=True)
|
||||
recipe = relationship("RecIngred", back_populates="ingredient")
|
||||
trunks = relationship("IngredTrunk", back_populates="ingredient")
|
||||
|
||||
class IngredTrunk(Base):
|
||||
__tablename__ = 'ingredtrunk'
|
||||
ingredient_name = Column(String(50), ForeignKey('ingredient.name'), primary_key=True)
|
||||
trunk_name = Column(String(50), ForeignKey('ingredient.name'), primary_key=True)
|
||||
ingredient_name = Column(String(200), ForeignKey('ingredient.name'), primary_key=True)
|
||||
trunk_name = Column(String(50), ForeignKey('trunk.name'), primary_key=True)
|
||||
|
||||
ingredient = relationship("Ingredient", back_populates="trunk")
|
||||
trunk = relationship("Trunk", back_populates="ingredient")
|
||||
ingredient = relationship("Ingredient", back_populates="trunks")
|
||||
trunk = relationship("Trunk", back_populates="ingredients")
|
||||
|
||||
class Trunk(Base):
|
||||
__tablename__ = "trunk"
|
||||
|
|
|
|||
|
|
@ -20,10 +20,10 @@ class RecipeList(Resource):
|
|||
args = parser.parse_args()
|
||||
ingreds = args["ingred"]
|
||||
|
||||
ingreds = [migrate.stemWord(ingred)[0] for ingred in ingreds + search.defaultArr]
|
||||
ingreds = [migrate.stem(ingred)[0] for ingred in ingreds + search.defaultArr]
|
||||
|
||||
start = time.time()
|
||||
indx = search.fastes(ingreds)
|
||||
indx = search.search2(ingreds)
|
||||
end = time.time()
|
||||
print("get recipes",end - start, "\n")
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import cv2
|
|||
import base64
|
||||
import nltk as nltk
|
||||
from nltk.corpus import stopwords
|
||||
import db as db1
|
||||
import db2 as db2
|
||||
#import db as db1
|
||||
#import db2 as db2
|
||||
|
||||
def stemWord(word):
|
||||
try:
|
||||
|
|
@ -25,7 +25,7 @@ def stemWord(word):
|
|||
|
||||
#migrate('./data/recs.json')
|
||||
|
||||
def migrateDb1ToDb2():
|
||||
def migrateRecsDb1ToDb2():
|
||||
|
||||
session1 = db1.Session()
|
||||
session2 = db2.Session()
|
||||
|
|
@ -57,4 +57,47 @@ def migrateDb1ToDb2():
|
|||
count+=1
|
||||
print(count/length)
|
||||
|
||||
migrateDb1ToDb2()
|
||||
def TrunkDb2():
|
||||
session2 = db2.Session()
|
||||
|
||||
count = 0
|
||||
length = session2.query(db2.Ingredient).count()
|
||||
for i2 in session2.query(db2.Ingredient).all():
|
||||
try:
|
||||
for trunk1 in stem(i2.name):
|
||||
|
||||
ri2 = db2.IngredTrunk()
|
||||
trunk = session2.query(db2.Trunk).filter(db2.Trunk.name == trunk1).first()
|
||||
if trunk is None:
|
||||
trunk = db2.Trunk(name=trunk1)
|
||||
|
||||
if session2.query(db2.IngredTrunk).filter(db2.IngredTrunk.ingredient_name == i2.name, db2.IngredTrunk.trunk_name == trunk1).first() is None:
|
||||
ri2.trunk = trunk
|
||||
i2.trunks.append(ri2)
|
||||
|
||||
session2.commit()
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
session2 = db2.Session()
|
||||
|
||||
count+=1
|
||||
print(count/length)
|
||||
|
||||
|
||||
def stem(l1):
|
||||
'''Tokenize and stem word, result is 1d list'''
|
||||
snowball = nltk.SnowballStemmer(language='german')
|
||||
stopset = set(stopwords.words('german'))
|
||||
stopset |= set("(),")
|
||||
l2 = []
|
||||
|
||||
for token in nltk.word_tokenize(l1):
|
||||
token = snowball.stem(token)
|
||||
if token in stopset or not token.isalpha() or len(token) < 2:
|
||||
continue
|
||||
l2.append(token)
|
||||
|
||||
return l2
|
||||
#migrateDb1ToDb2()
|
||||
#TrunkDb2()
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
import sqlalchemy as db
|
||||
from sqlalchemy import Column, String, Integer, Numeric, Table, DateTime, ARRAY, ForeignKey, create_engine, LargeBinary, Enum, Text
|
||||
from sqlalchemy.orm import sessionmaker, relationship, column_property
|
||||
from datetime import datetime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
import enum
|
||||
from flask import Flask
|
||||
import time
|
||||
|
||||
engine = db.create_engine('mysql+mysqldb://root@server/fs?charset=utf8mb4', echo=False, encoding="utf8", pool_size=1000, max_overflow=0)
|
||||
|
||||
Base = declarative_base()
|
||||
Session = sessionmaker(bind=engine)
|
||||
|
||||
# https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#association-object
|
||||
|
||||
|
||||
class Recipe(Base):
|
||||
__tablename__ = "recipe"
|
||||
recipe_id = Column('recipe_id', Integer, primary_key=True, autoincrement=True)
|
||||
name = Column('name', Text)
|
||||
instructions = Column('instructions', Text)
|
||||
url = Column('url', Text)
|
||||
img = Column('img', LargeBinary(length=(2**32)-1))
|
||||
ingredient = relationship("Ingredient", backref="recipe")
|
||||
trunk = relationship("Trunk", backref="recipe")
|
||||
|
||||
|
||||
class Ingredient(Base):
|
||||
__tablename__ = "ingredient"
|
||||
ingredient_id = Column('ingredient_id', Integer, primary_key=True, autoincrement=True)
|
||||
name = Column('name', Text)
|
||||
ingredient_amount = Column('ingredient_amount', Text)
|
||||
ingredient_amount_mu = Column('ingredient_amount_mu', Text) # measurement unit
|
||||
|
||||
recipe_id = Column(Integer, ForeignKey('recipe.recipe_id'))
|
||||
|
||||
|
||||
class Trunk(Base):
|
||||
__tablename__ = "trunk"
|
||||
trunk_id = Column('trunk_id', Integer, primary_key=True, autoincrement=True)
|
||||
name = Column('name', Text)
|
||||
|
||||
recipe_id = Column(Integer, ForeignKey('recipe.recipe_id'))
|
||||
|
||||
def initDB(counter):
|
||||
try:
|
||||
Base.metadata.create_all(engine)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
counter += 1
|
||||
if counter < 13:
|
||||
time.sleep(5)
|
||||
initDB(counter)
|
||||
|
||||
initDB(0)
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
import sqlalchemy as db
|
||||
from sqlalchemy import Column, String, Integer, Numeric, Table, DateTime, ARRAY, ForeignKey, create_engine, LargeBinary, Enum, Text
|
||||
from sqlalchemy.orm import sessionmaker, relationship, column_property
|
||||
from datetime import datetime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
import enum
|
||||
from flask import Flask
|
||||
import time
|
||||
|
||||
engine = db.create_engine('mysql+mysqldb://root@server/fs2?charset=utf8mb4', echo=False, encoding="utf8", pool_size=1000, max_overflow=0)
|
||||
|
||||
Base = declarative_base()
|
||||
Session = sessionmaker(bind=engine, autoflush=False)
|
||||
|
||||
# https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#association-object
|
||||
|
||||
class Recipe(Base):
|
||||
__tablename__ = "recipe"
|
||||
recipe_id = Column('recipe_id', Integer, primary_key=True, autoincrement=True)
|
||||
name = Column('name', Text)
|
||||
instructions = Column('instructions', Text)
|
||||
url = Column('url', Text)
|
||||
img = Column('img', LargeBinary(length=(2**32)-1))
|
||||
imgURL = Column('imgURL', Text)
|
||||
ingredient = relationship("RecIngred", back_populates="recipe")
|
||||
|
||||
class RecIngred(Base):
|
||||
|
||||
__tablename__ = 'recingred'
|
||||
ingredient_amount = Column('ingredient_amount', Text)
|
||||
ingredient_amount_mu = Column('ingredient_amount_mu', Text) # measurement unit
|
||||
|
||||
recipe = relationship("Recipe", back_populates="ingredient")
|
||||
ingredient = relationship("Ingredient", back_populates="recipe")
|
||||
|
||||
recipe_id = Column(Integer, ForeignKey('recipe.recipe_id'), primary_key=True)
|
||||
ingredient_name = Column(String(200), ForeignKey('ingredient.name'), primary_key=True)
|
||||
|
||||
|
||||
class Ingredient(Base):
|
||||
__tablename__ = "ingredient"
|
||||
name = Column('name', String(200), primary_key=True)
|
||||
recipe = relationship("RecIngred", back_populates="ingredient")
|
||||
trunks = relationship("IngredTrunk", back_populates="ingredient")
|
||||
|
||||
class IngredTrunk(Base):
|
||||
__tablename__ = 'ingredtrunk'
|
||||
ingredient_name = Column(String(200), ForeignKey('ingredient.name'), primary_key=True)
|
||||
trunk_name = Column(String(50), ForeignKey('trunk.name'), primary_key=True)
|
||||
|
||||
ingredient = relationship("Ingredient", back_populates="trunks")
|
||||
trunk = relationship("Trunk", back_populates="ingredients")
|
||||
|
||||
class Trunk(Base):
|
||||
__tablename__ = "trunk"
|
||||
name = Column('name', String(50), primary_key=True)
|
||||
ingredients = relationship("IngredTrunk", back_populates="trunk")
|
||||
|
||||
|
||||
def initDB(counter=0):
|
||||
try:
|
||||
Base.metadata.create_all(engine)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
counter += 1
|
||||
if counter < 13:
|
||||
time.sleep(5)
|
||||
initDB(counter)
|
||||
|
||||
initDB()
|
||||
15
search.py
15
search.py
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
from application.db import Session, Recipe, Ingredient, Trunk
|
||||
import application.db2 as db
|
||||
from flask import g
|
||||
import nltk as nltk
|
||||
from nltk.corpus import stopwords
|
||||
|
|
@ -21,6 +22,20 @@ def fastes(inputArr):
|
|||
indx[str(recipe_id[0])] += 1
|
||||
return(indx)
|
||||
|
||||
def search2(inputArr):
|
||||
indx = {}
|
||||
dbSession = db.Session()
|
||||
for inpu in inputArr:
|
||||
ids = []
|
||||
for recipe in dbSession.query(db.Trunk.ingredients.recipe).filter(db.Trunk.name == inpu).all():
|
||||
|
||||
if str(recipe.recipe_id) not in indx:
|
||||
indx[str(recipe.recipe_id)] = 0
|
||||
|
||||
indx[str(recipe.recipe_id)] += 1
|
||||
return(indx)
|
||||
|
||||
|
||||
def stemInput(inputArr):
|
||||
inputArr2 = []
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue