本文整理匯總了Python中db.DB.get_instance方法的典型用法代碼示例。如果您正苦於以下問題:Python DB.get_instance方法的具體用法?Python DB.get_instance怎麽用?Python DB.get_instance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類db.DB
的用法示例。
在下文中一共展示了DB.get_instance方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def load(user_id=None, user_name=None):
assert user_id is not None or user_name is not None
u = None
if user_id is not None:
u = DB.get_instance().query(Author).filter_by(id=user_id).first()
elif user_name is not None:
u = DB.get_instance().query(Author).filter_by(name=user_name).first()
return u
示例2: __init__
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def __init__(self, user):
self.user = user.id
self.user_vec = ProfileOneHotEncoder.add_info_to_vec(self.user.vector, self.user.gender, self.user.location,
self.user.emotion)
self.graph = Author.load_graph()
self.graph.add_node(user.id)
favorite_tweets = DB.get_instance().query(Favorite.tweet_id).filter(Favorite.user_id == user.id)
self.authors_liked = DB.get_instance().query(Tweet.user_name).filter(
Tweet.id.in_(favorite_tweets.subquery())).all()
for a in self.authors_liked:
self.graph.add_edge(user.id, a[0])
示例3: create_authors
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def create_authors(limit=None):
from models.tweet import Tweet
tpc = TopicsClassifier(limit=limit)
pp = PredictionProfile(limit=limit)
query = DB.get_instance().query(Tweet.user_id, Tweet.vector, Tweet.user_name)
if limit is not None:
query = query.limit(limit)
for t in query.all():
a = Author.load(user_name=t.user_name)
if a is None:
a = Author()
a.id = t.user_id
a.name = t.user_name
a.nb_click = 0
a.vector = np.zeros(300)
DB.get_instance().add(a)
a.update_profile(np.array(t.vector))
DB.get_instance().commit()
print('Authors created')
# graph = Author.load_graph()
# centralities = nx.eigenvector_centrality(graph)
for author in DB.get_instance().query(Author).all():
# author.centrality = centralities[author.name] if author.name in centralities else 0.
author.predict_profile(tpc, pp)
DB.get_instance().commit()
示例4: _load_enc
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def _load_enc():
encoder = OneHotEncoder()
X = []
query = DB.get_instance().query(Tweet.gender, Tweet.country, Tweet.sentiment).distinct(Tweet.country).group_by(
Tweet.country, Tweet.gender, Tweet.sentiment)
for gender, country, sentiment in query.all():
X.append([gender, country, sentiment])
encoder.fit(X)
return encoder
示例5: update_profile
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def update_profile(self, vec):
"""
Update the profile of the user with the new vec param
:param vec: (np.array) vector of the tweet to add
:return:
"""
assert type(vec) == np.array
self.nb_click += 1
for i in range(len(self.vector)):
self.vector[i] = (self.vector[i] * (self.nb_click - 1)) / self.nb_click + (vec[i] / self.nb_click)
tpc = TopicsClassifier()
pp = PredictionProfile()
self.predict_profile(tpc, pp)
DB.get_instance().commit()
示例6: remove_favorite
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def remove_favorite(self, tweet):
from models.favorite import Favorite
from models.tweet import Tweet
DB.get_instance().query(Favorite).filter_by(user_id=self.id).filter_by(tweet_id=tweet.id).delete(
synchronize_session='fetch')
favs = DB.get_instance().query(Favorite.tweet_id, Tweet.vector).join(Tweet,
Tweet.id == Favorite.tweet_id).filter(
'favorites.user_id=' + str(self.id)).all()
vects = [f[1] for f in favs]
self.vector = np.mean(vects, axis=0)
tpc = TopicsClassifier()
pp = PredictionProfile()
self.predict_profile(tpc, pp)
DB.get_instance().commit()
示例7: __init__
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def __init__(self, user, parent):
QAbstractTableModel.__init__(self, parent)
self.current_user_name = user
self.db = DB.get_instance()
self.words = self.get_words()
self.columns_keys = self.get_columns_keys()
self.columns_names = self.get_columns_names()
self.rows_count = len(self.words)
self.columns_count = len(self.columns_names)
self.portuguese_index = self.get_portuguese_index()
示例8: rerank_authors
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def rerank_authors(self, authors_prio):
"""
rerank results from authors_prio based on their similarity with the user
:param authors_prio: the jaccard coefficient for the link prediction between the user and each kept author
:return:
"""
reranked_reco = []
for a, p in authors_prio:
author = DB.get_instance().query(Author).filter(Author.name == a).first()
author_vec = ProfileOneHotEncoder.add_info_to_vec(author.vector, author.gender, author.localisation,
author.emotion).reshape(1, -1)
sim = cosine_similarity(self.user_vec, author_vec)
reranked_reco.append({'author_name': a, 'sim': sim[0]})
return sorted(reranked_reco, key=lambda k: k['sim'], reverse=True)
示例9: __init__
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import get_instance [as 別名]
def __init__(self):
self.corpus = None
# self.load_corpus()
self.parser = Parser()
self.tweets = DB.get_instance().query(Tweet).limit(100)
print('tweets loaded')