def recommned_friends_for_all(): activeUsers = filter_users_with_checkintimes(10) for user in activeUsers: print "{0} starts".format(user) other = _gen_recommend_time_friends_for(user, 20) try: while True: t = other.next() print "recommend {0} and {1} with each other, they have {2} common checkin time".format(user, t[0], t[1]) except StopIteration: pass finally: print "{0} over".format(user)
def friends_checkin_location_similarity(): totalCheckinsFile = open('Gowalla_totalCheckins.txt', 'r') # filter users, we care only about users whose checkin times greater than some number activeUsers = filter_users_with_checkintimes(200) locations = {} similarities = [] with open('friends.txt', 'r') as f: for line in f: token = line.strip().split('\t') uid = int(token[0]) # if the user is one we care about if uid in activeUsers: friends = [int(s) for s in token[1].split(' ')] # get my most check in location myLocation = -1 if uid in locations.keys(): myLocation = locations[uid] else: myLocation = get_user_location(uid, totalCheckinsFile) # get the most checkin location of the user locations[uid] = myLocation #print "{0} {1}".format(uid, myLocation) totalCheckinsFile.seek(0) # get friends' most check in location, and count the same ones sameCount = 0 for fuid in friends: if fuid in activeUsers: friendLocation = -1 if fuid in locations.keys(): friendLocation = locations[fuid] else: friendLocation = get_user_location(fuid, totalCheckinsFile) locations[fuid] = friendLocation if friendLocation == myLocation: sameCount += 1 #print "{0} {1}".format(fuid, friendLocation) similarity = float(sameCount) / len(friends) similarities.append(similarity) print "user {0} checkin location similarity: {1}".format(uid, similarity) totalCheckinsFile.close() return sum(similarities) / len(similarities)
def _gen_recommend_friends_for(me, minCommonFriends): # we only recommend users whose checkin times greater than some number for you activeUsers = filter_users_with_checkintimes(10) myFriends = get_user_friends(me) if len(myFriends) >= minCommonFriends: for other in activeUsers: if other not in myFriends and other != me: otherFriends = get_user_friends(other) if len(otherFriends) >= minCommonFriends: commonFriends = len(myFriends.intersection(otherFriends)) if commonFriends >= minCommonFriends: yield other, commonFriends
def friends_checkin_time_similarity(): totalCheckinsFile = open('Gowalla_totalCheckins.txt', 'r') # filter users, we care only about users whose checkin times greater than some number activeUsers = filter_users_with_checkintimes(200) times = {} similarities = [] with open('friends.txt', 'r') as f: for line in f: token = line.strip().split('\t') uid = int(token[0]) if uid in activeUsers: friends = [int(s) for s in token[1].split(' ')] myTime = -1 if uid in times.keys(): myTime = times[uid] else: myTime = get_user_time(uid, totalCheckinsFile) times[uid] = myTime #print "{0} {1}".format(uid, myTime) totalCheckinsFile.seek(0) sameCount = 0 for fuid in friends: if fuid in activeUsers: friendTime = -1 if fuid in times.keys(): friendTime = times[fuid] else: friendTime = get_user_time(fuid, totalCheckinsFile) times[fuid] = friendTime if friendTime == myTime: sameCount += 1 #print "{0} {1}".format(fuid, friendTime) similarity = float(sameCount) / len(friends) similarities.append(similarity) print "user {0} checkin time similarity: {1}".format(uid, similarity) totalCheckinsFile.close() return sum(similarities) / len(similarities)
def _gen_recommend_time_friends_for(me, minTimes): totalCheckinsFile = open('Gowalla_totalCheckins.txt', 'r') activeUsers = filter_users_with_checkintimes(200) myFriends = get_user_friends(me) totalCheckinsFile.seek(0) myTimes = get_user_times(me, totalCheckinsFile) #print "me: {0}, time:{1}".format(me, myTimes) totalCheckinsFile.seek(0) if len(myTimes) >= minTimes: for other in activeUsers: if other not in myFriends and other != me: otherTimes = get_user_times(other, totalCheckinsFile) #print "other:{0} time:{1}".format(other, otherTimes) if (len(otherTimes) >= minTimes): commonTimes = len(myTimes.intersection(otherTimes)) if commonTimes >= minTimes: yield other, commonTimes totalCheckinsFile.close()