private List <string> recommendNN(RecommendationMethod sAlgorithm, string sUserId, int cRecommendations) { Dictionary <string, double> sumWmovies = new Dictionary <string, double>(); Dictionary <string, double> similarUsers = calcSimilarUsers(sAlgorithm, sUserId); foreach (string movie in m_trainMovieToUser.Keys) { if (m_trainMovieToUser[movie].Contains(sUserId)) { continue; } foreach (string user in similarUsers.Keys) { if (m_trainMovieToUser[movie].Contains(user)) { if (!sumWmovies.ContainsKey(movie)) { sumWmovies.Add(movie, 0); } sumWmovies[movie] += similarUsers[user]; } } } List <string> moviesSorted = sumWmovies.Keys.ToList(); moviesSorted.Sort((a, b) => sumWmovies[a].CompareTo(sumWmovies[b])); return(moviesSorted.Take(cRecommendations).ToList()); }
public List <string> Recommend(RecommendationMethod sAlgorithm, string sUserId, int cRecommendations) { List <string> result = new List <string>(); switch (sAlgorithm) { case (RecommendationMethod.Popularity): result = GetPopularItems(sUserId, cRecommendations); break; case (RecommendationMethod.Pearson): result = GetTopItems(predictionEngine.getModel(PredictionMethod.Pearson), sUserId, cRecommendations); break; case (RecommendationMethod.Cosine): result = GetTopItems(predictionEngine.getModel(PredictionMethod.Cosine), sUserId, cRecommendations); break; case (RecommendationMethod.BaseModel): result = GetTopItems(predictionEngine.getModel(PredictionMethod.BaseModel), sUserId, cRecommendations); break; case (RecommendationMethod.Stereotypes): result = GetTopItems(predictionEngine.getModel(PredictionMethod.Stereotypes), sUserId, cRecommendations); break; case (RecommendationMethod.NNPearson): result = GetTopItemsBasedNN(new PearsonMethod(), sUserId, cRecommendations); break; case (RecommendationMethod.NNCosine): result = GetTopItemsBasedNN(new CosineMethod(), sUserId, cRecommendations); break; case (RecommendationMethod.NNBaseModel): result = GetTopItemsBasedNN(new BaseModelMethod(predictionEngine.getModel(PredictionMethod.BaseModel)), sUserId, cRecommendations); break; case (RecommendationMethod.NNJaccard): result = GetTopItemsBasedNN(new JaccardMethod(), sUserId, cRecommendations); break; case (RecommendationMethod.CP): result = GetTopItemsCp(sUserId, cRecommendations); break; case (RecommendationMethod.Jaccard): result = GetTopItemsJaccard(sUserId, cRecommendations); break; } return(result); }
private Dictionary <string, double> calcSimilarUsers(RecommendationMethod sAlgorithm, string sUserId) { Dictionary <string, double> userW = new Dictionary <string, double>(); List <int> usedLocations = new List <int>(); double sumOfW = 0; int numOfUsers = 0; Random r = new Random(); while (numOfUsers < 500) //not going over all the users to save time { if (numOfUsers >= 20 && (sumOfW / (double)numOfUsers) > 0.8) //!! { break; } int location = (int)((m_train.Count - 1) * r.NextDouble()); if (usedLocations.Contains(location)) { continue; } string user = m_train.Keys.ToList()[location]; if (user.Equals(sUserId) /*|| userW.ContainsKey(user)*/) { continue; } double w = 0; if (sAlgorithm == RecommendationMethod.NNCosine) { w = calcWCosine(sUserId, user, ""); } else if (sAlgorithm == RecommendationMethod.NNJaccard) { w = calcJaccardSimilarity(sUserId, user); } else if (sAlgorithm == RecommendationMethod.BaseModel) { w = calcBaseModelSimilarity(sUserId, user); } else { w = calcWPearson(sUserId, user, ""); } if (w >= 0.5)//!! { sumOfW += w; numOfUsers++; userW.Add(user, w); } } return(userW.OrderBy(x => x.Value).Take(20).ToDictionary(x => x.Key, x => x.Value)); }
public List <string> Recommend(RecommendationMethod sAlgorithm, string sUserId, int cRecommendations) { if (sAlgorithm == RecommendationMethod.Popularity) { return(recommendPopularity(sUserId, cRecommendations)); } else if (sAlgorithm.ToString().StartsWith("NN")) { return(recommendNN(sAlgorithm, sUserId, cRecommendations)); } else if (sAlgorithm == RecommendationMethod.Jaccard) { return(recommendCP(sUserId, cRecommendations, true)); } else if (sAlgorithm == RecommendationMethod.CP) { return(recommendCP(sUserId, cRecommendations, false)); } else //prediction { PredictionMethod predictionMethod = PredictionMethod.Pearson; if (sAlgorithm == RecommendationMethod.BaseModel) { predictionMethod = PredictionMethod.BaseModel; } else if (sAlgorithm == RecommendationMethod.Cosine) { predictionMethod = PredictionMethod.Cosine; } else if (sAlgorithm == RecommendationMethod.Stereotypes) { predictionMethod = PredictionMethod.Stereotypes; } return(recommendPredictions(sUserId, cRecommendations, predictionMethod)); } }
public List <string> Recommend(RecommendationMethod sAlgorithm, string sUserId, int cRecommendations) { throw new NotImplementedException(); }