Created
July 25, 2014 10:17
-
-
Save prashantdce19/d1e636e8e6a2acc0224c to your computer and use it in GitHub Desktop.
Updated the function select_no_of_users for invalid question ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
import datetime | |
client = MongoClient('localhost',27017) | |
db = client['convflowdev_trial'] | |
problems = db['problems'] | |
users = db['users'] | |
def count_keywords(posted_problem_keywords,search_problem_keywords): | |
count = 0 | |
for i in posted_problem_keywords: | |
if i in search_problem_keywords: | |
count = count + 1 | |
return count | |
def find_similiar_problems(keywords): | |
""" | |
(list of keywords) --> objectIDs | |
""" | |
list_of_ids = [(i['_id'],count_keywords(keywords,i['keywords'])) \ | |
for i in problems.find({'keywords':{'$in':keywords}})] | |
return sorted(list_of_ids, key=lambda ids : ids[1],reverse=True) | |
def user_activity_count(list_of_obj_ids): | |
#problem IdS | |
req_entries = problems.aggregate([ | |
{ "$match" : {'_id':{'$in':list_of_obj_ids}}}, | |
{ "$unwind": "$acl.users" }, | |
{ "$group": | |
{"_id": "$acl.users", | |
"count": {"$sum": 1 } | |
} | |
} | |
]) | |
return req_entries['result'] | |
def network_participants(user_id): | |
req_entries = problems.aggregate([{ "$match" : {'acl.users':{'$in':[user_id]}}},{ "$unwind": "$acl.users" },{ "$group":{"_id": "$acl.users","count": {"$sum": 1 }}}]) | |
return [(i['_id'],i['count']) for i in req_entries['result'] if i['_id']!=user_id] | |
def distribute_list_and_count_activity_index(id_list): | |
priority_list_order_1 = [i[0] for i in id_list if i[1]>3] | |
priority_list_order_2 = [i[0] for i in id_list if i[1]>1 and i[1]<4] | |
priority_list_order_3 = [i[0] for i in id_list if i[1]==1] | |
user_l1 = {i['_id']:i['count'] for i in user_activity_count(priority_list_order_1)} | |
user_l2 = {i['_id']:i['count'] for i in user_activity_count(priority_list_order_2)} | |
user_l3 = {i['_id']:i['count'] for i in user_activity_count(priority_list_order_3)} | |
f_user_list = set(user_l1.keys() + user_l2.keys() + user_l3.keys()) | |
final_list = {} | |
for i in f_user_list: | |
if i not in user_l1.keys(): | |
score_1 = 0 | |
else: | |
score_1 = user_l1[i] | |
if i not in user_l2.keys(): | |
score_2 = 0 | |
else: | |
score_2 = user_l2[i] | |
if i not in user_l3.keys(): | |
score_3 = 0 | |
else: | |
score_3 = user_l3[i] | |
net_score = score_1*0.6 + score_2*0.25 + score_3*0.15 | |
final_list[i] = net_score | |
all_scores = set(final_list.values()) | |
ff_dict = {} | |
for i in all_scores: | |
tmp_list = [] | |
for j in final_list: | |
if final_list[j] == i: | |
tmp_list.append(j) | |
ff_dict[i] = tmp_list | |
return ff_dict | |
def select_no_of_users(qID,number): | |
""" | |
integer -> list of first no. of 'integer' users with highest activity | |
""" | |
question_id = problems.find_one({'_id':qID}) | |
if str(type(question_id)) == "<type 'NoneType'>": | |
return "Error: No Such Problem" | |
else: | |
keywords = question_id['keywords'] | |
final_list = distribute_list_and_count_activity_index(find_similiar_problems(keywords)) | |
netscores = sorted(final_list,reverse=True) | |
required_scores = netscores[:number] | |
req_users = [] | |
for i in required_scores: | |
if(len(req_users)<2*number): | |
req_users = req_users + final_list[i] | |
else: | |
break | |
#users who are relevant by keywords -- 50% of all required recommendations to be taken from this | |
first_half = req_users[:2*number] | |
#users who are relevant by networks -- 50% of all required recommendations to be taken from this | |
users_in_network = sorted(network_participants(question_id['creator']),key=lambda users_IN: users_IN[1],reverse=True) | |
second_half = [i[0] for i in users_in_network[:2*number]] | |
net_users_in_network = [] #users who are in keywords list and networks list | |
net_users_not_in_network = [] #users who are in keywords list but NOT in networks list | |
for i in first_half: | |
if i in second_half: | |
net_users_in_network.append(i) | |
else: | |
net_users_not_in_network.append(i) | |
net_users = net_users_in_network[:(number/2)+1] + net_users_not_in_network[:(number/2)+1] | |
return net_users[:number] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment