示例#1
0
        static void Main(string[] args)
        {
            KMeans km = new KMeans();

            int nItemsCount = km.LoadItemsFromFile(FileNames.MoviesFile);

            Dictionary <string, List <string> > usersAndTheirSeenMovies = new Dictionary <string, List <string> >();

            usersAndTheirSeenMovies = FileNames.LoadUserDataFromFile(FileNames.UsersMovies);

            var user = ReadUserForRecommendation(usersAndTheirSeenMovies);

            var seenMovies = usersAndTheirSeenMovies[user];

            IClustersList obtainedClusters = new IClustersList();

            obtainedClusters = ComputeClustering(km, FileNames.GenresFile, nItemsCount, "Enter the number of movies per genre");

            File.Delete(FileNames.RecommendedMovies);
            RecommendMoviesBasedOnClusters(seenMovies, obtainedClusters);

            km          = new KMeans();
            nItemsCount = km.LoadItemsFromFile(FileNames.RecommendedMovies);
            var newObtainedClusters = ComputeClustering(km, FileNames.RankingFile, nItemsCount, "Enter the number of movies per rank");

            RecommendMoviesBasedOnClusters(seenMovies, newObtainedClusters, true);

            Console.Read();
        }
示例#2
0
        public object Clone()
        {
            IClustersList TargetClustersList = new IClustersList();

            foreach (ICluster cluster in ClustersList)
            {
                IItemsList TargetCentroidsList = new IItemsList();
                foreach (Item centroid in (IItemsList)cluster.Centroids.Clone())
                {
                    TargetCentroidsList.Add(new Item(centroid.ItemText, (IAttributeList)centroid.AttributeList.Clone(),
                                                     centroid.Distance, centroid.IsCentroid, centroid.Exists));
                }

                IItemsList TargetItemsList = new IItemsList();
                foreach (Item item in (IItemsList)cluster.Items.Clone())
                {
                    TargetItemsList.Add(new Item(item.ItemText, (IAttributeList)item.AttributeList.Clone(),
                                                 item.Distance, item.IsCentroid, item.Exists));
                }

                TargetClustersList.Add(new ICluster((IItemsList)TargetCentroidsList.Clone(),
                                                    (IItemsList)TargetItemsList.Clone()));
            }

            return(TargetClustersList);
        }
示例#3
0
        private static void RecommendMoviesBasedOnClusters(List <string> seenMovies, IClustersList obtainedClusters, bool isRankingBased = false)
        {
            Dictionary <int, List <KeyValuePair <ICluster, List <string> > > > ocurrencesAndClustersMapping = new Dictionary <int, List <KeyValuePair <ICluster, List <string> > > >();
            int sum = 0;

            foreach (ICluster cluster in obtainedClusters.ClustersList)
            {
                int           countPerCluster = 0;
                List <string> seenMovieItems  = new List <string>();
                foreach (var item in cluster.Items.ItemsList)
                {
                    foreach (string movieName in seenMovies)
                    {
                        if (item.ItemText.Contains(movieName))
                        {
                            countPerCluster += 1;
                            seenMovieItems.Add(item.ItemText);
                            sum += Int32.Parse(item.ItemText.Split(' ').LastOrDefault());
                        }
                    }
                }
                if (ocurrencesAndClustersMapping.ContainsKey(countPerCluster))
                {
                    ocurrencesAndClustersMapping[countPerCluster].Add(new KeyValuePair <ICluster, List <string> >(cluster, seenMovieItems));
                }
                else
                {
                    ocurrencesAndClustersMapping.Add(countPerCluster, new List <KeyValuePair <ICluster, List <string> > > {
                        new KeyValuePair <ICluster, List <string> >(cluster, seenMovieItems)
                    });
                }
            }

            if (!isRankingBased)
            {
                Average = sum / seenMovies.Count;
            }

            var maxKey = ocurrencesAndClustersMapping.Keys.Max();

            var chosenClusterData = ocurrencesAndClustersMapping[maxKey];

            var recommendations = new List <string>();

            foreach (var data in chosenClusterData)
            {
                foreach (var item in data.Key.Items.ItemsList)
                {
                    if (!data.Value.Contains(item.ItemText))
                    {
                        if (isRankingBased)
                        {
                            if (Int32.Parse(item.ItemText.Split().LastOrDefault()) >= Average)
                            {
                                recommendations.Add(item.ItemText);
                            }
                        }
                        else
                        {
                            recommendations.Add(item.ItemText);
                            File.AppendAllLines(FileNames.RecommendedMovies, new List <string> {
                                item.ItemText
                            });
                        }
                    }
                }
            }

            Console.WriteLine("Future recommendations");
            Console.WriteLine("_____________________");
            foreach (var recomm in recommendations)
            {
                Console.WriteLine(recomm);
            }
        }