Last active
August 29, 2015 14:04
-
-
Save prashantdce19/396576b5f7023b8b8ef2 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
var underscore = require('underscore') | |
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/db'); | |
var Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId; | |
var userSchema = new Schema({ | |
username: String, | |
displayname: String, | |
timeCreated: Date, | |
_id: ObjectId, | |
organization: ObjectId | |
}); | |
var users = mongoose.model('users', userSchema); | |
var problemsSchema = new Schema({ | |
voters: Array, | |
title: String, | |
creator: ObjectId, | |
creationtime: Date, | |
acl: {mode: String, groups: Array, users: Array}, | |
keywords: Array, | |
_id: ObjectId | |
}); | |
var problems = mongoose.model('problems', problemsSchema); | |
function count_keywords(posted_problem_keywords,search_problem_keywords) | |
{ | |
count = 0; | |
p = underscore.intersection(posted_problem_keywords,search_problem_keywords); | |
count = p.length; | |
return count | |
} | |
function find_similiar_problems(qID,number) | |
{ problems.find({'_id':qID}).exec(function(err, question) { | |
if (!err) { | |
keywords = question[0]['keywords']; | |
user_id = question[0]['creator']; | |
problems.find({'keywords':{'$in':keywords}}).exec(function(err, req_users) { | |
if (!err) { | |
list_of_id_count = [] | |
for(i=0;i<req_users.length;i++) | |
{ | |
ID = req_users[i]['_id']; | |
COUNT = count_keywords(keywords,req_users[i]['keywords']); | |
list_of_id_count.push([ID,count]); | |
} | |
new_list_of_id_count = list_of_id_count.sort(Comparator); | |
list_of_ids = [] | |
for(i=0;i<new_list_of_id_count.length;i++) | |
{list_of_ids.push(new_list_of_id_count[i][0])} | |
priority_list_order_1 = [] | |
priority_list_order_2 = [] | |
priority_list_order_3 = [] | |
for(i=0;i<new_list_of_id_count.length;i++) | |
if (new_list_of_id_count[i][1]>3) | |
{priority_list_order_1.push(mongoose.Types.ObjectId(new_list_of_id_count[i][0])); } | |
else if (new_list_of_id_count[i][1]>1 && new_list_of_id_count[i][1]<4) | |
{priority_list_order_2.push(mongoose.Types.ObjectId(new_list_of_id_count[i][0])); } | |
else if (new_list_of_id_count[i][1]==1) | |
{priority_list_order_3.push(mongoose.Types.ObjectId(new_list_of_id_count[i][0])); } | |
problems.aggregate([ | |
{ "$match" : {'_id':{'$in':priority_list_order_1}}}, | |
{ "$unwind": "$acl.users" }, | |
{ "$group": | |
{"_id": "$acl.users", | |
"count": {"$sum": 1 } | |
} | |
} | |
]).exec(function(err, req_users1) { | |
if (!err) { | |
problems.aggregate([ | |
{ "$match" : {'_id':{'$in':priority_list_order_2}}}, | |
{ "$unwind": "$acl.users" }, | |
{ "$group": | |
{"_id": "$acl.users", | |
"count": {"$sum": 1 } | |
} | |
} | |
]).exec(function(err, req_users2){ | |
if (!err) | |
{ | |
problems.aggregate([ | |
{ "$match" : {'_id':{'$in':priority_list_order_3}}}, | |
{ "$unwind": "$acl.users" }, | |
{"$group": | |
{"_id": "$acl.users", | |
"count": {"$sum": 1 } | |
} | |
} | |
]).exec(function(err, req_users3) { | |
if (!err) { | |
user_l1 = {} | |
user_l2 = {} | |
user_l3 = {} | |
for(i=0;i<req_users1.length;i++) | |
{user_l1[mongoose.Types.ObjectId(req_users1[i]['_id'])] = req_users1[i]['count']} | |
for(i=0;i<req_users2.length;i++) | |
{user_l2[mongoose.Types.ObjectId(req_users2[i]['_id'])] = req_users2[i]['count']} | |
for(i=0;i<req_users3.length;i++) | |
{user_l3[mongoose.Types.ObjectId(req_users3[i]['_id'])] = req_users3[i]['count'];} | |
final_list = {}; | |
f_user_list = underscore.union(Object.keys(user_l1),Object.keys(user_l2),Object.keys(user_l3)); | |
for(i=0;i<f_user_list.length;i++) | |
{ | |
if (underscore.contains(Object.keys(user_l1),f_user_list[i])) | |
{ score_1 = parseFloat(user_l1[f_user_list[i]]); } | |
else | |
{ score_1 = 0;} | |
if (underscore.contains(Object.keys(user_l2),f_user_list[i])) | |
{ score_2 = parseFloat(user_l2[f_user_list[i]]);} | |
else | |
{score_2 = 0;} | |
if (underscore.contains(Object.keys(user_l3),f_user_list[i])) | |
{score_3 = parseFloat(user_l3[f_user_list[i]]);} | |
else | |
{score_3 = 0;} | |
net_score = score_1*0.6 + score_2*0.25 + score_3*0.15; | |
final_list[mongoose.Types.ObjectId(f_user_list[i])] = net_score; | |
} | |
var values = Object.keys(final_list).map(function(key){return final_list[key]}); | |
var all_scores = underscore.uniq(values) | |
var ff_dict = {}; | |
for(i=0;i<all_scores.length;i++) | |
{ | |
tmp_list = []; | |
var key_needed = Object.keys(final_list); | |
for(j=0;j<key_needed.length;j++) | |
{ | |
if(final_list[key_needed[j]] == all_scores[i]) | |
{ | |
tmp_list.push(mongoose.Types.ObjectId(key_needed[j])); | |
} | |
} | |
ff_dict[parseFloat(all_scores[i])] = tmp_list | |
} | |
netscores = Object.keys(ff_dict); | |
netscores_new = []; | |
for(i=0;i<netscores.length;i++) | |
{netscores_new.push(parseFloat(netscores[i]));} | |
latest_netscores = netscores_new.sort().reverse(); | |
//console.log(latest_netscores) | |
req_scores = latest_netscores.slice(0,number); | |
final_req_users = [] | |
for(i=0;i<req_scores.length;i++) | |
{ | |
if(final_req_users.length<2*number) | |
{ | |
final_req_users = underscore.union(final_req_users,ff_dict[req_scores[i]]); | |
} | |
else | |
{break;} | |
} | |
first_half = final_req_users.slice(0,2*number) | |
problems.aggregate([{ "$match" : {'acl.users':{'$in':[mongoose.Types.ObjectId(user_id)]}}}, | |
{ "$unwind": "$acl.users" }, | |
{ "$group":{"_id": "$acl.users","count": {"$sum": 1 } | |
}}]).exec(function(err, network_users) { | |
if (!err) | |
{ | |
users_in_network = [] | |
for(i=0;i<network_users.length;i++) | |
{ if(mongoose.Types.ObjectId(network_users[i]['_id'])!=user_id) | |
{users_in_network.push([mongoose.Types.ObjectId(network_users[i]['_id']),network_users[i]['count']]);} | |
} | |
second_half_1 = users_in_network.sort(Comparator).reverse().slice(0,2*number); | |
second_half = []; | |
for(i=0;i<second_half_1.length;i++) | |
{second_half.push(second_half_1[i][0]);} | |
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=0;i<first_half.length;i++) | |
{ | |
if (underscore.contains(second_half,mongoose.Types.ObjectId(first_half[i]))) | |
{net_users_in_network.push(first_half[i]);} | |
else | |
{net_users_not_in_network.push(first_half[i]);} | |
} | |
num_1 = net_users_in_network.slice(0,(number/2)+1).length; | |
num_2 = number - num_1; | |
net_users = underscore.union(net_users_in_network.slice(0,num_1),net_users_not_in_network.slice(0,num_2)); | |
console.log(net_users.slice(0,number)); | |
}}); | |
}}); | |
}}); | |
} | |
}); | |
} | |
else { | |
// error handling | |
} | |
}); | |
}}); | |
} | |
function Comparator(a,b){ | |
if (a[1] < b[1]) return -1; | |
if (a[1] > b[1]) return 1; | |
return 0; | |
} | |
find_similiar_problems(mongoose.Types.ObjectId('53d0b0bcd8a292144ce5c1c1'),12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment