public string ListUnionGet() { BLL.Recommend_ResouceBLL bll = new BLL.Recommend_ResouceBLL(); string ii = Newtonsoft.Json.JsonConvert.SerializeObject(bll.ListUnion("sa16045")); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("您可能需要这些资源", ii); string result = Newtonsoft.Json.JsonConvert.SerializeObject(dic); return(result); }
public string GetAll(Models.MyUserModels myUserModels) { if (myUserModels == null || myUserModels.UId == null || myUserModels.UId == "") { return("error:用户名不对!"); } else { BLL.Recommend_ResouceBLL bll = new BLL.Recommend_ResouceBLL(); string ii = Newtonsoft.Json.JsonConvert.SerializeObject(bll.GetAll(myUserModels.UId)); return(ii); } }
public string ListUnion(Models.MyUserModels myUserModels) { if (myUserModels == null || myUserModels.UId == null || myUserModels.UId == "") { return("error:用户名不对!"); } else { BLL.Recommend_ResouceBLL bll = new BLL.Recommend_ResouceBLL(); string ii = Newtonsoft.Json.JsonConvert.SerializeObject(bll.ListUnion(myUserModels.UId)); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("您可能需要这些资源", ii); string result = Newtonsoft.Json.JsonConvert.SerializeObject(dic); return(result); } }
public int operationDistance() { SqlHelper sh = new SqlHelper(); sh.Open(); OperationDAL opt = new OperationDAL(sh); opt.DeleteRecommend(); //删除推荐结果表中数据 try { //1、取出所有老师的ID List <string> techIDs = opt.getAllTechID(); //2、构造每个老师的评分表 Dictionary <string, Dictionary <string, double> > dic = new Dictionary <string, Dictionary <string, double> >(); dic = opt.makeMarkTable(techIDs); //3、计算距离(数据稀疏-->余弦相似度;分数膨胀-->皮尔逊;稀疏且膨胀-->修正余弦相似度) //(1).计算每个老师对资源打分的平均值 这部分存在问题 //Dictionary<string, double> dicAveg = new Dictionary<string, double>(); //foreach (string tID in dic.Keys) //{ // double sum = 0.0; // //遍历教师tID对资源的评分 // foreach (string res in dic[tID].Keys) // { // sum = sum + dic[tID][res]; // } // double aveg = sum / dic[tID].Count; // dicAveg.Add(tID, aveg); //} //计算每个用户同其他用户的(曼哈顿)距离,并按远近排序 Dictionary <string, Dictionary <string, double> > sortDic = new Dictionary <string, Dictionary <string, double> >(); foreach (string tid in dic.Keys) { Dictionary <string, double> neghborDic = nearestNeighbor(tid, dic); sortDic.Add(tid, neghborDic); } //4.在排序后的评分字典中为每个取出最相近的K个,把这些用户评分高并且推荐目标没有用过的资源作为推荐项,把推荐结果保存(更新)到数据库 foreach (string tid in sortDic.Keys) { Dictionary <string, double> dicJuLi = sortDic[tid]; List <string> techList = new List <string>(); int K = 5; //K近邻 List <string> recomlist = new List <string>(); for (int i = 0; i < K; i++) { KeyValuePair <string, double> kvp = dicJuLi.ElementAt(i); foreach (string res in dic[kvp.Key].Keys) { if (dic[kvp.Key][res] > 2 && !recomlist.Contains(res)) { //评分大于3的加入推荐候选项列表 recomlist.Add(res); } } } //找出本用户已经用过的资源列表 List <string> usedlist = new List <string>(); foreach (string res in dic[tid].Keys) { usedlist.Add(res); } //recomlist和usedlist做差集 recomlist = recomlist.Except(usedlist).ToList <string>(); if (recomlist.Count < 5) { //推荐项少于5,补充社会化属性标签推荐结果 BLL.Recommend_ResouceBLL bll = new BLL.Recommend_ResouceBLL(); List <string> recomlistB = bll.ListUnion(tid); foreach (string res in recomlistB) { if (!recomlist.Contains(res) && recomlist.Count < 5) { recomlist.Add(res); } } } //把用户ID和推荐结果集作为参数调用插入数据库的方法 int temp = opt.saveResult(tid, recomlist); } } catch (Exception ex) { return(0); } return(1); }