Created
July 22, 2014 09:50
-
-
Save prashantdce19/f229aa7c9fe925bdb095 to your computer and use it in GitHub Desktop.
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'] | |
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): | |
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 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(keywords,number): | |
""" | |
integer -> list of first no. of 'integer' users with highest activity | |
""" | |
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)<number): | |
req_users = req_users + final_list[i] | |
else: | |
break | |
return req_users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment