def buildFollowership(self): # get the follower list user_profile = UserProfile() user_profile.getUserList() self.follower_list = user_profile.user_list #convert to set to speed up index follower_set = set(self.follower_list) print(len(follower_set)) db = DB() result = db.db[config.followeroffollower_collection_name].find() for doc in result: if doc['user_id'] not in follower_set: continue if doc['user_id'] not in self.followership_dict: self.followership_dict[doc['user_id']] = [] if doc['followerID'] not in follower_set: # this follower is not in the target comnunitiy, # which is the followers of the target users defined in config.py continue self.followership_dict[doc['user_id']].append(doc['followerID']) db.close()
def __init__(self): self.user_corp = {} self.corp_dict = {} self.corp_bow = [] self.user_profile = UserProfile() self.user_profile.getUserList() self.user_list = self.user_profile.user_list self.cosine_distance_dict = {} self.lda_vect_dict = {}
def upload_hairstyle(request_obj): if 'selfie' not in request_obj.files and 'gender' not in request_obj.form and 'category' not in request_obj.form and 'style' not in request_obj.form: return "failed status.HTTP_400_BAD_REQUEST" # selfie is assumed to be of type bytes selfie = request_obj.files['selfie'] username = request_obj.form['username'] gender = request_obj.form['gender'] category = request_obj.form['category'] style = request_obj.form['style'] hair_model_path = os.path.dirname( __file__) + '/file_storage/trash/temp_files/hair_model.jpg' # if a file is currently stored at the users_face_path delete file if os.path.isfile(hair_model_path): os.remove(hair_model_path) if type(selfie) == np.ndarray: # no need for type conversion. cv2.imwrite(hair_model_path, selfie) pass else: # selfie is of type bytes. Store at the users_face_path location. selfie.save(hair_model_path) UserProfile(username).upload_hairstyle(hair_model_path, gender, category, style) return "saved"
def sign_up(request_obj): """ Collect and store new user's username, password, profile pic & gender. :param request_obj: request object containing the new user's name and gender in a request form. :return: """ if 'username' not in request_obj.form and 'gender' not in request_obj.form and 'password' not in request_obj.form and 'selfie' not in request_obj.files: return "failed status.HTTP_400_BAD_REQUEST FORMAT" username = request_obj.form['username'] gender = request_obj.form['gender'] password = request_obj.form['password'] selfie = request_obj.files['selfie'] users_face_path = os.path.dirname( __file__) + '/file_storage/trash/temp_files/profile_pic.jpg' # if a file is currently stored at the users_face_path delete file if os.path.isfile(users_face_path): os.remove(users_face_path) if type(selfie) == np.ndarray: # save to system folder, at the users_face_path location. cv2.imwrite(users_face_path, selfie) else: # save to system folder, at the users_face_path location. selfie.save(users_face_path) # create an instance of a user with name username and add details of the user. UserProfile(username).add_user(password, gender, users_face_path) # if successful then status = 'success ' return status
class TestWriteToFile(unittest.TestCase): database = DataManipulator() user = UserProfile(None,'Tester', '*****@*****.**', '0000') def test_create_user(self): result = self.database.add_new_user(self.user) self.assertEqual(True, result) def test_create_page(self): page = Page('testpage',None,self.user.get_id()) result = self.database.write_page_to_file(page.to_json()) self.assertEqual(True, result) def test_create_group(self): group = Group('testgroup',None,self.user.get_id()) result = self.database.write_group_to_file(group.to_json()) self.assertEqual(True, result) def test_create_event(self): event = Event('testing event',None,self.user.get_id()) event.set_place('at home') event.set_date('13/01/2020') event.set_time('15:55') event.set_about('testing the functionality of testing unit') result = self.database.write_event_to_file(event.to_json()) self.assertEqual(True, result) def test_create_post(self): post_content = 'this is a sample post to test the functionality of the test unit' post = Post(id=None, owner =self.user.get_id(),content=post_content) result = self.database.write_post(post.to_json()) self.assertEqual(True, result)
def __init__(self, user_name, hairstyle_category='All', style='All', index='All'): """ :param user_name: :param hairstyle_category: """ self.hairstyle_category = hairstyle_category self.style = style self.index = index self.user_name = user_name self.user_profile = UserProfile(user_name).profile()
def sign_in(request_obj): """ Collect and store new user's username, password, profile pic & gender. :param request_obj: request object containing the new user's name and gender in a request form. :return: """ if 'username' not in request_obj.form and 'password' not in request_obj.form: return "failed status.HTTP_400_BAD_REQUEST FORMAT" username = request_obj.form['username'] password = request_obj.form['password'] # create an instance of a user with name username and add details of the user. UserProfile(username).sign_in(password) # if successful then status = 'success ' return status
def create_new_user(self): """This function is used to create new users""" name = input('Enter user name: ') used_emails = [ user.get_email() for user in self.database.users.values() ] while True: email = input('Enter user email: ') if email in used_emails: print('email is already available in the system') else: break while True: password = input('Enter user password: '******'Re-enter user password: '******'Create user? (y/n)') if confirm.lower() == 'y' or confirm.lower() == 'yes': user = UserProfile(None, name, email, password) self.database.add_new_user(user)
def __read_users(self): """This function is used to read the information of all users stored in the directory /data/users. Every file is named using the ID of the user and contains his/her name,email, password,list of friends, list of owned pages, list of followed pages, list of owned groups, list of joined groups and list of events in a json format. """ path = os.path.join(self.cwd,'data/users') available_users = os.listdir(path) if len(available_users)>0: for user_id in available_users: if user_id == 'README.md': continue #assuming the user data was stored in JSON format with open(os.path.join(path,user_id),'r') as file: user_data = json.load(file) user = UserProfile(user_data['id'],user_data['name'], user_data['email'], user_data['password'], user_data['timeline']) user.init_friends(user_data['friends']) user.init_my_groups(user_data['my_groups']) user.init_joined_groups(user_data['joined_groups']) user.init_my_pages(user_data['my_pages']) user.init_followed_pages(user_data['followed_pages']) user.init_events(user_data['my_events']) self.users[user_id.split('.')[0]]=user
def add_new_user(self, user: UserProfile): self.users[user.get_id()] =user self.pages[user.timeLine.get_id()]= user.timeLine result1 = self.write_user_to_file(user.to_json()) result2 = self.write_page_to_file(user.timeLine.to_json()) return result1== True and result2 == True
from userProfile import UserProfile user_profile = UserProfile() user_profile.buildCorpProfile()
class LdaRecsys: def __init__(self): self.user_corp = {} self.corp_dict = {} self.corp_bow = [] self.user_profile = UserProfile() self.user_profile.getUserList() self.user_list = self.user_profile.user_list self.cosine_distance_dict = {} self.lda_vect_dict = {} def loadCorpDict(self, user_list=[]): return def saveCorpDict(self): return def buildCorpDict(self, user_list=[], no_blow_doc=10, no_above_doc=0.5): if not user_list: if not self.user_list: print('user list is empty! will run getUserList') self.user_profile.getUserList() self.user_list = self.user_profile.user_list print('get the user list') user_list = self.user_list if not user_list: print('user list is still empty!') return [] self.user_corp = self.user_profile.getCorpProfile(self.user_list) self.corp_dict = Dictionary( [corp for corp in list(self.user_corp.values())]) # Keep tokens which are contained in at least no_below documents # Keep tokens which are contained in no more than no_above documents # (fraction of total corpus size, not an absolute number) self.corp_dict.filter_extremes(no_below=no_blow_doc, no_above=no_above_doc) self.corp_dict.compactify() def buildCorpBow(self): if not self.user_corp or not self.corp_dict: self.buildCorpDict() self.corp_bow = {} for user, corp in self.user_corp.items(): self.corp_bow[user] = self.corp_dict.doc2bow(corp) def saveCorpBow(self): return def loadCorpBow(self): return def trainLDA(self, topics_num, iter_num=50): # reset the cosine_distance_dict in every training self.cosine_distance_dict = {} self.topics_num = topics_num corp_bow = [bow for bow in list(self.corp_bow.values())] self.lda = LdaMulticore(corp_bow, num_topics=topics_num, id2word=self.corp_dict, iterations=iter_num, workers=4) def runLDA(self, user_name): if user_name in self.corp_bow: user_bow = self.corp_bow[user_name] else: print('no such user! Please check the screen name') return user_lda = self.lda[user_bow] return user_lda def buildLdaVect(self): for user, bow in self.corp_bow.items(): vect = np.zeros(self.topics_num) user_lda = self.lda[bow] for i in user_lda: vect[i[0]] = i[1] self.lda_vect_dict[user] = vect def ldaCosineDistance(self, user_name): if not self.lda_vect_dict: self.buildLdaVect() if user_name not in self.lda_vect_dict: print('no such user') return cosine_distance_dict = {} user_vect = self.lda_vect_dict[user_name] for user, lda_vect in self.lda_vect_dict.items(): cosine_distance = pairwise_distances( np.array(user_vect).reshape(1, -1), np.array(lda_vect).reshape(1, -1), metric='cosine')[0][0] cosine_distance_dict[user] = cosine_distance self.cosine_distance_dict[user_name] = cosine_distance_dict def makeRecommendation(self, user_name, topn_recommendation=10): if user_name not in self.cosine_distance_dict: self.ldaCosineDistance(user_name) user_recommendations = self.cosine_distance_dict[user_name] n = 0 for recommendation, cosine_distance in sorted( user_recommendations.items(), key=lambda x: x[1]): print((recommendation, cosine_distance)) n += 1 if n == topn_recommendation: break return user_recommendations def showTopic(self, topic_number, topn_word=5): """ topic_numer: which topic to show topn_word: show top n words in this topic """ print(u'{:20} {}'.format(u'term', u'frequency') + u'\n') for term, frequency in self.lda.show_topic(topic_number, topn_word): print(u'{:20} {:.3f}'.format(term, round(frequency, 3))) def showUserTopic(self, user_name, topn_word=10): if user_name not in self.corp_bow: print('no such user! please check the screen name') return user_bow = self.corp_bow[user_name] user_lda = self.lda[user_bow] user_lda = sorted(user_lda, key=lambda x: -x[1]) for topic_number, freq in user_lda: print('topic number {} {}'.format(topic_number, freq)) print('|____') self.showTopic(topic_number, topn_word) print('\n')