Exemplo n.º 1
0
        public static Dictionary<int, Movie> ParseFromFolder(string folder, int limit = NOLIMIT)
        {
            if (!Directory.Exists(folder)) return null;

            Dictionary<int, Movie> movies = new Dictionary<int, Movie>();
            foreach (var file in Directory.EnumerateFiles(folder))
            {
                if (limit > -1 && movies.Count == limit)
                    break;

                string[] lines = File.ReadAllLines(file);
                int movieId = Int32.Parse(lines[0].Replace(":", string.Empty));
                Movie movie = new Movie(movieId);
                for (int i = 1; i < lines.Length; i++)
                {
                    string[] fields = lines[i].Split(',');
                    int userId = Int32.Parse(fields[0]);
                    int rating = Int32.Parse(fields[1]);
                    movie.AddOrUpdate(userId, rating);
                }
                movies.Add(movieId, movie);
            }

            return movies;
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Control.UseNativeMKL();
            bool readFromFile = true;

            if (!readFromFile)
            {
                Dictionary<int, Movie> allTestMovies = MovieParser.ParseFromFolder(@"C:\Users\Frederik\Desktop\download\download\training_set\training_set", 1000);
                List<Movie> rawProbes = ProbeParser.Parse(@"C:\Users\Frederik\Desktop\download\download\probe.txt", 1000);

                List<Movie> tempProbes = new List<Movie>();
                foreach (var probe in rawProbes)
                {
                    if (allTestMovies.ContainsKey(probe.Id))
                    {
                        Movie movie = new Movie(probe.Id);
                        foreach (var userId in probe.Ratings.Keys)
                        {
                            int rating = allTestMovies[probe.Id].GetRating(userId);
                            movie.AddOrUpdate(userId, rating);
                            allTestMovies[probe.Id].RemoveRating(userId);
                        }
                        tempProbes.Add(movie);
                    }
                }
                rawProbes = null;
                GC.Collect();
                //WriteListToFile("testMovies.txt", allTestMovies.Values.ToList());
                allTestMovies = null;
                GC.Collect();
                WriteListToFile("probeMovie.txt", tempProbes);
                tempProbes = null;
                GC.Collect();
            }

            //List<Movie> testMovies = MovieParser.Parse("testMovies.txt");
            List<Movie> probes = MovieParser.Parse("probeMovie.txt");

            Preprocesser preprocesser = new Preprocesser();
            preprocesser.BuildRatingMatrix(probes);
            SparseMatrix matrix = preprocesser.GetMeanMatrix();
            Factorization factorization = new Factorization();
            factorization.Factorize(matrix);
            //double sum = matrix.SumMatrix();
            //Console.WriteLine(sum);


            Console.ReadLine();
        }
Exemplo n.º 3
0
        public static List<Movie> Parse(string file, int limit = NOLIMIT)
        {
            if (!File.Exists(file)) return null;

            List<Movie> movies = new List<Movie>();

            int previousId = default(int);
            int movieId = default(int);
            Dictionary<int, int> ratings = new Dictionary<int, int>();
            using (StreamReader reader = new StreamReader(file))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (line.Contains(":"))
                    {
                        if (movieId != previousId)
                        {
                            Movie movie = new Movie(movieId);
                            movie.AddOrUpdate(ratings);
                            movies.Add(movie);
                            previousId = movieId;
                            ratings = new Dictionary<int, int>();
                        }
                        movieId = Int32.Parse(line.Replace(":", string.Empty));
                    }
                    else
                    {
                        string[] fields = line.Split(',');
                        int userId = Int32.Parse(fields[0]);
                        int rating = Int32.Parse(fields[1]);
                        ratings.Add(userId, rating);
                    }
                }
                Movie m = new Movie(movieId);
                m.AddOrUpdate(ratings);
                movies.Add(m);
            }

            return movies;
        }
Exemplo n.º 4
0
        public static List<Movie> Parse(string file, int limit = NOLIMIT)
        {
            if (!File.Exists(file)) return null;

            List<Movie> movies = new List<Movie>();
            
            int movieId = default(int);
            List<int> userIds = new List<int>();
            using (StreamReader reader = new StreamReader(file))
            {
                while (!reader.EndOfStream)
                {
                    if (limit > -1 && movies.Count == limit)
                        break;

                    string line = reader.ReadLine();
                    if (line.Contains(":"))
                    {
                        if (movieId != default(int))
                        {
                            Movie movie = new Movie(movieId);
                            foreach (var userId in userIds)
                            {
                                movie.AddOrUpdate(userId, 0);
                            }
                            movies.Add(movie);
                            userIds = new List<int>();
                        }
                        movieId = Int32.Parse(line.Replace(":", string.Empty));
                    }
                    else
                    {

                        int userId = Int32.Parse(line);
                        userIds.Add(userId);
                    }
                }
            }

            return movies;
        }