Last active
August 29, 2015 14:04
-
-
Save prashantdce19/c7e7376ff67416b0c3f3 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) --> objectIDs | |
""" | |
list_of_ids = [(i['_id'],count_keywords(keywords,i['keywords'])) \ | |
for i in problems.find({'keywords':{'$in':keywords}})] | |
## print 'list_of_ids --> ' + str(list_of_ids) #ids are problems IDs | |
return sorted(list_of_ids, key=lambda ids : ids[1],reverse=True) | |
def distribute_list_and_count_activity_index(id_list): | |
l1 = [i[0] for i in id_list if i[1]>3] | |
l2 = [i[0] for i in id_list if i[1]>1 and i[1]<4] | |
l3 = [i[0] for i in id_list if i[1]==1] | |
## print 'list l1 --> ' + str(l1) | |
## print 'list l2 --> ' + str(l2) | |
## print 'list l3 --> ' + str(l3) | |
## print '' | |
## print 'f_user_list --> ' + str(f_user_list) | |
## print '' | |
user_list_1 = [] | |
user_list_2 = [] | |
user_list_3 = [] | |
## print 'working on l1' | |
for i in l1: | |
#post = problems.find_one( | |
a = problems.find_one({'_id':i}) | |
## print '' | |
## print a | |
user_list_1 = user_list_1 + a['acl']['users'] | |
ul_1 = {i:user_list_1.count(i) for i in set(user_list_1)} | |
## print 'ul_1 --> ' + str(ul_1) | |
## print '' | |
## print 'working on l2' | |
for i in l2: | |
a = problems.find_one({'_id':i}) | |
## print '' | |
## print a | |
user_list_2 = user_list_2 + a['acl']['users'] | |
ul_2 = {i:user_list_2.count(i) for i in set(user_list_2)} | |
## print 'ul_2 --> ' + str(ul_2) | |
## | |
## print '' | |
## print 'working on l3' | |
for i in l3: | |
a = problems.find_one({'_id':i}) | |
## print '' | |
## print a | |
user_list_3 = user_list_3 + a['acl']['users'] | |
## print '' | |
## print 'user_list_1 --> ' + str(user_list_1) | |
## print 'user_list_2 --> ' + str(user_list_2) | |
## print 'user_list_3 --> ' + str(user_list_3) | |
## print '' | |
f_user_list = set(user_list_1 + user_list_2 + user_list_3) | |
ul_3 = {i:user_list_3.count(i) for i in set(user_list_3)} | |
## print 'ul_3 --> ' + str(ul_3) | |
final_list = {} | |
#net_scores = [] | |
for i in f_user_list: | |
if i not in set(user_list_1): | |
score_1 = 0 | |
else: | |
score_1 = ul_1[i] | |
if i not in set(user_list_2): | |
score_2 = 0 | |
else: | |
score_2 = ul_2[i] | |
if i not in set(user_list_3): | |
score_3 = 0 | |
else: | |
score_3 = ul_3[i] | |
net_score = score_1*0.6 + score_2*0.25 + score_3*0.15 | |
#net_scores.append(net_score) | |
final_list[i] = net_score | |
## print 'final_list[' + str(net_score)+'] = ' + str(i) | |
## print '' | |
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)) | |
## print 'final_list --> ' + str(final_list) | |
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 | |
for i in req_users: | |
a = users.find_one({'_id':i}) | |
print a['username'] | |
return req_users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment