Ejemplo n.º 1
0
 public Book[] GetBooksFromGivenBookScores(IEnumerable <string> bookIds)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.Books.Where(x => bookIds.Contains(x.ISBN)).ToArray());
     }
 }
Ejemplo n.º 2
0
 public Book[] GetBooksReadByUser(User user)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.BooksRatings.AsNoTracking().Where(x => x.UserId == user.UserId).Select(z => z.Book).ToArray());
     }
 }
Ejemplo n.º 3
0
 public User[] GetAllUsersWithRatedBooks()
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.BooksRatings.AsNoTracking().Select(x => x.User).Distinct().ToArray());
     }
 }
Ejemplo n.º 4
0
 public IEnumerable <string> GetBooksIdsRatedByAtLeastNUsers(int n)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         var books = db.BooksRatings.AsNoTracking().GroupBy(y => y.ISBN);
         return(books.Where(x => x.Count() >= n).OrderByDescending(x => x.Count()).Select(x => x.Key).ToArray());
     }
 }
Ejemplo n.º 5
0
 public string[] GetBooksIdsRatedByAtLeastNUsers(string[] ids, int n)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         var books = db.BooksRatings.AsNoTracking().Where(x => ids.Contains(x.ISBN)).GroupBy(y => y.ISBN);
         return(books.Where(x => x.Count() >= n).Select(x => x.Key).ToArray());
     }
 }
Ejemplo n.º 6
0
 public List <UserSimilar> GetUserNeighbors(int userId, int settingsVersion)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.UserSimilars.AsNoTracking()
                .Where(x => x.UserId == userId && x.ParametersSet == settingsVersion)
                .ToList());
     }
 }
Ejemplo n.º 7
0
        public void AddSimilarUsers(List <UsersSimilarity> similarUsers, int settingsVersion)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                var users = similarUsers.Select(x => MapToUserSimilarDataModel(x, settingsVersion)).ToList();

                db.UserSimilars.AddRange(users);
                db.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        public void AddTestResult(List <BookScore> score, int settingVersion)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                var result = score.Where(x => x.PredictedRate > 0)
                             .Select(bookScore => MapScoreToTest(bookScore, settingVersion));

                db.Tests.AddRange(result);
                db.SaveChanges();
            }
        }
Ejemplo n.º 9
0
 public int[] GetAllUsersWithComputedSimilarity(int settingVersion)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.UserSimilars.AsNoTracking()
                .Where(x => x.ParametersSet == settingVersion)
                .Select(x => x.UserId)
                .Distinct()
                .ToArray());
     }
 }
Ejemplo n.º 10
0
 public Book[] GetRecommendedBooksForUser(int userId)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.BookRecomendations
                .Where(x => x.UserId == userId)
                .OrderByDescending(x => x.PredictedRate)
                .Select(b => b.Book)
                .ToArray());
     }
 }
Ejemplo n.º 11
0
 public List <int> GetUsersWhoRatedAnyOfGivenBooks(string[] bookIds)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.BooksRatings.AsNoTracking()
                .Where(x => bookIds.Contains(x.ISBN))
                .Select(x => x.UserId)
                .Distinct()
                .ToList());
     }
 }
Ejemplo n.º 12
0
 public double?GetAverageRateForUser(int userId)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         var ratings = db.BooksRatings.AsNoTracking().Where(x => x.UserId == userId).ToArray();
         if (ratings.Length == 0)
         {
             return(null);
         }
         var average = ratings.Average(x => x.Rate);
         return(Math.Round(average, 2));
     }
 }
Ejemplo n.º 13
0
 public BookScore[] GetBooksRatesByUserId(int userId)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         return(db.BooksRatings.AsNoTracking()
                .Where(x => x.UserId == userId)
                .AsEnumerable()
                .Select(x => new BookScore {
             BookId = x.ISBN, Rate = x.Rate, UserId = userId
         })
                .ToArray());
     }
 }
Ejemplo n.º 14
0
        public void SaveParametersSet(Parameter parameter)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                var temp = db.Parameters.Find(parameter.Id);
                if (temp == null)
                {
                    db.Parameters.Add(parameter);
                }

                db.SaveChanges();
            }
        }
Ejemplo n.º 15
0
        public List <int> GetUserIdsWithNorMoreRatedBooks(int n)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                var users = db.BooksRatings
                            .AsNoTracking()
                            .Where(x => x.User.BooksRatings.Count >= n && x.User.BooksRatings.Count < 1000)
                            .Select(x => x.UserId)
                            .ToList();

                return(users.Distinct()
                       .ToList());
            }
        }
Ejemplo n.º 16
0
        public void AddTestResults(List <BookScore[]> scores, int settingVersion)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                var result = new List <Test>();
                foreach (var score in scores)
                {
                    result.AddRange(score.Select(bookScore => MapScoreToTest(bookScore, settingVersion)));
                }

                db.Tests.AddRange(result);
                db.SaveChanges();
            }
        }
Ejemplo n.º 17
0
        public void AddRecommendedBooksForUser(BookScore[] books, int userId)
        {
            using (var db = new BooksRecomendationsEntities())
            {
                //delete any if exist
                db.Database.ExecuteSqlCommand("Delete from BookRecomendation where UserId=@userId",
                                              new SqlParameter("@userId", userId));

                if (books != null)
                {
                    db.BookRecomendations.AddRange(books.Select(x => MapToBookRecommendationModel(x, userId)));
                }

                db.SaveChanges();
            }
        }
Ejemplo n.º 18
0
 public Parameter GetParameters(int settingsVersion)
 {
     using (var db = new BooksRecomendationsEntities())
     {
         var settings = db.Parameters.Where(x => x.Id == settingsVersion).ToList();
         return(settings.Select(x =>
                                new Parameter
         {
             BookPopularity = x.BookPopularity,
             DistanceSimilarityType = x.DistanceSimilarityType,
             DistanceType = x.DistanceType,
             Id = x.Id,
             Kneigbors = x.Kneigbors,
             NumberOfBooksEachUserRated = x.NumberOfBooksEachUserRated
         })
                .FirstOrDefault());
     }
 }