Created
July 29, 2014 12:31
-
-
Save prashantdce19/c1a60edaca0e3c69aa90 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/convflowdev_trial'); | |
console.log("chal para"); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error:')); | |
db.once('open', function callback () { | |
// yay! | |
}); | |
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); | |
users.find({}).exec(function(err, result) { | |
if (!err) { | |
//all_users = result | |
//for(i=0;i<result.length;i++) | |
//{usr.push(result[i])} | |
} else { | |
// error handling | |
}; | |
}); | |
problems.find({'keywords':{'$in':['innovation']}}).exec(function(err, result) { | |
if (!err) { | |
//console.log(result) //console.log(result) | |
//foo(null, result); | |
} else { | |
// error handling | |
}; | |
}); | |
function foo(err, results) { | |
console.log(results); | |
} | |
function count_keywords(posted_problem_keywords,search_problem_keywords) //def 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(keywords,number) | |
{ //problems.find({'_id':qID}).exec(function(err, question) { | |
// if (!err) { | |
// console.log(question) | |
// keywords = ['innovation'] | |
problems.find({'keywords':{'$in':keywords}}).exec(function(err, req_users) { | |
if (!err) { | |
//console.log(req_users[0]); | |
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); | |
//console.log(new_list_of_id_count); | |
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])} | |
//console.log(list_of_ids) | |
//console.log(list_of_ids); | |
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])); } | |
//console.log(priority_list_order_1) | |
//console.log(priority_list_order_2) | |
//console.log(priority_list_order_3) | |
//console.log(values) | |
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) { //console.log(req_users1); | |
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) | |
{//console.log(req_users2); | |
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) { | |
//console.log(req_users3); | |
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)); | |
//console.log(user_l1); | |
//console.log(user_l2); | |
//console.log(user_l3); | |
//console.log(f_user_list); | |
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; | |
} | |
//console.log(final_list) | |
var values = Object.keys(final_list).map(function(key){return final_list[key]}); | |
var all_scores = underscore.uniq(values) | |
//console.log(all_scores) | |
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 | |
} | |
//console.log(ff_dict); | |
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<number) | |
{ | |
final_req_users = underscore.union(final_req_users,ff_dict[req_scores[i]]); | |
} | |
else | |
{break;} | |
} | |
absolutely_final = final_req_users.slice(0,number) | |
console.log(absolutely_final); | |
}}); | |
}}); | |
} | |
}); | |
} | |
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(['innovation'],12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment