/// <summary> /// User gets number of choices to compute similaries and recommendations /// </summary> public static void PickAlgorithm() { IDistance iDistance = null; IReader iReader = null; Console.WriteLine("Pick 1 for Euclidean"); Console.WriteLine("Pick 2 for Pearson"); Console.WriteLine("Pick 3 for Cosine"); Console.WriteLine("Pick 4 for Recommendation"); Console.WriteLine("Pick 5 for NearestNeighbor"); choice = int.Parse(Console.ReadLine()); Console.WriteLine("Pick 1 for Basic Dataset"); Console.WriteLine("Pick 2 for Advanced Dataset"); choiceData = int.Parse(Console.ReadLine()); Dictionary <int, double[, ]> dictionaryBasic = new Dictionary <int, double[, ]>(); Dictionary <int, double[, ]> dictionaryAdvanced = new Dictionary <int, double[, ]>(); switch (choiceData) { case 1: iReader = new FileReader(); dictionaryBasic = iReader.GetData(); break; case 2: iReader = new DataReader(); dictionaryAdvanced = iReader.GetData(); break; default: Console.WriteLine("Closed"); Console.ReadLine(); break; } switch (choice) { case 1: PickTargetUsers(); Console.WriteLine("You have chosen Euclidian"); if (choiceData == 1) { iDistance = new Euclidean(); IterateSimilarity(dictionaryBasic, targetUser, iDistance); } else { iDistance = new Euclidean(); IterateSimilarity(dictionaryAdvanced, targetUser, iDistance); } break; case 2: PickTargetUsers(); Console.WriteLine("You have chosen Pearson"); if (choiceData == 1) { iDistance = new Pearson(); IterateSimilarity(dictionaryBasic, targetUser, iDistance); } else { iDistance = new Pearson(); IterateSimilarity(dictionaryAdvanced, targetUser, iDistance); } break; case 3: PickTargetUsers(); Console.WriteLine("You have chosen Cosine"); if (choiceData == 1) { iDistance = new Cosine(); IterateSimilarity(dictionaryBasic, targetUser, iDistance); } else { iDistance = new Cosine(); IterateSimilarity(dictionaryAdvanced, targetUser, iDistance); } break; case 4: Console.WriteLine("Select Targeted User"); targetUser = int.Parse(Console.ReadLine()); Console.WriteLine("You have chosen Recommendation"); Console.WriteLine("Select Top numbers of ranking"); int k = int.Parse(Console.ReadLine()); Console.WriteLine("Set up your threshold"); double threshold = double.Parse(Console.ReadLine()); if (choiceData == 1) { Recommender.NearestNeighbour.ComputeRecommendations(targetUser, dictionaryBasic, k, threshold); } else { Recommender.NearestNeighbour.ComputeRecommendations(targetUser, dictionaryAdvanced, k, threshold); } break; case 5: Console.WriteLine("Select Targeted User"); targetUser = int.Parse(Console.ReadLine()); Console.WriteLine("You have chosen NearestNeighbor"); Console.WriteLine("Select K"); k = int.Parse(Console.ReadLine()); Console.WriteLine("Set up your threshold"); threshold = double.Parse(Console.ReadLine()); iDistance = new Pearson(); if (choiceData == 1) { RecommendNearestNeighbour.Run(threshold, targetUser, iDistance, dictionaryBasic, k); } else { Console.WriteLine("How many users"); int amount = int.Parse(Console.ReadLine()); RecommendNearestNeighbour.Run(threshold, targetUser, iDistance, dictionaryAdvanced.OrderBy(x => x.Key).Take(amount).ToDictionary(pair => pair.Key, pair => pair.Value), k); } break; default: Console.WriteLine("Closed"); Console.ReadLine(); break; } }
private static List <Tuple <double, User> > nearestNeighbours(User selectedUser, List <int> emptyRatings, double smt, int amount) { List <Tuple <double, User> > nn_Euclidian = new List <Tuple <double, User> >(); var nn_Pearson = new List <Tuple <double, User> >(); List <Tuple <double, User> > nn_Cosine = new List <Tuple <double, User> >(); //loop through all the users and find the users that are most similar to selecteduser foreach (var user in userpref) { var user2 = userpref[user.Key]; if (selectedUser.id != user.Key) { var difference = 0; foreach (var rating in emptyRatings) { if (user2.articleRating[rating] != 0) { difference++; } } if (difference <= 0) { continue; } //TODO refactor this mess, only pearson has treshold atm Console.WriteLine("Compare user: "******" with user: "******"---------------------"); ed = new EuclidianDistance(); var euclidian_distance = ed.calculate(selectedUser, user2, uniqueArticles); user2.euclidean_distance = euclidian_distance; nn_Euclidian.Add(Tuple.Create(euclidian_distance, user2)); Console.WriteLine("Euclidian stuff: " + euclidian_distance); pe = new Pearsons(); var correlation = pe.calculate(selectedUser, user2, uniqueArticles); if (correlation >= smt) { var neighbour = Tuple.Create(correlation, user2); if (nn_Pearson.Count < amount) { nn_Pearson.Add(neighbour); if (nn_Pearson.Count() == amount) { smt = nn_Pearson.Min(x => x.Item1); } } else { nn_Pearson.Add(neighbour); var lowestSimilarityUser = nn_Pearson.OrderBy(x => x.Item1).First(); nn_Pearson.Remove(lowestSimilarityUser); smt = nn_Pearson.OrderBy(x => x.Item1).First().Item1; } } Console.WriteLine("Pearsons corr: " + correlation); co = new Cosine(); var cosine_similarity = co.calculate(selectedUser, user2, uniqueArticles); user2.cosine = cosine_similarity; nn_Cosine.Add(Tuple.Create(cosine_similarity, user2)); Console.WriteLine("Cosine similarity: " + cosine_similarity + "\n"); } else { continue; } } //maybe print the first 3 of this stuff List <Tuple <double, User> > nn_Euclidian_Sorted = nn_Euclidian.OrderByDescending(x => x.Item1).ToList(); List <Tuple <double, User> > nn_Pearson_Sorted = nn_Pearson.OrderByDescending(x => x.Item1).ToList(); List <Tuple <double, User> > nn_Cosine_Sorted = nn_Cosine.OrderByDescending(x => x.Item1).ToList(); for (var i = 0; i < amount_nearestNeighbours; i++) { Console.WriteLine("Nearest {0}", i + 1 + "\n----------------"); Console.WriteLine("Euclidian nn: " + nn_Euclidian_Sorted[i].Item2.id); Console.WriteLine("Pearson nn: " + nn_Pearson_Sorted[i].Item2.id); Console.WriteLine("consine nn: " + nn_Cosine_Sorted[i].Item2.id + "\n"); } return(nn_Pearson_Sorted); }