def get_similarity_for_tag(self, tag): # a = db.session.query(func.unnest(func.string_to_array(strings_tags, ';')).label('value')).cte('param') result = db.session.query( Tag.id, Tag.value, Tag.word, Tag.meaning, func.similarity(Tag.value, tag).label('similarity')).filter( func.similarity(Tag.value, tag) > 0.6).order_by( desc(func.similarity( Tag.value, tag).label('similarity'))).limit(2).all() return result
def auto_movie(self, q): orig_q = q q = q.lstrip().lower().split(' ') full_words, q = q[:-1], q[-1] if 'the' in full_words: full_words.remove('the') target = literal(' ').op('||')(Movie.title) filters = [] for word in full_words: filters.append(target.ilike('%% %s %%' % word)) filters.append(target.ilike('%% %s%%' % q)) if len(filters) > 1: filters = and_(*filters) else: filters = filters[0] res = self.session.query(Movie.id, Movie.title, Rating.rating)\ .outerjoin((Rating, and_(Rating.movie_id == Movie.id, Rating.user == self.user)))\ .filter(filters)\ .order_by(func.similarity(func.lower(target), orig_q).desc())\ .limit(7).all() self.return_json(res)
def fetch_all_by_trgm_sim(self, smiles, *expr, **kwargs): """ Returns all fragments that are similar to the given SMILES string using trigam similarity (similar to LINGO). Parameters ---------- threshold : float, default=0.6 Similarity threshold that will be used for searching. limit : int, default=25 Maximum number of hits that will be returned. Returns ------- resultset : list List of tuples (Fragment, similarity) containing the chemical components and the calculated trigram similarity. Queried Entities ---------------- Fragment Examples -------- >>>> FragmentAdaptor().fetch_all_by_trgm_sim('Cc1ccc(cc1Nc2nccc(n2)c3cccnc3)NC(=O)c4ccc(cc4)CN5CC[NH+](CC5)C') [(<Fragment(STI)>, 0.883721), (<Fragment(NIL)>, 0.73913), (<Fragment(PRC)>, 0.738095), (<Fragment(406)>, 0.666667), (<Fragment(J07)>, 0.604167), (<Fragment(AD5)>, 0.6), (<Fragment(AAX)>, 0.6), (<Fragment(VX6)>, 0.6)] Requires -------- .. important:: `pg_trgm <http://www.postgresql.org/docs/current/static/pgtrgm.html>`_ PostgreSQL extension. """ session = Session() threshold = kwargs.get('threshold', 0.6) # SET THE SIMILARITY THRESHOLD FOR THE INDEX session.execute( text("SELECT set_limit(:threshold)").execution_options( autocommit=True).params(threshold=threshold)) similarity = func.similarity(Fragment.ism, smiles).label('similarity') sim_thresh = func.show_limit().label('sim_thresh') query = self.query.add_columns(similarity, sim_thresh) query = query.filter(and_(Fragment.like(smiles), *expr)) # KNN-GIST query = query.order_by(Fragment.ism.op('<->')(smiles)) if kwargs.get('limit'): query = query.limit(kwargs['limit']) results = query.all() #session.close() return results #query
def fetch_all_by_trgm_sim(self, smiles, *expressions, **kwargs): ''' Returns all molecules that are similar to the given SMILES string using trigam similarity (similar to LINGO). By far the quickest way to get similar molecules! Parameters ---------- threshold : float, default=0.6 Similarity threshold that will be used for searching. limit : int, default=25 Maximum number of hits that will be returned. Returns ------- resultset : list List of tuples (Emolecule, similarity) containing the chemical components and the calculated trigram similarity. Queried Entities ---------------- Emolecule Examples -------- Requires -------- .. important:: `pg_trgm <http://www.postgresql.org/docs/current/static/pgtrgm.html>`_ PostgreSQL extension. Notes ----- Will make use of the KNNGIST operator <-> in 9.1. ''' threshold = kwargs.get('threshold', 0.6) limit = kwargs.get('limit', 25) # SET THE SIMILARITY THRESHOLD FOR THE INDEX session.execute( text("SELECT set_limit(:threshold)").execution_options( autocommit=True).params(threshold=threshold)) similarity = func.similarity(Emolecule.smiles, smiles).label('similarity') query = session.query(Emolecule, similarity) query = query.filter(and_(Emolecule.like(smiles), *expressions)) query = query.order_by(similarity.desc()).limit(limit) return query.all()
def get_similarity_query(self, query): return (func.coalesce(func.similarity(self.column, query), 0).label('first_name_rank') * self.rank)
def get_similarity(self, value): return db.session.query(TagerTag.id).filter( func.similarity(TagerTag.value, value) > 0.3).first()
def get_similarity(self, value): return db.session.query( TagerColor.id # TagerColor.name ).filter(func.similarity(TagerColor.name, value) > 0.3).first()